[BUG][csharp-netcore] C# Model never calls default constructor, it always calls parametrized constructor
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [x] [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>();
}
What is the reason you'd like the default constructor to be called?
There is no reason to manually assign all the properties in the default constructor either (unless we are talking about nullable reference types), they are set to the default values well... by default.
Please present an actual issue that this either creates or would solve if changed.
@Blackclaws this may be related: https://github.com/OpenAPITools/openapi-generator/issues/13128 I would love to use a parameterless constructor with no null checks, nor setting the _flag* for primitive types.
I would like to second this. As the constructor sets default values even for required parameters, the client consumer can make the mistake of initializing the class using the "constructor closure pattern" (don't know the right term):
protected CreateUserRequest() { }
public CreateUserRequest(string firstName = default)
{
if (firstName == null)
{
throw new ArgumentNullException("firstName is a required property for CreateUserRequest and cannot be null");
}
}
var userReq = new CreateUserRequest
{
firstName = "asdas",
}
// throws an ArgumentNullException - firstName is required - even though it is set
It would be preferable if the consumer got a syntax error on new CreateUserRequest() stating that there is no matching constructor - or that the empty constructor is public, so that the consumer can use the empty constructor pattern.
Now the coding error occurs during runtime.
Method protected CommHttpProxyPolicy() is protected. If it's made public, then it will be called when using new CommHttpProxyPolicy(). I wonder if this is what you want.
