Skip to content

DefaultOperationIdSelector should be updated to fallback to EndpointName if AttributeRouteInfo isn't populated #2165

@DamianEdwards

Description

@DamianEdwards

The new minimal API feature in ASP.NET Core 6 doesn't populate the ApiDescription.ActionDescriptor.AttributeRouteInfo property which is used by Swashbuckle by default to set the operation ID. Minimal APIs can have an endpoint name set via endpoint metadata however so if Swashbuckle's default behavior were updated to fallback to using the EndpointNameMetadata in ApiDescription.ActionDescriptor.EndpointMetadata then it would be simple for operation IDs to be configured on minimal APIs without having to configure Swashbuckle manually.

e.g. change in src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs

namespace Swashbuckle.AspNetCore.SwaggerGen
{
    public class SwaggerGeneratorOptions
    {
        ...
        private string DefaultOperationIdSelector(ApiDescription apiDescription)
        {
            return
                apiDescription.ActionDescriptor.AttributeRouteInfo?.Name
                ?? (apiDescription.ActionDescriptor.EndpointMetadata
                        .FirstOrDefault(m => m is EndpointNameMetadata) as EndpointNameMetadata)?.EndpointName;
        }
    }
}

Example minimal API:

app.MapGet("/", () => "Hello World")
   .WithMetadata(new EndpointNameMetadata("HelloWorldApi"));

It might be worth considering including RouteNameMetadata as a value to look for too, rather than relying solely on AttributeRouteInfo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions