RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.82k stars 1.3k forks source link

NSwag WebAPI generation generates double OneOf schema #3149

Open Havunen opened 4 years ago

Havunen commented 4 years ago

Having code:

public enum MyEnum
{
    Foo = 1,
    Bar = 20
}

private class TestModel
{
    public string foo { get; set; }
}

public class NullableSchemaTestController : ApiController
{

    [ResponseType(typeof(List<TestModel>))]
    public IHttpActionResult GetEventLogRows(
        [ModelBinder(typeof(EnumModelBinder))] MyEnum? integrationName = MyEnum.Foo
    )
    {
        return null;
    }
}

Usage:

var generator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings()
{
    SchemaType = SchemaType.OpenApi3,
    AllowNullableBodyParameters = false,
    FlattenInheritanceHierarchy = true,
    AllowReferencesWithProperties = false,
    IsAspNetCore = false
});
var document = await generator.GenerateForControllerAsync<NullableSchemaTestController>();

NSwag currently generates following output:

{
    "openapi": "3.0.0",
    "info": {
        "title": "My Title",
        "version": "1.0.0"
    },
    "paths": {
        "/api/EnumOneOfEscape": {
            "get": {
                "tags": [
                    "EnumOneOfEscape"
                ],
                "operationId": "EnumOneOfEscape_GetEventLogRows",
                "parameters": [{
                        "name": "integrationName",
                        "in": "query",
                        "schema": {
                            "default": 1,
                            "oneOf": [{
                                    "nullable": true,
                                    "oneOf": [{
                                            "$ref": "#/components/schemas/MyEnum"
                                        }
                                    ]
                                }
                            ]
                        },
                        "x-position": 1
                    }
                ],
                "responses": {
                    "200": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/TestModel"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "TestModel": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "foo": {
                        "type": "string",
                        "nullable": true
                    }
                }
            },
            "MyEnum": {
                "type": "integer",
                "description": "",
                "x-enumNames": [
                    "Foo",
                    "Bar"
                ],
                "enum": [
                    1,
                    20
                ]
            }
        }
    }
}

OneOf - OneOf syntax confuses Swagger client generator and it does not produce working code.

Expected result: Enum parameter should be like this:

{
"name": "integrationName",
"in": "query",
"schema": {
  "default": 1,
  "nullable": true,    
  "oneOf": [
    {
      "$ref": "#/components/schemas/MyEnum"
    }
  ]
  ]
},
"x-position": 1
}
Havunen commented 4 years ago

Other of the OneOf wrappers comes from here: https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Generation.WebApi/Processors/OperationParameterProcessor.cs#L498-L505

and other from here: https://github.com/Havunen/NSwag/blob/master/src/NSwag.Generation/OpenApiDocumentGenerator.cs#L127

@RicoSuter can something be done for this please, code generators dont work :(

Havunen commented 4 years ago

After investigating this further OpenApi 3 specification does not mention any OneOf references to be used with enums

https://swagger.io/docs/specification/data-models/enums/

efie45 commented 1 year ago

@RicoSuter Any updates on this? I am running into this issue as well, specifically for nullable enums or enums with default values being passed via query parameter. Why does the enum need to get wrapped in this way?