John Doe
John Doe

Reputation: 13

Swashbuckle won't generate schema for Enum

I have the following structure //controller GetProducts(string id, [FromQuery] Product)

//Product.cs class contains an enum member

Product 
{
EnumA ExampleA
}
`//startup.cs
    services.AddSwaggerGen(c =>
    {
    c.UseAllOfToExtendReferenceSchemas();
    }`

When trying to inspect the API for getting products using swagger ui, I get a resolver issue for the EnumA, because the in the generated json file there's not definition in components/schemas for the EnumA even though it is referenced in the paths section of the json. Something like this

`//paths section of the json
{
    "name": "ExampleA",
    "in": "query",
    "schema": {
    "allOf": [
    {
     "$ref": "#/components/schemas/EnumA"
     }
    ]}
}`

I tried a few things,

  1. removing c.UseAllOfToExtendReferenceSchemas(); will actually fix this behaviour but, removing this options is not something that suits my needs
  2. changingthe annotation of the parameter from [FromQuery] to -> [FromBody] will also fix this behaviour and have the component/schema for this Enum be generated, but also this options is something that doesn't suit my need
  3. creating a DocumentFilter and trying to manually add this enum for the schema definitions using something like swaggerDoc.Components.Schemas.Add("EnumA") .... will throw and error because swashbuckle has this Enum referenced at runtime but somehow it's not outputed in the final json file

Tips? (Swashbuckle 5.6.3)

Upvotes: 0

Views: 888

Answers (1)

jepozdemir
jepozdemir

Reputation: 509

If you're using Newtonsoft, you also need this package:

Swashbuckle.AspNetCore.Newtonsoft

And in startup:

services.AddSwaggerGenNewtonsoftSupport(); // needs to be placed after AddSwaggerGen()

You may follow instructions below: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#systemtextjson-stj-vs-newtonsoft

Upvotes: 0

Related Questions