OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.78k stars 6.57k forks source link

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

Open RedZone908 opened 8 months ago

RedZone908 commented 8 months ago

Bug Report Checklist

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
                    }
                }
            }
        }
    }
}
RedZone908 commented 8 months ago

I'm also wondering if it's related to this issue here? https://github.com/OpenAPITools/openapi-generator/issues/13128