-
Notifications
You must be signed in to change notification settings - Fork 10.3k
ArgumentException with polymorphism in generating OpenAPI schema #57285
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
Comments
...or is this coming in preview 7? (#54598) |
I can repro this with .NET SDK
|
Looking again at your object definitions, I don't think this is valid: [JsonPolymorphic(TypeDiscriminatorPropertyName = "animalType")]
[JsonDerivedType(typeof(Cat), "cat")]
[JsonDerivedType(typeof(Dog), "dog")]
abstract class Animal
{
[Microsoft.OpenApi.Attributes.Display("Animal type")]
[Required]
public string AnimalType { get; set; }
} System.Text.Json needs the discriminator to not be a serializable property because it's "reserved" (I've run into this myself, and have code adjusted accordingly from finding that while debugging). I can't at-a-glance see where in the documentation that's explicitly called out. If you make this edit: [JsonPolymorphic(TypeDiscriminatorPropertyName = "animalType")]
[JsonDerivedType(typeof(Cat), "cat")]
[JsonDerivedType(typeof(Dog), "dog")]
abstract class Animal
{
[Microsoft.OpenApi.Attributes.Display("Animal type")]
[Required]
+ [JsonIgnore]
public string AnimalType { get; set; }
} the exception changes to this:
If you make this edit, it renders the document: [JsonPolymorphic(TypeDiscriminatorPropertyName = "animalType")]
[JsonDerivedType(typeof(Cat), "cat")]
[JsonDerivedType(typeof(Dog), "dog")]
abstract class Animal
{
[Microsoft.OpenApi.Attributes.Display("Animal type")]
- [Required]
+ [JsonIgnore]
public string AnimalType { get; set; }
} /openapi/v1.json{
"openapi": "3.0.1",
"info": {
"title": "Animals API",
"version": "v1"
},
"servers": [
{
"url": "https://localhost:50001"
},
{
"url": "http://localhost:50000"
}
],
"paths": {
"/api/pets": {
"get": {
"tags": [
"TodoApp"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"required": [
"animalType"
],
"type": "object",
"anyOf": [
{
"$ref": "#/components/schemas/AnimalCat"
},
{
"$ref": "#/components/schemas/AnimalDog"
}
],
"discriminator": {
"propertyName": "animalType",
"mapping": {
"cat": "#/components/schemas/AnimalCat",
"dog": "#/components/schemas/AnimalDog"
}
}
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"AnimalCat": {
"properties": {
"animalType": {
"enum": [
"cat"
],
"type": "string"
}
}
},
"AnimalDog": {
"properties": {
"animalType": {
"enum": [
"dog"
],
"type": "string"
}
}
}
}
},
"tags": [
{
"name": "TodoApp"
}
]
} That's not to say there isn't an issue here, I think maybe the schema and serializer behaviours are conflicting with each other. |
@maartenba Thanks for reporting this issue! It looks like you're running into the same bug as #56575. I've had a really hard-time reproing this bug based on the details in the referenced issue. All my debugging would indicate that the issue is from the underlying I'll take a look at your repro and see if it reveals something about the root cause that the first bug report on this didn't reveal. In the meantime, @martincostello's workaround should help here... |
In the meantime, I'm going to go ahead and close this as a dupe in favor of keeping the conversation in the older issue. |
Is there an existing issue for this?
Describe the bug
In 9.0.0-preview.6.24328.4, when creating OpenAPI schema that has a model with polymorphism, an exception is thrown.
Expected Behavior
Polymorphism works, and a valid OpenAPI schema is created.
Steps To Reproduce
Repro solution is attached: repro.zip
Alternatively, use this code:
Next, request the
/openapi/v1.json
endpoint.Exceptions (if any)
.NET Version
9.0.0-preview.6.24328.4 (.NET 9 preview 6)
Anything else?
No response
The text was updated successfully, but these errors were encountered: