Open MoienTajik opened 4 years ago
Also running into this and I think @MoienTajik comments are spot on.
We are also running into the exact same issue. The linked issue suggests it was fixed but that's not the case.
I believe this can be fixed by
public {{{datatype}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
to
public {{{datatype}}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
Description
I have a simple class called Test which contains only two primitive properties in my server-side application:
When I try to generate the client version of this class, swagger-gen generates a class like this for me:
As you can see, swagger-gen has converted the required primitive types like int and bool to nullable types automatically. That's not my desired result and I think it's not true or at least there should be a flag to turn this feature on or off!
Swagger-codegen version
3.0.18
Swagger declaration file content or url
Command line used for generation
CMD:
java -jar swagger-codegen-cli.jar generate -i ./swagger.json -l csharp
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/issues/2885
Suggest a fix/enhancement
I've started investigation on swagger-gen source code and I've found out that model generation template is placed in modelGeneric.mustache file.
Property generation is happening on line 35 and the question mark(?) is placed near the type name when the required flag is set to \^ which means false in mustache language:
I'm pretty sure that required is set to true in that place and ? should not be rendered here because this variable is used to determine and generate InvalidDataException in the constructor of the same class earlier and both of these statements loop over the same #vars variable:
This section works correctly and exceptions exist in the constructor:
But I couldn't find out why required is working for generating constructor exceptions and not working for property generations!