RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

Multi-level inheritance more than one member with JsonExtensionDataAttribute #3888

Open csehatt741 opened 2 years ago

csehatt741 commented 2 years ago

When the OpenAPI 3.0 schema defines multi-level inheritance the generated children also have AdditionalProperties, which leads to the following runtime error: System.InvalidOperationException: 'The type 'TypeADerived' cannot have more than one member that has the attribute 'System.Text.Json.Serialization.JsonExtensionDataAttribute'.'

NSwag: 13.15.9 Serialization: System.Text.Json

OpenAPI definition:

TypeBase:
  type: object
  required:
    - $type
  properties:
    $type:
      type: string
  discriminator:
    propertyName: $type
TypeA:
  allOf:
    - $ref: '#/components/schemas/TypeBase'
    - type: object
TypeADerived:
  allOf:
    - $ref: '#/components/schemas/TypeA'
    - type: object
TypeB:
  allOf:
    - $ref: '#/components/schemas/TypeBase'
    - type: object
TypeBDerived:
  allOf:
    - $ref: '#/components/schemas/TypeB'
    - type: object

Generated model:

[JsonInheritanceConverter(typeof(TypeBase), "$type")]
[JsonInheritanceAttribute("TypeA", typeof(TypeA))]
[JsonInheritanceAttribute("TypeADerived", typeof(TypeADerived))]
[JsonInheritanceAttribute("TypeB", typeof(TypeB))]
[JsonInheritanceAttribute("TypeBDerived", typeof(TypeBDerived))]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.9.0 (NJsonSchema v10.6.8.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TypeBase
{

    private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

    [System.Text.Json.Serialization.JsonExtensionData]
    public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
    {
        get { return _additionalProperties; }
        set { _additionalProperties = value; }
    }

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.9.0 (NJsonSchema v10.6.8.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TypeA : TypeBase
{

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.9.0 (NJsonSchema v10.6.8.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TypeADerived : TypeA
{

    private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

    [System.Text.Json.Serialization.JsonExtensionData]
    public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
    {
        get { return _additionalProperties; }
        set { _additionalProperties = value; }
    }

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.9.0 (NJsonSchema v10.6.8.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TypeB : TypeBase
{

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.9.0 (NJsonSchema v10.6.8.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TypeBDerived : TypeB
{

    private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

    [System.Text.Json.Serialization.JsonExtensionData]
    public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
    {
        get { return _additionalProperties; }
        set { _additionalProperties = value; }
    }

}
StevenQuickGS1 commented 2 years ago

@csehatt741 did you workaround/resolve this?

StevenQuickGS1 commented 2 years ago

Same as older issue here https://github.com/RicoSuter/NSwag/issues/2818

csehatt741 commented 2 years ago

I had to change the model.

binginsin commented 2 years ago

This seems to have been fixed by #1512