Skip to content

Commit

Permalink
Feature optional emit default values (#4347)
Browse files Browse the repository at this point in the history
* This commit addresses issue #4346 and adds the proposed optionalEmitDefaultValues flag.

* ran /bin/cshapr-netcore-petstore.sh to create sample clients

* Fixed lost newline

* Ran script again to update samples
  • Loading branch information
Valentin-Tintin authored and wing328 committed Nov 6, 2019
1 parent 55737c2 commit d635ac3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/generators/csharp-netcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sidebar_label: csharp-netcore
|returnICollection|Return ICollection<T> instead of the concrete type.| |false|
|optionalMethodArgument|C# Optional method argument, e.g. void square(int x=10) (.net 4.0+ only).| |true|
|optionalAssemblyInfo|Generate AssemblyInfo.cs.| |true|
|optionalEmitDefaultValues|Set DataMember's EmitDefaultValue.| |false|
|optionalProjectFile|Generate {PackageName}.csproj.| |true|
|nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.| |false|
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ public CSharpNetCoreClientCodegen() {
CodegenConstants.OPTIONAL_ASSEMBLY_INFO_DESC,
this.optionalAssemblyInfoFlag);

addSwitch(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES,
CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES_DESC,
this.optionalEmitDefaultValuesFlag);

addSwitch(CodegenConstants.OPTIONAL_PROJECT_FILE,
CodegenConstants.OPTIONAL_PROJECT_FILE_DESC,
this.optionalProjectFileFlag);
Expand Down Expand Up @@ -370,6 +374,8 @@ public String modelTestFileFolder() {
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
postProcessPattern(property.pattern, property.vendorExtensions);
postProcessEmitDefaultValue(property.vendorExtensions);

super.postProcessModelProperty(model, property);
}

Expand Down Expand Up @@ -403,6 +409,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
@Override
public void postProcessParameter(CodegenParameter parameter) {
postProcessPattern(parameter.pattern, parameter.vendorExtensions);
postProcessEmitDefaultValue(parameter.vendorExtensions);
super.postProcessParameter(parameter);

if (!parameter.required && nullableType.contains(parameter.dataType)) { //optional
Expand Down Expand Up @@ -446,6 +453,10 @@ public void postProcessPattern(String pattern, Map<String, Object> vendorExtensi
}
}

public void postProcessEmitDefaultValue(Map<String, Object> vendorExtensions) {
vendorExtensions.put("x-emit-default-value", optionalEmitDefaultValuesFlag);
}

@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
// To avoid unexpected behaviors when options are passed programmatically such as { "supportsAsync": "" }
Expand All @@ -465,6 +476,13 @@ public void processOpts() {
* if (additionalProperties.containsKey(prop)) convertPropertyToBooleanAndWriteBack(prop);
*/

if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES)) {
setOptionalEmitDefaultValuesFlag(convertPropertyToBooleanAndWriteBack(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES));
} else {
additionalProperties.put(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, optionalEmitDefaultValuesFlag);
}


if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) {
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
}
Expand Down Expand Up @@ -610,6 +628,10 @@ public void setOptionalAssemblyInfoFlag(boolean flag) {
this.optionalAssemblyInfoFlag = flag;
}

public void setOptionalEmitDefaultValuesFlag(boolean flag){
this.optionalEmitDefaultValuesFlag = flag;
}

public void setOptionalProjectFileFlag(boolean flag) {
this.optionalProjectFileFlag = flag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{{#description}}
/// <value>{{description}}</value>
{{/description}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}})]
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
{{/isEnum}}
{{/vars}}
Expand Down Expand Up @@ -116,7 +116,7 @@
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
/// </summary>{{#description}}
/// <value>{{description}}</value>{{/description}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}})]{{#isDate}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]{{#isDate}}
[JsonConverter(typeof(OpenAPIDateConverter))]{{/isDate}}
public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }

Expand Down

0 comments on commit d635ac3

Please sign in to comment.