-
-
Notifications
You must be signed in to change notification settings - Fork 64
Add support for 2XX and Default Response Objects #660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0280c29
Enhance return type resolution for OpenAPI operations
christianhelle 7d15dda
Add unit tests for default and range response generation in OpenAPI
christianhelle bae0de4
Fix assert to check for ICollection<T> instead of IEnumerable<T>
christianhelle 4ec0ea9
Reorder HTTP response range codes for precedence
christianhelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
src/Refitter.Tests/Examples/DefaultResponseObjectTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| using FluentAssertions; | ||
| using Refitter.Core; | ||
| using Refitter.Tests.Build; | ||
| using Xunit; | ||
|
|
||
| namespace Refitter.Tests.Examples; | ||
|
|
||
| public class DefaultResponseObjectTests | ||
| { | ||
| private const string OpenApiSpec = @" | ||
| openapi: '3.0.0' | ||
| info: | ||
| version: '1.0.0' | ||
| title: 'Default Response API' | ||
| description: 'An API that uses default responses and range responses' | ||
| servers: | ||
| - url: 'https://api.example.com/v1' | ||
| paths: | ||
| /users: | ||
| get: | ||
| tags: | ||
| - 'Users' | ||
| summary: 'Get users list using default response' | ||
| description: 'Returns a list of users using default response type' | ||
| operationId: 'getUsersWithDefault' | ||
| responses: | ||
| default: | ||
| description: 'List of users with unknown status code' | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: 'array' | ||
| items: | ||
| $ref: '#/components/schemas/User' | ||
| /users/{id}: | ||
| get: | ||
| tags: | ||
| - 'Users' | ||
| summary: 'Get user by ID using default response' | ||
| description: 'Returns a user by ID using default response type' | ||
| operationId: 'getUserByIdWithDefault' | ||
| parameters: | ||
| - name: 'id' | ||
| in: 'path' | ||
| description: 'User ID' | ||
| required: true | ||
| schema: | ||
| type: 'string' | ||
| responses: | ||
| default: | ||
| description: 'User data with unknown status code' | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/User' | ||
| /categories: | ||
| get: | ||
| tags: | ||
| - 'Categories' | ||
| summary: 'Get categories using 2XX range response' | ||
| description: 'Returns a list of categories using 2XX range response' | ||
| operationId: 'getCategoriesWithRange' | ||
| responses: | ||
| 2XX: | ||
| description: 'List of categories with 2XX status code' | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: 'array' | ||
| items: | ||
| $ref: '#/components/schemas/Category' | ||
| /categories/{id}: | ||
| get: | ||
| tags: | ||
| - 'Categories' | ||
| summary: 'Get category by ID using 2XX range response' | ||
| description: 'Returns a category by ID using 2XX range response' | ||
| operationId: 'getCategoryByIdWithRange' | ||
| parameters: | ||
| - name: 'id' | ||
| in: 'path' | ||
| description: 'Category ID' | ||
| required: true | ||
| schema: | ||
| type: 'string' | ||
| responses: | ||
| 2XX: | ||
| description: 'Category data with 2XX status code' | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: '#/components/schemas/Category' | ||
| components: | ||
| schemas: | ||
| User: | ||
| type: 'object' | ||
| properties: | ||
| id: | ||
| type: 'string' | ||
| name: | ||
| type: 'string' | ||
| email: | ||
| type: 'string' | ||
| format: 'email' | ||
| Category: | ||
| type: 'object' | ||
| properties: | ||
| id: | ||
| type: 'string' | ||
| name: | ||
| type: 'string' | ||
| "; | ||
|
|
||
| [Fact] | ||
| public async Task Can_Generate_Code() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| generatedCode.Should().NotBeNullOrWhiteSpace(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Can_Build_Generated_Code() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| BuildHelper | ||
| .BuildCSharp(generatedCode) | ||
| .Should() | ||
| .BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Should_Generate_Default_Response_Return_Types() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| generatedCode.Should().Contain("Task<User> GetUserByIdWithDefault(string id);"); | ||
| generatedCode.Should().Contain("Task<ICollection<User>> GetUsersWithDefault();"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Should_Generate_Range_Response_Return_Types() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| generatedCode.Should().Contain("Task<Category> GetCategoryByIdWithRange(string id);"); | ||
| generatedCode.Should().Contain("Task<ICollection<Category>> GetCategoriesWithRange();"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Should_Generate_User_Contract() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| generatedCode.Should().Contain("public partial class User"); | ||
| generatedCode.Should().Contain("public string Id { get; set; }"); | ||
| generatedCode.Should().Contain("public string Name { get; set; }"); | ||
| generatedCode.Should().Contain("public string Email { get; set; }"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Should_Generate_Category_Contract() | ||
| { | ||
| string generatedCode = await GenerateCode(); | ||
| generatedCode.Should().Contain("public partial class Category"); | ||
| generatedCode.Should().Contain("public string Id { get; set; }"); | ||
| generatedCode.Should().Contain("public string Name { get; set; }"); | ||
| } | ||
|
|
||
| private static async Task<string> GenerateCode() | ||
| { | ||
| var swaggerFile = await CreateSwaggerFile(OpenApiSpec); | ||
| var settings = new RefitGeneratorSettings | ||
| { | ||
| OpenApiPath = swaggerFile, | ||
| UseCancellationTokens = false | ||
| }; | ||
|
|
||
| var sut = await RefitGenerator.CreateAsync(settings); | ||
| var generatedCode = sut.Generate(); | ||
| return generatedCode; | ||
| } | ||
|
|
||
| private static async Task<string> CreateSwaggerFile(string contents) | ||
| { | ||
| var filename = $"{Guid.NewGuid()}.yml"; | ||
| var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
| Directory.CreateDirectory(folder); | ||
| var swaggerFile = Path.Combine(folder, filename); | ||
| await File.WriteAllTextAsync(swaggerFile, contents); | ||
| return swaggerFile; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already merged, still as a feedback: Does it really make sense to allow the non-positive response wildcards?
What I mean is: Shouldn't this be limited to
2XX. Because otherwise you should also allow things like404,500and so on, which doesn't really make sense, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pfeigl No it doesn't and they will also be removed. This was purely implemented by CoPilot in Agent mode as an experiment. This vibe coding is not really my thing and now I have to clean up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Impressive as it that an AI agent literally did everything and all I did was click on a button on GitHub to let an agent do all the work. The result had flaws in the end and human intervention is simply always required