Skip to content

Enum name is generated twice for nested structures #40

@petrzjunior

Description

@petrzjunior

When generating enums using the EnumsWithValuesFixFilters, enum names get generated twice when used in conjucation with UseAllOfToExtendReferenceSchemas. Once in the enum schema, which is correct, but second time in the property referencing the schema, which is IMHO incorrect. This behavior breaks swagger-typescript-api generator which misinterprets the propetry as enum type due to the x-enumNames field.

Code to reproduce

public enum Color
{
	Green = 1,
	Blue = 2
}

public class Elephant
{
	public Color Color { get; init; }
}

[Route("zoo")]
public class AnimalController(): ControllerBase
{
	[HttpPost("elephant")]
	public IActionResult CreateElephant([FromBody] Elephant elephant)
	{
		return Ok();
	}
}

builder.Services.AddSwaggerGen(options =>
{
	options.UseAllOfToExtendReferenceSchemas();
	options.SupportNonNullableReferenceTypes();
	options.NonNullableReferenceTypesAsRequired();
	options.AddEnumsWithValuesFixFilters();
});

generates

{
  "openapi": "3.0.4",
  "info": {
    "title": "Zoo",
    "version": "1.0"
  },
  "paths": {
    "/zoo/elephant": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Elephant"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
       "Color": {
        "enum": [
          1,
          2
        ],
        "type": "integer",
        "description": "\n\n1 = Green\n\n2 = Blue",
        "format": "int32",
        "x-enumNames": [
          "Green",
          "Blue"
        ]
      },
      "Elephant": {
        "required": [
          "color"
        ],
        "type": "object",
        "properties": {
          "color": {
            "allOf": [
              {
                "$ref": "#/components/schemas/Color"
              }
            ],
            "description": "\n\n1 = Green\n\n2 = Blue",
            "x-enumNames": [
              "Green",
              "Blue"
            ]
          }
        },
        "additionalProperties": false
      }
    }
  }
}

Expected behavior

I expect the filter to not generate the extra x-enumNames in the outer property.

    "color": {
      "allOf": [
        {
          "$ref": "#/components/schemas/Color"
        }
      ],
      "description": "\n\n1 = Green\n\n2 = Blue",
-     "x-enumNames": [
-       "Green",
-       "Blue"
-     ]
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions