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

[.NET Core 3.1] Hot to configure OData with Swagger? #2024

Closed
MDzyga opened this issue Jan 17, 2020 · 5 comments
Closed

[.NET Core 3.1] Hot to configure OData with Swagger? #2024

MDzyga opened this issue Jan 17, 2020 · 5 comments

Comments

@MDzyga
Copy link

MDzyga commented Jan 17, 2020

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 [.NET Core 3.1] How to hide OData endpoints in new version of Shwashbuckle (vr 5.0.0-rc5)? domaindrivendev/Swashbuckle.AspNetCore#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)])

@Mhirji
Copy link

Mhirji commented Jan 17, 2020

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"));
}
});

@MDzyga
Copy link
Author

MDzyga commented Jan 17, 2020

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 :)

@MDzyga MDzyga closed this as completed Jan 17, 2020
@KishorNaik
Copy link

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

@Pzixel
Copy link

Pzixel commented Nov 13, 2020

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.

@VarunSaiTeja
Copy link

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

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

5 participants