Skip to content

Polymorphic derived types do not have discriminator property marked as required in OpenAPI schema. #60469

@andrewimcclement

Description

@andrewimcclement

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

[JsonDerivedType(typeof(Bar), "bar")]
[JsonDerivedType(typeof(Baz), "baz")]
public abstract record Foo;
public record Bar(int Value) : Foo;
public record Baz(string Name) : Foo;
Generated schema
  "components": {
    "schemas": {
      "Foo": {
        "required": [
          "$type"
        ],
        "type": "object",
        "anyOf": [
          {
            "$ref": "#/components/schemas/FooBar"
          },
          {
            "$ref": "#/components/schemas/FooBaz"
          }
        ],
        "discriminator": {
          "propertyName": "$type",
          "mapping": {
            "bar": "#/components/schemas/FooBar",
            "baz": "#/components/schemas/FooBaz"
          }
        }
      },
      "FooBar": {
        "required": [
          "value"
        ],
        "properties": {
          "$type": {
            "enum": [
              "bar"
            ],
            "type": "string"
          },
          "value": {
            "type": "integer",
            "format": "int32"
          }
        }
      },
      "FooBaz": {
        "required": [
          "name"
        ],
        "properties": {
          "$type": {
            "enum": [
              "baz"
            ],
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }

In the schema, FooBar and FooBaz have required properties, but critically the discriminator property $type is not required,
even though it is required in the base class Foo. This is inconsistent and can cause issues when e.g.
trying to generate TypeScript classes from the Open API schema.

Expected Behavior

Expected generated schema
  "components": {
    "schemas": {
      "Foo": {
        "required": [
          "$type"
        ],
        "type": "object",
        "anyOf": [
          {
            "$ref": "#/components/schemas/FooBar"
          },
          {
            "$ref": "#/components/schemas/FooBaz"
          }
        ],
        "discriminator": {
          "propertyName": "$type",
          "mapping": {
            "bar": "#/components/schemas/FooBar",
            "baz": "#/components/schemas/FooBaz"
          }
        }
      },
      "FooBar": {
        "required": [
          "$type",
          "value"
        ],
        "properties": {
          "$type": {
            "enum": [
              "bar"
            ],
            "type": "string"
          },
          "value": {
            "type": "integer",
            "format": "int32"
          }
        }
      },
      "FooBaz": {
        "required": [
          "$type",
          "name"
        ],
        "properties": {
          "$type": {
            "enum": [
              "baz"
            ],
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }

Steps To Reproduce

An example reproducing this issue can be found at https://github.com/andrewimcclement/openApiDiscriminatorIssue.

Exceptions (if any)

No response

.NET Version

9.0.2

Anything else?

ASP.NET Core version: 9.0.2.

I do not believe this is the same as #57982

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions