-
Notifications
You must be signed in to change notification settings - Fork 0
feature/fesd-615_response_ordering #99
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
7 commits
Select commit
Hold shift + click to select a range
30eb6f2
WIP
PrincessMadMath 2d683d5
WIP
PrincessMadMath 84f9712
WIP
PrincessMadMath 41d3a12
feat: add response ordering and new endpoints for OpenAPI documentation
PrincessMadMath 504d403
feat: implement response ordering in OpenAPI configuration
PrincessMadMath 3468737
fix: correct spelling in endpoint and comments for response ordering
PrincessMadMath 71b86c5
refactor: change class modifiers for OperationIdController and OrderR…
PrincessMadMath 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
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
40 changes: 40 additions & 0 deletions
40
src/Workleap.Extensions.OpenAPI/Ordering/OrderResponseFilter.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,40 @@ | ||
| using Microsoft.OpenApi.Models; | ||
| using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
|
||
| namespace Workleap.Extensions.OpenAPI.Ordering; | ||
|
|
||
| /// <summary> | ||
| /// This filter ensures consistent ordering for better source control diffs and predictable documentation. | ||
| /// </summary> | ||
| internal sealed class OrderResponseFilter : IDocumentFilter | ||
| { | ||
| public void Apply(OpenApiDocument document, DocumentFilterContext context) | ||
| { | ||
| var paths = document.Paths.ToList(); | ||
| document.Paths.Clear(); | ||
| document.Paths = new OpenApiPaths(); | ||
| foreach (var path in paths) | ||
| { | ||
| document.Paths.Add(path.Key, path.Value); | ||
|
|
||
| var sortedOperations = path.Value.Operations.OrderBy(op => (int)op.Key).ToList(); | ||
| path.Value.Operations.Clear(); | ||
| foreach (var operation in sortedOperations) | ||
| { | ||
| path.Value.Operations.Add(operation.Key, operation.Value); | ||
|
|
||
| // Sort responses by status code (200, 400, 403, 404, 500, etc.) | ||
| // This is critical because responses from both controller-level ProducesResponseType | ||
| // and method-level attributes are added in the order they're processed, not by status code. | ||
| // Without sorting, a 403 from a controller-level attribute might appear before a 200 | ||
| // from the method-level TypedResult, causing unpredictable ordering and noisy diffs. | ||
| var sortedResponse = operation.Value.Responses.OrderBy(responseKvp => responseKvp.Key, StringComparer.Ordinal).ToList(); | ||
| operation.Value.Responses.Clear(); | ||
| foreach (var response in sortedResponse) | ||
| { | ||
| operation.Value.Responses.Add(response.Key, response.Value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
22 changes: 22 additions & 0 deletions
22
src/tests/WebApi.OpenAPI.SystemTest/Ordering/OrderingController.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,22 @@ | ||
| using Microsoft.AspNetCore.Http.HttpResults; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using System.Net.Mime; | ||
| using Workleap.Extensions.OpenAPI.TypedResult; | ||
|
|
||
| namespace WebApi.OpenAPI.SystemTest.Ordering; | ||
|
|
||
| // Repro case of a bug where a ProducesResponseType on the controller level messes up the ordering of the responses | ||
| [ApiController] | ||
| [Route("ordering")] | ||
| [ProducesResponseType(typeof(IEnumerable<ForbiddenReason>), 403, MediaTypeNames.Application.Json)] | ||
| public class OrderingController : ControllerBase | ||
| { | ||
| [HttpPost("withLotsOfResults")] | ||
| public async Task<Results<Ok<string>, NotFound, BadRequest, InternalServerError<ProblemDetails>>> WithLotsOfResults() | ||
| { | ||
| await Task.CompletedTask; | ||
| return TypedResults.Ok("Result"); | ||
| } | ||
|
|
||
| private sealed record ForbiddenReason(string Reason); | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.