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

[Question]: Ensuring the Correct Order of Operation and Schema Filters in Swashbuckle.AspNetCore #2993

Open
gokayokutucu opened this issue Jul 23, 2024 · 4 comments
Labels

Comments

@gokayokutucu
Copy link

What are you wanting to achieve?

I want to provide the correct execution order of OperationFilter and SchemaFilter in Swashbuckle.AspNetCore when generating Swagger documents. In particular, I want my OperationFilter to first work to cache certain header names, then I want my SchemaFilter to ignore those cached header names. Despite specifying the order in my configuration, SchemaFilter runs before OperationFilter.

What code or approach do you have so far?

Here is a minimalistic code snippet demonstrating the issue:

public class CacheHeaderNamesOperationFilter : IOperationFilter
{
    public static List<string> CachedHeaderNames = new();
    
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        // Caching header names logic
        CachedHeaderNames.Add("ExampleHeaderName");
    }
}

public class IgnoreCachedHeaderNamesSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        // Logic to ignore cached header names
        foreach (var header in CacheHeaderNamesOperationFilter.CachedHeaderNames)
        {
            schema.Properties.Remove(header);
        }
    }
}

services.AddSwaggerGen(c =>
{
    c.OperationFilter<CacheHeaderNamesOperationFilter>(); // First, cache the header names
    c.SchemaFilter<IgnoreCachedHeaderNamesSchemaFilter>(); // Then, ignore the cached header names
});

Despite this order, IgnoreCachedHeaderNamesSchemaFilter runs before CacheHeaderNamesOperationFilter.

What is the expected outcome?

The expected outcome is that CacheHeaderNamesOperationFilter should run before IgnoreCachedHeaderNamesSchemaFilter, ensuring that the cached header names are available for the schema filter to process and ignore. This way, the Swagger documentation accurately reflects the intended API design.

Additional context

No response

@martincostello
Copy link
Collaborator

Schemas are created as part of the process of enumerating the operations found in the document, which is why different types of filters run in a certain order.

For your specific scenario, to get the behaviour your want you probably need to use a document filter and remove the schemas you wish to ignore from the almost-final document before it is serialized after your operation processors have run.

@gokayokutucu
Copy link
Author

gokayokutucu commented Jul 23, 2024

Thank you, your response. In particular, what steps should I follow to get the correct order and how can I use the document filter correctly?
Also, you said that the different filter types work in a certain order, what exactly is this order?

@martincostello
Copy link
Collaborator

martincostello commented Jul 23, 2024

@gokayokutucu
Copy link
Author

Thank you very much for your response. It really helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants