Integrate swashbuckle swagger with odata in ASP.Net Core [closed]

浪尽此生 提交于 2019-11-29 07:00:40

We ran into the same issue when adding OData to our .Net Core project. The workarounds shown in the code snippet on this post fixed our API error(s) when Swagger UI loads.

As far as I can tell, OData isn't supported in Swashbuckle for AspNetCore. So after adding the workaround code in the link above, our Swagger UI works, but none of the OData endpoints show.

Code snippet from the link:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddOData();

        // Workaround: https://github.com/OData/WebApi/issues/1177
        services.AddMvcCore(options =>
        {
            foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
            foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {                
        var builder = new ODataConventionModelBuilder(app.ApplicationServices);

        builder.EntitySet<Product>("Products");

        app.UseMvc(routebuilder => 
        {
            routebuilder.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());

            // Workaround: https://github.com/OData/WebApi/issues/1175
            routes.EnableDependencyInjection();
        });
    }
}

I was able to do this using a DocumentFilter. Create a class like the example below, then add it to your Swagger configuration as:

        services.AddSwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new Info { Title = "Your title API v1.0", Version = "v1.0" });
            options.DocumentFilter<CustomDocumentFilter>();
        });

Github Example

You can integrate Swagger a couple of different ways. For barebones support, you can use the ODataSwaggerConverter provided by OData. This will effectively convert the EDM to a Swagger document. To wire this up to a Swagger generator library like Swashbuckle, you just need create and register a custom generator. The UI and client side of things should remain unchanged. If the generated Swagger document isn't sufficient, the base implementation of the ODataSwaggerConverter is still a reasonable start.

If you're using API Versioning for OData with ASP.NET Core, you need only add the corresponding API Explorer package. Swashuckle will light up with little-to-no additional work on your part. The ASP.NET Core with OData Swagger sample application has an end-to-end working example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!