OpenAPITools / OpenAPITools/openapi-generator
[BUG] [csharp-netcore] Usage of snake_case option generates models constructor function error
Nobody has claimed this yet.
- 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
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.MyTokenRequestMyTokenType? 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the csharp-netcore generation command, the supplied OpenAPI document and config, then inspect modelGeneric.mustache around line 132, especially the camelcase_param use of name. Reproduce the generated MyTokenRequest constructor and confirm it emits distinct parameters for $_type and type; done means the generated C# code builds with the expected assignments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100