OpenAPITools / OpenAPITools/openapi-generator

[BUG] [C#] Polymorphism with 2 level derived classes generates invalid client

Open
#8,849 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Inline Schema Handling Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Generated client is invalid for this example:
Using latest version 5.0.1

    public class BaseClass1
    {
        public int Number { get; set; }
    }

    public class DerivedClass_A : BaseClass1
    {
        public int Number2 { get; set; }
    }

    public class DerivedClass_B : DerivedClass_A
    {
        public int Number3 { get; set; }
    }

document

{
  "openapi": "3.0.1",
  "info": {
    "title": "Test",
    "description": "Test",
    "contact": {
      "name": "John Doe",
      "url": "https://mydomain.com",
      "email": "johndoe@hotmail.com"
    },
    "license": {
      "name": "-"
    },
    "version": "1.0.0.0"
  },
  "servers": [
    {
      "url": "http://127.0.0.1:5000"
    }
  ],
  "paths": {
    "/poly1": {
      "get": {
        "tags": [
          "Test"
        ],
        "operationId": "TestPolymorphism1",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "$ref": "#/components/schemas/BaseClass1"
                    },
                    {
                      "$ref": "#/components/schemas/DerivedClass_A"
                    },
                    {
                      "$ref": "#/components/schemas/DerivedClass_B"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "BaseClass1": {
        "required": [
          "discriminator"
        ],
        "type": "object",
        "properties": {
          "discriminator": {
            "type": "string"
          },
          "number": {
            "type": "integer",
            "format": "int32"
          }
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "discriminator",
          "mapping": {
            "BaseClass1": "#/components/schemas/BaseClass1",
            "DerivedClass_A": "#/components/schemas/DerivedClass_A",
            "DerivedClass_B": "#/components/schemas/DerivedClass_B"
          }
        }
      },
      "DerivedClass_A": {
        "required": [
          "discriminator"
        ],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/BaseClass1"
          }
        ],
        "properties": {
          "discriminator": {
            "type": "string"
          },
          "number2": {
            "type": "integer",
            "format": "int32"
          }
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "discriminator",
          "mapping": {
            "DerivedClass_A": "#/components/schemas/DerivedClass_A",
            "DerivedClass_B": "#/components/schemas/DerivedClass_B"
          }
        }
      },
      "DerivedClass_B": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/DerivedClass_A"
          }
        ],
        "properties": {
          "number3": {
            "type": "integer",
            "format": "int32"
          }
        },
        "additionalProperties": false
      }
    }
  }
}

The error is in the constructor of DerivedClass_A when calling base(....),
discriminator is missing in the arguments

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 supplied OpenAPI document and the generated DerivedClass_A constructor, where the report says the base call lacks the discriminator argument. Reproduce generation with version 5.0.1 and verify that the generated C# client compiles with the discriminator passed through the inheritance chain.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.