RickStrahl / Westwind.Globalization

Database driven resource localization for .NET applications
543 stars 135 forks source link

Swagger 2.0 gives error due to missing HttpMethod Binding #176

Open aathar opened 5 years ago

aathar commented 5 years ago

Been using swagger on a project but as soon as I installed "WestWind.Globalization.AspNetCore" package, the swagger crashed and started giving this error. " WestWind.Globalization.AspNetCore.Controllers.JavaScriptLocalizationResourcesController.JavaScriptLocalizationResources (Westwind.Globalization.AspnetCore). Actions require an explicit HttpMethod binding for Swagger 2.0 "

I had to write a custom method to ignore the controllers "LocalizationAdministration" and "JavaScriptLocalizationResources" until we fix this issue.

Here is a potential solution https://stackoverflow.com/a/47829194

Please have a look.

Cnogz commented 5 years ago

Below method can help to resolve conflict for the time being.

public class IgnoreContollerAmbiguity : IActionModelConvention { public void Apply(ActionModel action) { var controllerName = action.Controller.ControllerName; if (controllerName.Equals("LocalizationAdministration") || controllerName.Equals("JavaScriptLocalizationResources")) { action.ApiExplorer.IsVisible = false; }

    }
}

And Startup Implementation:

services.AddMvc(c=>c.Conventions.Add(new IgnoreContollerAmbiguity()));

nick-gaudreau-cwb commented 5 years ago

@aathar Thx!

vinod-vetrivel commented 4 years ago

Similar issue in swagger 3.0 :

Ambiguous HTTP method for action - WestWind.Globalization.AspNetCore.Controllers.JavaScriptLocalizationResourcesController.JavaScriptLocalizationResources (Westwind.Globalization.AspnetCore). 
Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

The above workaround provided by @Cnogz can be changed like below to use with swagger 3.0

public class IgnoreContollerAmbiguity : IApplicationModelConvention
    {
        public void Apply(ApplicationModel application)
        {
            if (application != null)
            {
                application.Controllers.FirstOrDefault(c => c.ControllerName == "LocalizationAdministration").ApiExplorer.IsVisible = false;
                application.Controllers.FirstOrDefault(c => c.ControllerName == "JavaScriptLocalizationResources").ApiExplorer.IsVisible = false;
            }
        }
    }