OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSharp] constructor does not initialize enum properties when ConditionalSerialization=true, though they are in the parameters

Open
#18,026 1 comment 1 reaction 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When one runs the openapi-generator-cli 2.7.0-2.10.0 against a swagger.json to make a csharp client with the argument conditionalSerialization=true, the generated model class's constructor does not initialize any enum properties, though they are present in the constructor parameters, and the arguments for them are passed by the calling code.

Constructor that is output is:

        public TestModel(string text = default(string), SomeEnumEnum? someEnum = SomeEnumEnum.NUMBER_1)
        {
            this._Text = text;
            if (this.Text != null)
            {
                this._flagText = true;
            }
        }

Expected result is:

        public TestModel(string text = default(string), SomeEnumEnum? someEnum = SomeEnumEnum.NUMBER_1)
        {
            this._Text = text;
            if (this.Text != null)
            {
                this._flagText = true;
            }
            this._someEnum = someEnum;
            if (this.SomeEnum != null)
            {
                this._flagSomeEnum = true;
            }
        }

This doesn't happen when conditionalSerialization=false; at that point, the enum is initialized properly just as expected.

openapi-generator version

CLI version:

2.7.0 - 2.10.0 (don't know if it affects earlier versions)

OpenAPI declaration file content or url
{
    "openapi": "3.0.1",
    "info": {
        "title": "test functions",
        "description": "test functions",
        "version": "1.0.0"
    },
    "servers": [
        {
            "url": "http://localhost:7204/api"
        }
    ],
    "paths": {
        "/Function2": {
            "get": {
                "operationId": "function 2",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/testModel"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "200": {
                        "description": "Payload of TestModel",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/testModel"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "testModel": {
                "type": "object",
                "properties": {
                    "text": {
                        "type": "string",
                        "description": "Nullable Text",
                        "nullable": true
                    },
                    "someEnum": {
                        "enum": [
                            1,
                            2
                        ],
                        "type": "integer",
                        "format": "int32",
                        "default": 1
                    }
                }
            }
        }
    }
}

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 by locating the C# generator code that handles conditionalSerialization and constructor generation, then reproduce the issue with the supplied OpenAPI schema and compare it with conditionalSerialization=false. Done means generated constructors assign enum parameters and set their conditional flags, with regression coverage for the supplied model.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.