13

I have tried to implement both ( swagger and odata ) in asp.net core, but it's not working.

I'm unable to integrate the route given for odata.

I have the following Configuration and I receive a generic error.

Configuration

This is the error

this is the error

Jinto Jacob
  • 385
  • 2
  • 17
Ayushi Sharma
  • 155
  • 1
  • 1
  • 4
  • 1
    Please put the errors inline in the message easier to read. As for the question: You could try using `.MapWhen` to register a conditional middleware. Sorry, can't write a complex answer now, got to run – Tseng Jun 20 '18 at 06:11
  • Try NSwag. At least it does not choke on odata. https://github.com/RSuter/NSwag/ – Will Tartak Nov 01 '18 at 19:13

3 Answers3

20

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();
        });
    }
}
Kizmar
  • 2,465
  • 3
  • 20
  • 27
6

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

nycdanielp
  • 187
  • 1
  • 2
4

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.

Chris Martinez
  • 3,185
  • 12
  • 28