Openapi-generator: [REQ][CSharp] Set EmitDefaultValue to true

Created on 3 Jul 2019  路  12Comments  路  Source: OpenAPITools/openapi-generator

Is your feature request related to a problem? Please describe.

CSharp generated files have "EmitDefaultValue=false".

[DataContract]
    public partial class MyClass: IEquatable<MyClass>
    { 
        /// <summary>
        /// Gets or Sets MyVal
        /// </summary>
        [DataMember(Name="Unrealized",  EmitDefaultValue=false)]
        public double MyVal{ get; set; }`
        ...
    }

Describe the solution you'd like

There should be an option to not set this value (The default is true).
It looks like there is a option on https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/csharp-refactor.md.
But i was unable to generate models with that generator.

So something like --additional-properties optionalEmitDefaultValues=true

Describe alternatives you've considered

None?

Feature

Most helpful comment

I agree, a nullable value can be null.

But returning a nullable field in this case makes no sense, right? Either you throw a hard error. Or you produce a correct result of the calculation, thus the resulting double is always a value never null. Thus setting nullable: false, should make sense?

The default value of a double in c# is 0.0 afterall. Meaning if you set EmitDefaultValue of a double(not double? if nullable is set to true) to false. It will not emit a property when it's 0.0.

double? -> default value is null - Per default this sets emit default value to true so no big deal.
double -> default value is 0.0 - I still want to emit this.

All 12 comments

EmitDefaultValue's value is determined by the nullable attribute (OAS v3) in the model property. If you're still using OAS v2, please use x-nullable: true instead.

Ref: https://github.com/OpenAPITools/openapi-generator/blob/26c493b4a13f48224706f247dceacd22d20cede5/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache#L34

This means, all nullable: false omit default values, meaning if i have a number, say:

 MyDouble:
          type: number
          format: double
          nullable: false
          example: 0.0

I would still want it to explicitly write "MyDouble": 0.0 in my api result. But i am unable to do that?

Are you referring to the default value instead?

If I remember correctly, default values are not included in the client's model as the server should take care of these default values.

Well in this case the value "0.0" would be the default value and thus omitted in the result.

A concrete example:

A simple subtraction service takes 2 values as input, and should return the result.

If a client sends {"value1": 2.0, "value2": 2.0}.
Then the server should reply with {"result": 0.0}.
The result cannot be nullable, thus nullable: false, and since 0.0 is the default value it will omit the result thus only returning {}. This means a client would have to infer the 0.0 (which is possible). But i would personally prefer the explicitness of the actual 0.0 value.

From what I understand, nullable value is just null (not false, 0.0): https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-20

I agree, a nullable value can be null.

But returning a nullable field in this case makes no sense, right? Either you throw a hard error. Or you produce a correct result of the calculation, thus the resulting double is always a value never null. Thus setting nullable: false, should make sense?

The default value of a double in c# is 0.0 afterall. Meaning if you set EmitDefaultValue of a double(not double? if nullable is set to true) to false. It will not emit a property when it's 0.0.

double? -> default value is null - Per default this sets emit default value to true so no big deal.
double -> default value is 0.0 - I still want to emit this.

double -> default value is 0.0 - I still want to emit this.

One solution is to customize the templates with the -t option and hard code EmitDefaultValue to true.

I will try to look into it monday. Thanks looking into it with me - I really appreciate it.
I update the issue and possibly close it then, if that's alright :) - I personally still think it should be a separate option if you want to display a value vs it being nullable.

Hello, any news on this issue? optionalEmitDefaultValues parameter is still ignored.

This whole behaviour of omitting values is fatal.

We use a request validation on the server side and fields - marked as _required_ - are missing because those values are either 0 or false. Those requests are rejected because they are invalid. And the request validation is right - required fields have to be there.

Therefore:
I plead for handle this issue as an error.
And the default value for the optionalEmitDefaultValues shall be true.

Had anyone time to look at the pull request?

Just reviewed and merged. Thanks for the PR.

Let's go with this approach and see if users have any feedback on this.

Was this page helpful?
0 / 5 - 0 ratings