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

Setting ApiDescription.RelativePath in a OperationFilter does not work properly #361

Closed
dmunozgarrido opened this issue May 27, 2015 · 1 comment

Comments

@dmunozgarrido
Copy link

I need to set a custom relative path for my action controller, for that purpose I created a new attribute and use it in the action controller

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public sealed class SwaggerRouteAttribute : Attribute
    {
        public SwaggerRouteAttribute(string route)
        {
            this.Route = route;
        }

        public string Route { get; internal set; }
    }

Then in an OperationFilter I get that custom attribute for apiDescription.ActionDescription and modify the RelativePath property with the value set in the action controller.

public void Apply(Operation operation, SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
{
    var swaggerRouteAttribute = apiDescription.ActionDescriptor.GetCustomAttributes<SwaggerRouteAttribute>().FirstOrDefault();
    if (swaggerRouteAttribute != null)
    {
        apiDescription.RelativePath = swaggerRouteAttribute.Route;
    }
}

The issue is at the first load of Swagger documentation (no matter if it is the json doc or the ui) because, despite the RelativePath property is being set properly, it does not change until the second load and following ones.

I am not totally sure if it is a bug or I am making something wrong or even If there is any workaround to fix this issue.

@domaindrivendev
Copy link
Owner

The purpose of an IOperationFilter is to modify the provided "Operation" not the ApiDescription. The corresponding ApiDescription is just passed for additional context. A typical implementation would modify the operation according to information inferred from the provided ApiDescription. Modifying the ApiDescription will have no impact on the generated Swagger.

If you really need to modify the paths (although I'm not quite sure why this would be necessary), then you'll need to create an IDocumentFilter and adjust SwaggerDocument.paths accordinly.

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

No branches or pull requests

2 participants