swagger-api / swagger-api/swagger-codegen

[C#] "allOf" and class inheritence

Open
#4,273 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Hi, all

I have problem with using "allOf" and generating class hierarchy suing C# client code generator on http://editor.swagger.io.

YAML:

swagger: '2.0'

info:
  version: '1.0.0'
  title: Demo

paths:
  /:
    post:
      operationId: demoPath
      responses:
        default:
          description: None
     
definitions:
  IntegerValue:
    allOf:
      - $ref: '#/definitions/Value'
    type: object
    properties:
      IntegerValue:
        type: integer
  Value:
    description: Value.
    type: object
    properties:
      StringValue:
        type: string
      NumericValue:
        type: number
        format: double

As you can see IntegerValue should inherit all Value's properties and add IntegerValue property.
In generated C# code I can see:
C#
image

As you can see IntegerValue copy all Value's properties but do not add any new.
Same issue happened if we will use 3 or more inheritance level: only primal base class properties are generated.

Solution:
I hope that there is simple fix.
Derived class should:

  1. inherit base class (without coping properties)
  2. contains new properties
  3. use constructor with all properties and send base properties to base constructor using "base" word.

NOTE: wrong model produce wrong model test.
See Folder IO.Swagger.Test.Model - test equal to this wrong generated models.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Swagger YAML and the generated models in Folder IO.Swagger.Test.Model, then inspect the C# generator output for the multi-level allOf case. Compare the generated inheritance and model tests with the expected hierarchy. Done means derived classes inherit base properties without copying them, add their own properties, and the model tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.