-
Notifications
You must be signed in to change notification settings - Fork 2.8k
APIs: Use EndpointMetadata to check for MapToApiAttribute at runtime to include Controller in Swagger document #20144
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
base: main
Are you sure you want to change the base?
Conversation
Hi there @mdubbelv, 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 🤖 🙂 |
src/Umbraco.Cms.Api.Common/Extensions/ActionDescriptorApiCommonExtensions.cs
Outdated
Show resolved
Hide resolved
@@ -11,22 +11,11 @@ | |||
|
|||
namespace Umbraco.Cms.Api.Common.Configuration; | |||
|
|||
public class ConfigureUmbracoSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions> | |||
public class ConfigureUmbracoSwaggerGenOptions( |
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.
looking at other files in the source code, the pattern appears to not be using primary constructors without private backing fields. so maybe revert this change to again use private backing fields instead to adhere to the style guide set
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.
Might be time to migrate over to primary constructors while editing a class. Maybe we'll let a main contributor have his/her/their say.
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.
Yes, I think let's revert this back please - just so we are consistent throughout the code base and don't present something that might be a bit surprising still to a contributor.
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.
Thanks for raising this issue @mdubbelv-consid and providing a solution. I've tested out, and it looks to work as expected.
Let me just relay my testing steps so you or anyone else looking at this can verify they are valid.
- Created a new, standalone management API endpoint:
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Common.Attributes;
using Umbraco.Cms.Api.Common.Filters;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.DependencyInjection;
using Umbraco.Cms.Api.Management.Filters;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Api.Management.ViewModels.Culture;
using Umbraco.Cms.Core;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Cms.Web.Common.Filters;
namespace Umbraco.Cms.Api.Management.Controllers.Culture;
[ApiVersion("1.0")]
[VersionedApiBackOfficeRoute("andy-testing")]
[ApiExplorerSettings(GroupName = "Andy Testing")]
[ApiController]
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[MapToApi(ManagementApiConfiguration.ApiName)]
[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
[AppendEventMessages]
[DisableBrowserCache]
public class TestController : Controller
{
[HttpGet]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<CultureReponseModel>), StatusCodes.Status200OK)]
public Task<string> Get(CancellationToken cancellationToken)
{
return Task.FromResult("Hello");
}
}
-
Verified that it appeared on the Swagger UI at:
/umbraco/swagger/index.html?urls.primaryName=Umbraco+Management+API
-
Commented out
[MapToApi(ManagementApiConfiguration.ApiName)]
and verified that it no longer appeared. -
Add the following class to the solution:
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Umbraco.Cms.Api.Common.Attributes;
using Umbraco.Cms.Api.Management.Controllers.Culture;
namespace Umbraco.Cms.Api.Management;
public sealed class MapToApiConvention(string apiName) : IApplicationModelConvention
{
public void Apply(ApplicationModel app)
{
SetMapToApiName(app, apiName);
}
private static void SetMapToApiName(ApplicationModel app, string apiName)
{
foreach (var controller in app.Controllers)
{
if (!typeof(TestController).IsAssignableFrom(controller.ControllerType))
continue;
if (controller.Attributes.OfType<MapToApiAttribute>().Any())
continue;
var attribute = new MapToApiAttribute(apiName);
foreach (var action in controller.Actions)
{
foreach (var selector in action.Selectors)
{
selector.EndpointMetadata.Add(attribute);
}
}
}
}
}
- Registered it in my start-up project with:
public void Compose(IUmbracoBuilder builder) => builder.Services
.AddMvc(options =>
{
options.Conventions.Add(new MapToApiConvention("management"));
});
- Verified that with the current code in
main
the endpoint still does not appear on the Swagger UI documentation. - Switched to the code provided by this PR, rebuilt, and now the endpoint does appear on the Swagger UI documentation.
So I think this is all good to go once the raised comments are resolved.
I'll also ask - but not insist as I don't know how difficult it would be - but wonder if it's possible to add some form of automated testing for this? I.e. verify that ConfigureUmbracoSwaggerGenOptions
will now correctly handle both compile and runtime attributes. That would be nice as a check against future regressions and also help document the behaviour.
@@ -11,22 +11,11 @@ | |||
|
|||
namespace Umbraco.Cms.Api.Common.Configuration; | |||
|
|||
public class ConfigureUmbracoSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions> | |||
public class ConfigureUmbracoSwaggerGenOptions( |
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.
Yes, I think let's revert this back please - just so we are consistent throughout the code base and don't present something that might be a bit surprising still to a contributor.
|
||
namespace Umbraco.Extensions; | ||
|
||
public static class ActionDescriptorApiCommonExtensions |
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.
Could you add some XML header comments on this public class and the public methods within please? Just so it's clear for anyone coming across this in future what's it's doing and what problem it's solving.
Prerequisites
This PR fixes this issue:
#20143
It adds the possibility to add Controllers to a Swagger document at runtime. For example by using a IApplicationModelConvention
That is achieved by not checking MethodInfo and instead checking ActionDescriptor.EndpointMetaData when trying to find a MapToApiAttribute on a Controller when filtering out documents using SwaggerGenOptions.DocInclusionPredicate.
Description
Test by:
See more: https://docs.umbraco.com/umbraco-cms/reference/api-versioning-and-openapi#adding-your-own-swagger-documents