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

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

Open
1 task done
andrewimcclement opened this issue Feb 18, 2025 · 0 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@andrewimcclement
Copy link

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 18, 2025
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

2 participants