Webapi: [.NET Core 3.1] Hot to configure OData with Swagger?

Created on 17 Jan 2020  路  5Comments  路  Source: OData/WebApi

Hi,
I would like to configure Swagger (Swashbuckle vr 5.0.0) in my project (.NET Core 3.1) with OData (vr 7.3). Currently I get an exception:

{"message":"No media types found in 'Microsoft.AspNet.OData.Formatter.ODataOutputFormatter.SupportedMediaTypes'. Add at least one media type to the list of supported media types. "}

Assemblies affected

Microsoft.AspNetCore.OData 7.3.0
Swashbuckle.AspNetCore 5.0.0
.NET Core 3.1

Reproduce steps

1) Create project with .NET Core 3.1
2) Configure Swashbuckle (my configuration it's here https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1442)
3) Create ODATA endpoint & standard endpoint
4) Run application

Expected result

You will see at least standard endpoint.

Actual result

You will see Swagger page with information 'Failed to load API Definition'

Additional detail

In my opinion API/Swagger shouldn't generate OData endpoints because they are marked as ignored ([ApiExplorerSettings(IgnoreApi = true)])

Most helpful comment

Ok, the solution is:

services.AddMvcCore(options =>
{
   foreach (var outputFormatter in options.OutputFormatters.OfType<OutputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
   {
          outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
    }

   foreach (var inputFormatter in options.InputFormatters.OfType<InputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
   {
         inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
   }
});

Thanks Mhirji :)

All 5 comments

We managed to get this working by adding this:

services.AddMvcCore(options =>
{
foreach (var outputFormatter in options.OutputFormatters.OfType().Where(_ => _.SupportedMediaTypes.Count == 0))
{
outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
foreach (var inputFormatter in options.InputFormatters.OfType().Where(_ => _.SupportedMediaTypes.Count == 0))
{
inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
});

Ok, the solution is:

services.AddMvcCore(options =>
{
   foreach (var outputFormatter in options.OutputFormatters.OfType<OutputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
   {
          outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
    }

   foreach (var inputFormatter in options.InputFormatters.OfType<InputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
   {
         inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
   }
});

Thanks Mhirji :)

I have created a service extension for the same.
Please check following link.
https://github.com/KishorNaik/Sol_OData_Swagger_Support

Works to me. Important note: formatters must be fixed AFTER odata is registered. This is why my original code AddMvcCore(...).AddOData() didn't work. Be careful.

services.AddMvcCore(options =>
{
foreach (var outputFormatter in options.OutputFormatters.OfType().Where(x => x.SupportedMediaTypes.Count == 0))
{
outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}

foreach (var inputFormatter in options.InputFormatters.OfType().Where(x => x.SupportedMediaTypes.Count == 0))
{
inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
});

It works

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khodaie picture khodaie  路  4Comments

suadev picture suadev  路  3Comments

davidmorissette picture davidmorissette  路  3Comments

TehWardy picture TehWardy  路  5Comments

joelmeaders picture joelmeaders  路  4Comments