Open Ultre00 opened 3 years ago
I tried to debug this and apparently it has to do something with the RemoveNullability check in the NJsonSchema.CodeGeneration. https://github.com/RicoSuter/NJsonSchema/blob/288eb13e931562264ed9ddd12a83741c67a1ddc4/src/NJsonSchema.CodeGeneration/TypeResolverBase.cs#L89
I don't know what is happening here. Why is it checking if the inherited type is not nullable? Because of the fact that this check returns false it is returning the schema of the first inherited type instead of the base type.
Is there any work around for this?
Hi, I encountered a related issue in case of generic arrays:
type: array
items:
oneOf:
The generated C# List is not of the base class neither.
Any updates or workaround?
I also recently encountered this when using oneOf
in responses where the schemas all inherit a base through allOf
.
Yes. Help.
This swagger, which references the abstract class:
"AccessPolicy": {
"type": "object",
"properties": {
"assignee": {
"$ref": "#/components/schemas/Party"
}
}
}
Produces proper client:
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class AccessPolicy
{
[Newtonsoft.Json.JsonProperty("assignee", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Party Assignee { get; set; }
}
This swagger, using OneOf :
"AccessPolicy": {
"type": "object",
"properties": {
"assignee": {
"oneOf": [
{
"$ref": "#/components/schemas/AnyParty"
},
{
"$ref": "#/components/schemas/UserParty"
}
]
}
},
"Party": {
"required": [
"$type"
],
"type": "object",
"properties": {
"$type": {
"type": "string"
},
"id": {
"type": "string",
"description": "Party id.",
"nullable": true
},
"name": {
"type": "string",
"description": "Party name.",
"nullable": true
}
},
"additionalProperties": false,
"description": "Represents an actor attempting to perform an action on a resource.",
"discriminator": {
"propertyName": "$type",
"mapping": {
"AnyParty": "#/components/schemas/AnyParty",
"UserParty": "#/components/schemas/UserParty"
}
}
},
"UserParty": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Party"
}
],
"additionalProperties": false,
"description": "User party type."
},
"AnyParty": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Party"
}
],
"additionalProperties": false,
"description": "Any party record."
}
Produces unusable client
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class AccessPolicy
{
/// <summary>
/// Atlas.FileStore.Security.PartyParty.
/// </summary>
[Newtonsoft.Json.JsonProperty("assignee", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public AnyParty Assignee { get; set; }
}
Might not be a perfect solution. But by making the base class non abstract, it gets added to the swagger, and generates a usable client.
When you generate a C# class then the first oneOf is being used as a parameter in e put request. I would expect this to be the abstract class. See example below. CustomerCreateUpdateDto should be used and not CompanyCreateUpdateDto
JSON
Generated Code