Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Generated code does not compile for inheritance with System.Text.Json #3871

@csehatt741

Description

@csehatt741

When the OpenAPI 3.0 schema defines inheritance the generated code using System.Text.Json is full of compilation errors.

NSwag: 13.15.7

OpenAPI definition:

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

Command: dotnet nswag openapi2csclient /JsonLibrary:SystemTextJson

Generated model specifies JsonInheritanceConverter improperly, which leaves a compilation error:

[JsonInheritanceConverter(typeof(TypeBase), "$type")]
[JsonInheritanceAttribute("TypeA", typeof(TypeA))]
[JsonInheritanceAttribute("TypeB", typeof(TypeB))]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.15.7.0 (NJsonSchema v10.6.7.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.7.0 (NJsonSchema v10.6.7.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TypeA : TypeBase
{

}

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

}

Generated JsonInheritanceConverter still uses NewtonsoftJson, which leaves a lot of compilation errors: internal class JsonInheritanceConverter : Newtonsoft.Json.JsonConverter

Is there any issue with the OpenAPI definition or does the command require some more parameters?
Though, the generated code using NewtonsoftJson works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions