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

[CSharp] Objects with only inherited properties are not generated #5070

Open
rima1098 opened this issue Dec 19, 2024 · 0 comments
Open

[CSharp] Objects with only inherited properties are not generated #5070

rima1098 opened this issue Dec 19, 2024 · 0 comments

Comments

@rima1098
Copy link

Describe the bug

When a schema (object) only inherits properties from a base object and does not have own properties then the corresponding C# class is not generated and on the Base object no JsonInheritanceAttribute is generated.

I tried to circumvent this by setting an additional Attribute on BaseObject with the objectType: ObjectA, however, it turns out that a stackoverflow occurs when trying to write a BaseObject.

Eventhough ObjectA in general equals the BaseObject, it might still be necessary for the API to know wheter it receives ObjectA or only a BaseObject.

Version used

14.2.0

To Reproduce

generate C# Client in Nswag Studio with the default settings and the following specification:

openapi: 3.0.0
info:
  title: test
  description: test
  version: 0.0.0

paths:
  /test:
    post:
      operationId: test
      requestBody:
        content:
          'application/json':
            schema:
              $ref: '#/components/schemas/BaseObject'
      responses:
        '200':
          description: "OK"
        
components:
  schemas:
    BaseObject:
      type: object
      required:
        - objectType
        - prop1
      properties:
        objectType:
          type: string
        prop1:
          type: string
      discriminator:
        propertyName: objectType
          
    ObjectA:
      allOf:
        - $ref: '#/components/schemas/BaseObject'
        
    ObjectB:
      allOf:
        - $ref: '#/components/schemas/BaseObject'
      properties:
        prop2:
          type: string
  

Expected behavior

A C# Class for Object A should be generated like:

public class ObjectA : BaseObject ...

A JsonInheritanceAttribute should be generated on the Base Object:

[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), "objectType")]
[JsonInheritanceAttribute("ObjectB", typeof(ObjectB))]
[JsonInheritanceAttribute("ObjecA", typeof(ObjectA))]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class BaseObject
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

1 participant