Skip to content

Commit d635ac3

Browse files
Valentin-Tintinwing328
authored andcommitted
Feature optional emit default values (#4347)
* 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
1 parent 55737c2 commit d635ac3

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

docs/generators/csharp-netcore.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sidebar_label: csharp-netcore
1919
|returnICollection|Return ICollection<T> instead of the concrete type.| |false|
2020
|optionalMethodArgument|C# Optional method argument, e.g. void square(int x=10) (.net 4.0+ only).| |true|
2121
|optionalAssemblyInfo|Generate AssemblyInfo.cs.| |true|
22+
|optionalEmitDefaultValues|Set DataMember's EmitDefaultValue.| |false|
2223
|optionalProjectFile|Generate {PackageName}.csproj.| |true|
2324
|nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.| |false|
2425
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ public CSharpNetCoreClientCodegen() {
185185
CodegenConstants.OPTIONAL_ASSEMBLY_INFO_DESC,
186186
this.optionalAssemblyInfoFlag);
187187

188+
addSwitch(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES,
189+
CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES_DESC,
190+
this.optionalEmitDefaultValuesFlag);
191+
188192
addSwitch(CodegenConstants.OPTIONAL_PROJECT_FILE,
189193
CodegenConstants.OPTIONAL_PROJECT_FILE_DESC,
190194
this.optionalProjectFileFlag);
@@ -370,6 +374,8 @@ public String modelTestFileFolder() {
370374
@Override
371375
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
372376
postProcessPattern(property.pattern, property.vendorExtensions);
377+
postProcessEmitDefaultValue(property.vendorExtensions);
378+
373379
super.postProcessModelProperty(model, property);
374380
}
375381

@@ -403,6 +409,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
403409
@Override
404410
public void postProcessParameter(CodegenParameter parameter) {
405411
postProcessPattern(parameter.pattern, parameter.vendorExtensions);
412+
postProcessEmitDefaultValue(parameter.vendorExtensions);
406413
super.postProcessParameter(parameter);
407414

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

456+
public void postProcessEmitDefaultValue(Map<String, Object> vendorExtensions) {
457+
vendorExtensions.put("x-emit-default-value", optionalEmitDefaultValuesFlag);
458+
}
459+
449460
@Override
450461
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
451462
// To avoid unexpected behaviors when options are passed programmatically such as { "supportsAsync": "" }
@@ -465,6 +476,13 @@ public void processOpts() {
465476
* if (additionalProperties.containsKey(prop)) convertPropertyToBooleanAndWriteBack(prop);
466477
*/
467478

479+
if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES)) {
480+
setOptionalEmitDefaultValuesFlag(convertPropertyToBooleanAndWriteBack(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES));
481+
} else {
482+
additionalProperties.put(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, optionalEmitDefaultValuesFlag);
483+
}
484+
485+
468486
if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) {
469487
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
470488
}
@@ -610,6 +628,10 @@ public void setOptionalAssemblyInfoFlag(boolean flag) {
610628
this.optionalAssemblyInfoFlag = flag;
611629
}
612630

631+
public void setOptionalEmitDefaultValuesFlag(boolean flag){
632+
this.optionalEmitDefaultValuesFlag = flag;
633+
}
634+
613635
public void setOptionalProjectFileFlag(boolean flag) {
614636
this.optionalProjectFileFlag = flag;
615637
}

modules/openapi-generator/src/main/resources/csharp-netcore/modelGeneric.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{{#description}}
3434
/// <value>{{description}}</value>
3535
{{/description}}
36-
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}})]
36+
[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}})]
3737
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
3838
{{/isEnum}}
3939
{{/vars}}
@@ -116,7 +116,7 @@
116116
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
117117
/// </summary>{{#description}}
118118
/// <value>{{description}}</value>{{/description}}
119-
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}})]{{#isDate}}
119+
[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}}
120120
[JsonConverter(typeof(OpenAPIDateConverter))]{{/isDate}}
121121
public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
122122

0 commit comments

Comments
 (0)