0

I have ASP.NET Core application where I can extend swagger enum using,

public class MyParameterFilter : IParameterFilter
{
    /// <inheritdoc />
    public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
    {
        var routeInfo = context.ApiParameterDescription.RouteInfo;
        if (routeInfo?.Constraints != null && routeInfo.Constraints.Any(c => c is MyConstraint))
        {
            parameter.Schema.Enum = Myvalues.Select(p => new OpenApiString(p)).ToList<IOpenApiAny>();
        }
    }
}

Now I wanna do the same in my classic ASP.NET Web Api project where I see DocumentFilter, OperationFilter and SchemaFilter but no ParameterFilter. I mean I couldn't found IParameterFilter

1 Answer 1

0

Used document filter,

public class MyDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        var pathItems = swaggerDoc.paths.Values;
        
        var deletes = pathItems.Select(pathItem => pathItem.delete).Where(o => o?.parameters != null);
        var gets = pathItems.Select(pathItem => pathItem.get).Where(o => o?.parameters != null);
        var heads = pathItems.Select(pathItem => pathItem.head).Where(o => o?.parameters != null);
        var patches = pathItems.Select(pathItem => pathItem.patch).Where(o => o?.parameters != null);
        var puts = pathItems.Select(pathItem => pathItem.put).Where(o => o?.parameters != null);
        var posts = pathItems.Select(pathItem => pathItem.post).Where(o => o?.parameters != null);
        var options = pathItems.Select(pathItem => pathItem.options).Where(o => o?.parameters != null);
        
        var allOperations = deletes.Concat(gets)
                                   .Concat(heads)
                                   .Concat(patches)
                                   .Concat(puts)
                                   .Concat(posts)
                                   .Concat(options)
                                   .ToList();

        foreach (Operation operation in allOperations.Where(o => o.parameters.Any(p => p.name == "propName")))
        {
            operation.parameters.First(p => p.name == "propName").@enum = MyValues.Select(p => (object)p.Name).ToList();
            var successResponse = operation.responses.First(d => d.Key == "200").Value;
            if (successResponse == null)
            {
                return;
            }
            successResponse.examples = MyValues.ToDictionary(d => d.Name, d => "string");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.