Description
The goal here is to improve model and api constructors. For models, the existing code gives all parameters a default value whether the parameter is nullable or not. This enables a class to be constructed in a bad state, where parameters that should never be null are. This PR will fix that. I am trying to do this in a way that will only effect the generic host library.
Removed sortParamsByRequiredFlag option from AbstractCSharp
All C# libraries should be sorting signature arguments so those with default values are last. I removed the CLI switch but enabled the option (the default was true anyway). I also enabled setSortModelPropertiesByRequiredFlag but only for GenericHost. It seems like a bug that it wasn't enabled before, but due to the constructor allowing all properties to be null, this was okay.
Added Lambdas RequiredParameterLambda and OptionalParameterLambda
The required lambda will strip a ?
from the text if present. The optional lambda will append a ?
if it is not present. I will explain below a bit more about why we need this.
Sorting parameters
We were not sorting models before at all. This was not an issue because all parameters were given default values.
Modified Constructors
Modified model constructors to not provide default values for non-nullable properties. This was rather difficult because there are two properties of note: required and isNullable. Unless I'm misunderstanding something, it seems that all non-required properties should be nullable. So when we decide if a property is nullable, we must look at both properties and also deal with specs that give us non-required and non-nullable properties, which doesn't make sense. That is where the new lambdas help us. If we just add the ? like string{{nrt?}}
then we might end up with string??
. I'll try to explain the new model constructor but it might be fruitless here since it is so long.
public {{classname}}({{#readWriteVars}}
#nullable #required = nullable, use lambda.optional
{{#isNullable}}{{#required}}{{#lambda.optional}}{{{datatypeWithEnum}}}{{/lambda.optional}}{{/required}}{{/isNullable}}
^nullable ^required = nullable, use lambda.optional
{{^isNullable}}{{^required}}{{#lambda.optional}}{{{datatypeWithEnum}}}{{/lambda.optional}}{{/required}}{{/isNullable}}
^nullable #required = required
{{^isNullable}}{{#required}}{{{datatypeWithEnum}}}{{/required}}{{/isNullable}}
#nullable ^required = nullable, use lambda.optional
{{#isNullable}}{{^required}}{{#lambda.optional}}{{{datatypeWithEnum}}}{{/lambda.optional}}{{/required}}{{/isNullable}}
{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}{{#defaultValue}} =
Default value provided, use it or just default
{{^isDateTime}}{{{defaultValue}}}{{/isDateTime}}{{#isDateTime}}default{{/isDateTime}}{{/defaultValue}}
No default value provided, use default
{{^required}}{{^defaultValue}} = default{{/defaultValue}}{{/required}}
{{^-last}}, {{/-last}}{{/readWriteVars}}){{#parent}}
: base({{#parentVars}}{{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}}{{^-last}}, {{/-last}}{{/parentVars}}){{/parent}}
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
Run the following to build the project and update samples: ./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh
./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH. -
File the PR against the correct branch: master
(5.3.0),6.0.x
-
If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@mandrean (2017/08) @frankyjuang (2019/09) @shibayan (2020/02) @Blackclaws (2021/03) @lucamazzanti (2021/05)