-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Different behavior for guid constraint with Swagger in Controllers and Minimal APIs #39886
Comments
There's a couple of things to verify as we investigate this:
|
Thanks for contacting us. We're moving this issue to the |
The type is correctly processed as
Yes, the issue is related to this one, but it happens only with Minimal APIs, Controllers works correctly. |
Took the time to investigate this. It looks like this bug is happening because of the parameter binding logic that is implemented in minimal APIs. Parameter types are try-parseable intro strings are automatically mapped as string types. aspnetcore/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs Lines 291 to 292 in dcbfb82
Similar behavior happens for things like DateTime, TimeSpan, etc. |
@marcominerva Meanwhile it's get delivered in .NET 7 (Hopefully), you can add a filter to your swagger configuration to set the guid parameter as 'uuid'. Guid Parameter Filter: public class GuidParameterFilter : IParameterFilter
{
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
if (parameter.Schema.Type == "string" &&
context.ApiParameterDescription.Type == typeof(Guid))
{
parameter.Schema.Format = "uuid";
}
}
} Program.cs: services.AddSwaggerGen(c =>
{
c.ParameterFilter<GuidParameterFilter>();
} For the other parameter bindings there as @captainsafia mentioned, it should be applied for any other type like DateTime, TimeSpan, etc; even may need to have other considerations and complexity. If you find a better solution please share. |
Thanks for contacting us. We're moving this issue to the |
Hi all, We just posted an announcement about the future of OpenAPI support in ASP.NET Core. TL;DR: we're adding OpenAPI document generation as a first-class feature in ASP.NET Core. I've included the GUID scenario mentioned here into our test bed:
And have validated that for both cases we generate the correct schema:
|
Closing as this is resolved with our new built-in OpenAPI support. |
Is there an existing issue for this?
Describe the bug
There is a different behavior in the
swagger.json
file that is generated in a Controller-based project and in a project that uses Minimal APIs, when there is a guid constraint. In Controller-based projects, theswagger.json
file specifies theuuid
format in theschema
section for the guid parameter. However, noformat
attribute is written in theschema
section when working with Minimal APIs.Expected Behavior
The
swagger.json
file should be the same for both project types.Steps To Reproduce
I'm using the default Swagger configuration that comes with the .NET 6.0 templates. Using a Controller like this:
The generated
swagger.json
files specifies theuuid
format for the id parameter:If, however, I create a Minimal API:
Then the
swagger.json
contains the following description:As you can see, no
format
attribute is written in theschema
section.Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
ASP.NET Core version: 6.0
IDE: Visual Studio 2022 17.0.5
The text was updated successfully, but these errors were encountered: