OpenAPITools / OpenAPITools/openapi-generator
[BUG] [csharp] EmitDefault false should allow to set null value for the nullable property type.
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
As per open API specification there are two ways to define the type which can be set to null. When EmitDefault is set to false in config file while generating the sdk, nullable property should not have attribute EmitDeafult = false
Here is the two case in which nullable property has two different behavior.
In case1 csharp does not apply EmitDefault fasle to the nullable property which is perfectly fine, where as in case2 it is unable to recognize the AssignesServer as nullable property and set the EmitDefault : false.
First way to define nullable property. (generator works as expected.)
Case1:-
"ConfigChangeDetails":
{
"description": "An array of relationships to serverConfigChangeDetail resources.",
"type": "array",
"items": {
"$ref": "#/components/schemas/server.ConfigChangeDetail.Relationship"
},
"nullable": true,
"readOnly": true,
"x-since": "2017-10-02"
}
for the above definition the generator is able to generate the perfect code as per the definition, with EmitDefault = true so that null can also be set.
The issue exist is the below i.e second way to define nullable property
Case2:- (generator does not work as expected.)
"AssignedServer": {
"$ref": "#/components/schemas/compute.Physical.Relationship",
"description": "A reference to a computePhysical resource.\nWhen the $expand query parameter is specified, the referenced resource is returned inline.",
"x-since": "2017-10-02"
}
"compute.Physical.Relationship": {
"description": "A relationship to the 'compute.Physical' resource, or the expanded 'compute.Physical' resource, or the 'null' value.",
"x-one-of-name": "compute.Physical.Relationship",
"oneOf": [
{
"type": "null"
},
{
"$ref": "#/components/schemas/mo.MoRef"
},
{
"$ref": "#/components/schemas/compute.Physical"
}
],
"discriminator": {
"propertyName": "ClassId"
}
}
The above property AssignedServer can also be set to null.
But the generator generates the code with EmitDefault = false which leads to issue, so the AssignedServer cannot be set to null.
generator should recognize such property as nullable property.
openapi-generator version
Latest Master
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Set the EmitDeafult : false in config file.
In the open API document define the property with oneOf which can be accept null or an object.
generate the code and check the EmitDeaflt attribute, it will never allow to set this to null, where as null is valid value for that property.
Related issues/PRs
Suggest a fix
generator needs to parse the property as nullable property.
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 C# generation case with EmitDefault set to false and the supplied oneOf schema containing a null type. Trace how the generator identifies nullable properties and emits the EmitDefault attribute; done means AssignedServer is recognized as nullable and the generated property can be set to null without the incorrect attribute.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, openapi
- Domain
- devtools, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100