OpenAPITools / OpenAPITools/openapi-generator
[BUG][csharp-netcore] C# Model never calls default constructor, it always calls parametrized constructor
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
C# Model has two constructor
- Default constructor
- Parameterized constructor
When we create a object using default constructor , it never calls the default constructor it always calls the parametrized constructor which has default value for all the parameters.
Example code defined in the class
public CommHttpProxyPolicy
{
[JsonConstructorAttribute]
protected CommHttpProxyPolicy()
{
this.AdditionalProperties = new Dictionary<string, object>();
}
public CommHttpProxyPolicy(ClassIdEnum classId = ClassIdEnum.CommHttpProxyPolicy, ObjectTypeEnum objectType = ObjectTypeEnum.CommHttpProxyPolicy, List<HyperflexClusterProfileRelationship> clusterProfiles = default(List<HyperflexClusterProfileRelationship>), string moid = default(string), List<string> owners = default(List<string>), List<MoTag> tags = default(List<MoTag>), MoVersionContext versionContext = default(MoVersionContext), MoBaseMoRelationship parent = default(MoBaseMoRelationship), string description = default(string), string name = default(string), string hostname = default(string), string password = default(string), long port = default(long), string username = default(string), OrganizationOrganizationRelationship organization = default(OrganizationOrganizationRelationship)) : base()
{
this.ClassId = classId;
this.ObjectType = objectType;
this.ClusterProfiles = clusterProfiles;
this.AdditionalProperties = new Dictionary<string, object>();
}
}
when we create a object for the above class
CommHttpProxyPolicy comm = new CommHttpProxyPolicy() // it always calls the parametrized constructor
ideally it should call the default constructor.
openapi-generator version
OAS v3
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
create the object using default constructor
CommHttpProxyPolicy comm = new CommHttpProxyPolicy() // it always calls the parametrized constructor even no parameter is specified.
Related issues/PRs
Suggest a fix
We can initialize the properties using default constructor with defaults.
protected CommHttpProxyPolicy()
{
this.AdditionalProperties = new Dictionary<string, object>();
ClassId = ClassIdEnum.CommHttpProxyPolicy;
ObjectType = ObjectTypeEnum.CommHttpProxyPolicy;
ClusterProfiles = default(List<HyperflexClusterProfileRelationship>);
Moid = default(string);
Owners = default(List<string>);
Tags = default(List<MoTag>);
VersionContext = default(MoVersionContext);
Parent = default(MoBaseMoRelationship);
Description = default(string);
Name = default(string);
Hostname = default(string);
Password = default(string);
Port = default(long);
Username = default(string);
Organization = default(OrganizationOrganizationRelationship)
}
but for parametrized constructor we should not use parameters with default value.
public CommHttpProxyPolicy(ClassIdEnum classId , ObjectTypeEnum objectType , List<HyperflexClusterProfileRelationship> clusterProfiles, string moid , List<string> owners , List<MoTag> tags , MoVersionContext versionContext, MoBaseMoRelationship parent , string description , string name , string hostname , string password , long port , string username , OrganizationOrganizationRelationship organization ) : base()
{
this.ClassId = classId;
this.ObjectType = objectType;
this.ClusterProfiles = clusterProfiles;
this.AdditionalProperties = new Dictionary<string, object>();
}
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 by reproducing the generated C# model behavior described in the issue, focusing on how the default and parameterized constructors are emitted. Confirm that parameterless instantiation uses the default constructor and that the parameterized constructor no longer relies on optional parameters; add or update a regression test if the relevant generator tests are located.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100