-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat: Swagger improvements (Retargeted) #15383
feat: Swagger improvements (Retargeted) #15383
Conversation
Hi there @nikcio, thank you for this contribution! 👍 While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:
Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution. If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request. Thanks, from your friendly Umbraco GitHub bot 🤖 🙂 |
Thanks for the PR @nikcio, someone on the core collabs team will review this soon! 😸 |
Hey @georgebid and @JasonElkin I think this PR fell through the cracks. Do you want me to resync the PR or change the target again. |
Ah yes, sorry about that @nikcio, if you could re-target this at /contrib - that would be very handy! Thanks 😸 |
src/Umbraco.Cms.Api.Common/Configuration/ConfigureUmbracoSwaggerGenOptions.cs
Outdated
Show resolved
Hide resolved
@georgebid The PR is now updated again :) |
Hey @georgebid anything else you want be to do with this PR? |
Hi @nikcio 👋 Good stuff! I'll give it a spin next week 👍 |
Hi @nikcio, Thanks for all this 😄 you're spot on - Minimal APIs clearly don't play nice with OpenAPI in Umbraco right now. Just for clarification: I think there might be a slight confusion here as to how this is all supposed to work. I've created an alternative PR in #17622 to support minimal APIs. Could you have a look at that? |
Closed in favour of #17622 |
Retarget of #15346
Prerequisites
If there's an existing issue for this PR then this fixes
Description
This PR improves the
DocInclusionPredicate
for Swagger and adds needed fixes to support minimal APIs in the Swagger schemas.Notice: Because this needs to test the minimal APIs it's targeted to
v13/devv14/devThis PR includes the following
Updated the
DocInclusionPredicate
The current solution to the
DocInclusionPredicate
for Swagger dictates that the endpoints are created with the conventional controller class setup and not the minimal APIs approach. Furthermore, it forces you to use the customMapToApiAttribute
to map an endpoint to anything other than the default schema.TLDR the old logic looked something like this:
MapToApiAttribue
if not = Default schemaI've tried to update the logic so that it's a bit more intuitive:
New logic:
MapToApiAttribute
this takes precedence and will determine if it's includedThis way no APIs "go missing" from the Swagger interface*
This also ensures that the
MapToApiAttribute
is opt-in instead of the single source of truth. Therefore, you can set up your endpoints like in any other Asp Net Core application (By group names).Added
builder.Services.AddEndpointsApiExplorer();
To add minimal APIs to the API Explorer you need this call.
Refactored two of the methods in the
src/Umbraco.Cms.Api.Common/Extensions/MethodInfoApiCommonExtensions.cs
Here you had two extension methods that did some casting and hidden logic that wasn't described by the extension name. To simplify this I've added
GetMapToApiAttribute
which is a helper for getting theMapToApiAttribute
if there is one.Unexpected errors using Minimal APIs
When I added the minimal APIs I noticed that a tag of
null
was added when no group name was added to the API. Therefore I've expanded the logic ofTagActionsBy
to check if the value is null.In the
ActionOrderBy
method I noticed that the method threw an exception due to missing data so I've added checks that all values that should be added is only added if the value isn't null.To test
To test this you can create normal endpoints with controllers like this:
Add the Swagger options in the
Program.cs
like so:Add minimal APIs in the
Program.cs
like so:Final thoughts for HQ
When looking at this I came to wonder why the
ActionOrderBy
customization is even needed and why it includes the template for an endpointapiDescription.ActionDescriptor.AttributeRouteInfo?.Template
. It would be nice if HQ added a comment in the code about what problem it solves because it seems like a weird customization. (And also thought about whether the value should allow spaces as it does now from the group name for example).Also, I have a hard time seeing the benefit of using the custom
MapToApiAttribute
instead of advocating for using the built-in[ApiExplorerSettings(GroupName = "My Group")]
attribute or.WithGroupName("My Group")
extension.