I use the Swashbuckle.AspNetCore @ v6.5.0 version to generate a OAS file. while I find that the file generate is not satisfy the OAS spec. Here is my c# code.
/// <summary>
/// Inheritance and Polymorphism
/// </summary>
/// <param name="pet">pet</param>
/// <returns></returns>
[HttpPost("Polymorphism", Name = "Polymorphism")]
[Consumes("application/json")]
public void GetSample([FromBody] Pet pet)
{
return;
}
public class Dog : Pet
{
public bool Bark { get; set; }
}
public class Cat : Pet
{
public int Age { get; set; }
}
public class Dog : Pet
{
public bool Bark { get; set; }
}
While I got an api file like
{
"openapi": "3.0.1",
"info": {
"title": "NswagTest",
"version": "1.0"
},
"paths": {
"/TestControllerNameTags/Polymorphism": {
"post": {
"tags": [
"TestControllerNameTags"
],
"summary": "Inheritance and Polymorphism",
"operationId": "Polymorphism",
"requestBody": {
"description": "pet",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"$ref": "#/components/schemas/Cat"
},
{
"$ref": "#/components/schemas/Dog"
}
],
"description": "Pet"
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
}
},
"components": {
"schemas": {
"Cat": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Pet"
}
],
"properties": {
"age": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
},
"Dog": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Pet"
}
],
"properties": {
"bark": {
"type": "boolean"
}
},
"additionalProperties": false
},
"Pet": {
"required": [
"petType"
],
"type": "object",
"properties": {
"petType": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false,
"description": "Pet",
"discriminator": {
"propertyName": "petType",
"mapping": {
"Cat": "#/components/schemas/Cat",
"Dog": "#/components/schemas/Dog"
}
}
}
}
}
}
2 parts I think that not satisfy the OAS spec
The 1st is in the components->schemas->Cat, the type and properties should be an item of "allof" The 2nd is the discriminator shoudl be the same level of "oneof" rather than in the base class definition. like: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
But a very strange issue that confuse me is the I can also use the Nswag to genereate a ts client correctly. So I don't know is there any misunderstanding for me about the OSA spec?
I searched in the github found that in some version of the Swashbuckle.AspNetCore, the 1 and 2 are satified. but in the latest version(v6.5.0), both 1 and 2 are not satisfied.