Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #14571
Closed
Open
Issue created Jan 31, 2023 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG] [csharp-netcore] Usage of snake_case option generates models constructor function error

Created by: salimzdn

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

While trying to generate c# source code with the csharp-netcore generator, and using the config option "modelPropertyNaming" with "snake_case" value, the generated models contain error in their constructor. Constructors contain duplicated input parameter.

It happens with this 2 schema parameters defined in the OpenAPI declaration file with the name

  • type
  • $_type
openapi-generator version

openapi-generator-cli 6.2.1, I have no idea if it's a regression.

OpenAPI declaration file content or url
{
    "openapi": "3.0.1",
    "info": {
        "title": "My Web API",
        "description": "My Web API",
        "version": "1.22.2"
    },
    "tags": [],
    "paths": {},
    "components": {
        "schemas": {
            "MyTokenType": {
                "type": "string",
                "description": "Token type",
                "enum": [
                    "JWT",
                    "OIDC_ACCESS_TOKEN"
                ],
                "x-enum-descriptions": [
                    "JWT",
                    "OIDC_ACCESS_TOKEN"
                ]
            },
            "MyTokenRequest": {
                "type": "object",
                "description": "My token request",
                "properties": {
                    "$_type": {
                        "type": "string",
                        "default": "MyTokenRequest",
                        "enum": [
                            "MyTokenRequest"
                        ]
                    },
                    "token": {
                        "type": "string"
                    },
                    "type": {
                        "$ref": "#/components/schemas/MyTokenType"
                    }
                }
            }
        }
    }
}
Generation Details

npx openapi-generator-cli generate -i swagger_simple_anonymised.json -g csharp-netcore -o out_dotnet -c openApiGeneratorConfig.json

And this is the openApiGeneratorConfig.json file :

{
    "packageName" : "com.myApi",
    "nullableReferenceTypes" : true,
    "targetFramework" : "net6.0",
    "modelPropertyNaming" : "snake_case"
}
Steps to reproduce

Use the command line with the given OpenAPI declaration file and the given config file. You should probably be able to reproduce it with any two parameters having this name rule :

  • name
  • $_name

It produces the following result :

namespace com. myApi.Model
{
    /// <summary>
    /// My token request
    /// </summary>
    [DataContract(Name = "MyTokenRequest")]
    public partial class MyTokenRequest : IEquatable<MyTokenRequest>, IValidatableObject
    {
        /// <summary>
        /// Defines _type
        /// </summary>
        [JsonConverter(typeof(StringEnumConverter))]
        public enum TypeEnum
        {
            /// <summary>
            /// Enum MyTokenRequest for value: MyTokenRequest
            /// </summary>
            [EnumMember(Value = "MyTokenRequest")]
            MyTokenRequest = 1

        }


        /// <summary>
        /// Gets or Sets _type
        /// </summary>
        [DataMember(Name = "$_type", EmitDefaultValue = false)]
        public TypeEnum? _type { get; set; }

        /// <summary>
        /// Gets or Sets type
        /// </summary>
        [DataMember(Name = "type", EmitDefaultValue = false)]
        public MyTokenType? type { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MyTokenRequest" /> class.
        /// </summary>
        /// <param name="type">type (default to TypeEnum.MyTokenRequest).</param>
        /// <param name="token">token.</param>
        /// <param name="type">type.</param>
        public MyTokenRequest(TypeEnum? type = TypeEnum.MyTokenRequest, string token = default(string), MyTokenType? type = default(MyTokenType?))
        {
            this._type = type;
            this.token = token;
            this.type = type;
        }
    }
}

//Useless functions removed

In this result the two parameters have diferents name :

  • public TypeEnum? _type { get; set; }
  • public MyTokenType? type { get; set; }

So this part is fine.

But the constructor function contains twice the same input parameter name :

  • TypeEnum? type = TypeEnum.MyTokenRequest
  • MyTokenType? type = default(MyTokenType?)

It makes this code unbuildable.

The expected constructor function is :

public MyTokenRequest(TypeEnum? _type = TypeEnum.MyTokenRequest, string token = default(string), MyTokenType? type = default(MyTokenType?))
        {
            this._type = _type;
            this.token = token;
            this.type = type;
        }
Related issues/PRs

None found

Suggest a fix

After having a look in the source code of the generator I have found the mustache file that is probably used to generate this constructor function : modelGeneric.mustache And particulary in the line 132 :

public {{classname}}( {{#readWriteVars}}{{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/isEnum}} {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}} = {{#defaultValue}}{{^isDateTime}}{{{defaultValue}}}{{/isDateTime}}{{#isDateTime}}default({{{datatypeWithEnum}}}){{/isDateTime}}{{/defaultValue}}{{^defaultValue}}default({{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/isEnum}}){{/defaultValue}}{{^-last}}, {{/-last}}{{/readWriteVars}}){{#parent}} : base({{#parentVars}}{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}{{^-last}}, {{/-last}}{{/parentVars}}){{/parent}}

The specific parameter is probably defined in the following code of this line : {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}

I suppose that, in order to fix this bug, this name parameter should be fixed, but I was not able to find where for now.

I would appreciate to have any help to resolve this bug.

Thank you

Assignee
Assign to
Time tracking