OpenAPITools / OpenAPITools/openapi-generator

[BUG][C#] Generator ignores "default" field name when it is a sibling of "allOf" field name

Open
#3,741 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description:

I would like to define a default value for a property of a class, whose type is an enum. So this enum is defined with a schema, and this schema is referenced by the property of my class, like so:

  "Color": {
    "enum": [
      "Red",
      "Blue",
    ],
    "type": "string",
    "description": "Specifies a color."
  }
      "PenColor": {
        "$ref": "#/components/schemas/Color"
      }

So I have my "PenColor" property, it is an enum of type "Color". Now I would like this property to have the default value of "Blue". The first thing I have tried was to add the "defaultValue" field as a sibling of the "$ref" field but this syntax is not allowed by Open API (see https://swagger.io/docs/specification/using-ref/ - last section "$ref and sibling elements").

I have done some research and found a workaround using the "allOf" field, it is explained on this link and seems to do the trick for some people: https://github.com/swagger-api/swagger-ui/issues/4732

Here is the updated Open API definition of my property using this syntax:

      "PenColor": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Color"
          }
        ],
        "default": "Blue"
      },

Unfortunately this does not work and here is respectively the generated code VS the code I am trying to obtain:

public Pen(PenColor color = default(PenColor)
public Pen(PenColor color = PenColor.Blue)

openapi-generator version:

5.0.0

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

Reproduce the C# generation case using the OpenAPI schema with a referenced Color enum, an allOf wrapper, and a default of "Blue". Compare the generated constructor with the requested PenColor.Blue default; done means the sibling default is honored without violating the $ref structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design, 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.