Skip to content
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

Parameter filters not invoked in Minimal API when using WithOpenApi extension method #45307

Closed
1 task done
marcominerva opened this issue Nov 28, 2022 · 3 comments
Closed
1 task done
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Comments

@marcominerva
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm using .NET 7.0. Following the workaround suggested here: #39886 (comment), I have created the following ParameterFilter to correcly handle Guid constraint:

builder.Services.AddSwaggerGen(options =>
{
    options.ParameterFilter<GuidParameterFilter>();
});

// ...

app.MapGet("api/minimalsample", (Guid id) => TypedResults.NoContent());

// ...

public class GuidParameterFilter : IParameterFilter
{
    public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
    {
        if (context.PropertyInfo is null && parameter.Schema.Type == "string"
            && (context.ApiParameterDescription.Type == typeof(Guid) || context.ApiParameterDescription.Type == typeof(Guid?)))
        {
            parameter.Schema.Format = "uuid";
        }
    }
}

This solves the issue and produce the correct schema for Guid parameter. If, however, I add a call to WithOpenApi extension method after the MapGet, the ParameterFilter is no longer invoked:

app.MapGet("api/minimalsample", (Guid id) => TypedResults.NoContent())
.WithOpenApi();    // This prevent the ParameterFilter to be invoked.

Expected Behavior

Parameter filters should be invoked even when using the WithOpenApi extension method.

Steps To Reproduce

Minimal repro here: https://github.com/marcominerva/ParameterFilterIssue

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

Visual Studio 2022 17.4.1

.NET SDK:
Version: 7.0.100
Commit: e12b7af219

Ambiente di runtime:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100\

Host:
Version: 7.0.0
Architecture: x64
Commit: d099f075e4

.NET SDKs installed:
7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

@javiercn javiercn added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 28, 2022
@captainsafia
Copy link
Member

So, the WithOpenApi extension method works by generating an OpenAPI operation and storing it in metadata. Swashbuckle will favor that OpenApiOperation over the one it generates by default (see here) but do some merging to generate the schemas itself. The ParameterFilters do not get invoked at this level. @marcominerva You might be able to leverage an OperationFilter for this and add another layer of nesting to your invocations.

Resolving domaindrivendev/Swashbuckle.AspNetCore#2521 should help with this in the future.

@marcominerva
Copy link
Contributor Author

Thank you @captainsafia, using an OperationFilter is a good workaround.

@captainsafia
Copy link
Member

captainsafia commented Dec 1, 2022

@marcominerva Great! I'll file an issue on Swashbuckle to see what it would take to make sure that parameter filters are applied for these types of operations as well. I believe that RequestBodyFilters might also suffer from this issue given my understanding of Swashbuckle's filtering implementation.

See domaindrivendev/Swashbuckle.AspNetCore#2561.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

No branches or pull requests

3 participants