Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
csehatt741 opened this issue Feb 10, 2022 · 4 comments
Closed

Comments

@csehatt741
Copy link

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.

@RicoSuter
Copy link
Owner

Did you use latest NSwag version? I think this has been fixed recently...

@csehatt741
Copy link
Author

I've just updated to 13.15.8

Same, nothing changed.

@RicoSuter
Copy link
Owner

RicoSuter commented Feb 10, 2022

v13.15.9 (soon)

@csehatt741
Copy link
Author

After updating to v13.15.9, still there is one more compilation error remained: internal class JsonInheritanceConverterAttribute : JsonConverterAttribute

JsonConverterAttribute should be fully qualified -> System.Text.Json.Serialization.JsonConverterAttribute

RicoSuter added a commit to RicoSuter/NJsonSchema that referenced this issue Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants