I am working with an API that involves complex DTOs with polymorphic properties and endpoints that accept polymorphic request bodies (oneOf
).
Here is an example of a C# complex type:
Example c# complex type
namespace Sample {
public class ComplexType
{
public PolymorphicTypeBase PolymorphicProperty { get; set; }
}
public abstract class PolymorphicTypeBase {
public string SomeSharedProperty { get; set; }
}
public class PolymorphicSubtypeA : PolymorphicTypeBase
{
public string PropertyA { get; set; }
}
public class PolymorphicSubtypeB : PolymorphicTypeBase
{
public string PropertyB { get; set; }
}
}
The openAPI specification for these types appears as follows:
...
{
"ComplexType" : {
"type" : "object",
"properties" : {
"polymorphicProperty" : {
"oneOf": [
{ "$ref": "#/components/schemas/PolymorphicTypeA" },
{ "$ref": "#/components/schemas/PolymorphicTypeB" }
],
"nullable": true
}
}
},
"PolymorphicTypeBase": {
"required": ["discriminator"],
"type": "object",
"properties": {
"discriminator": {
"type": "string",
"nullable": true
},
"someSharedProperty": {
"type": "string",
"nullable": true
},
},
"additionalProperties": false,
"discriminator": {
"propertyName": "discriminator",
"mapping": {
"PolymorphicTypeA": "#/components/schemas/PolymorphicTypeA",
"PolymorphicTypeB": "#/components/schemas/PolymorphicTypeB",
}
}
},
"PolymorphicTypeA" : {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/PolymorphicTypeBase"
}
],
"properties": {
"propertyA": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
},
"PolymorphicTypeB" : {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/PolymorphicTypeBase"
}
],
"properties": {
"propertyB": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
...
THE ISSUE
I want the openapi-generator-cli
to generate models in the client library that resemble the structure of the ComplexType
, where the PolymorphicProperty
is of type PolymorphicTypeBase
. Instead, what I currently get is an extra type called ComplexTypePolymorphicProperty
, which contains annotations for known subtypes but does not reference PolymorphicTypeBase
anywhere. This occurs because the information about the base type (PolymorphicTypeBase
) seems to be lost in the openAPI spec.
Question
Is there a way to reference the base type in the openAPI spec?
Can we customize the type and property names produced by the c#
, typescript-angular
, and java
generators?