Skip to content

Commit 261cee2

Browse files
committed
Remove null checks for C# value types
1 parent d748312 commit 261cee2

File tree

335 files changed

+2070
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

335 files changed

+2070
-518
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
7676
// nullable type
7777
protected Set<String> nullableType = new HashSet<String>();
7878

79+
protected Set<String> valueTypes = new HashSet<String>();
80+
7981
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCSharpCodegen.class);
8082

8183
public AbstractCSharpCodegen() {
@@ -190,6 +192,10 @@ public AbstractCSharpCodegen() {
190192
nullableType = new HashSet<String>(
191193
Arrays.asList("decimal", "bool", "int", "float", "long", "double", "DateTime", "Guid")
192194
);
195+
// value Types
196+
valueTypes = new HashSet<String>(
197+
Arrays.asList("decimal", "bool", "int", "float", "long", "double")
198+
);
193199
}
194200

195201
public void setReturnICollection(boolean returnICollection) {
@@ -414,6 +420,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
414420
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
415421
final Map<String, Object> processed = super.postProcessAllModels(objs);
416422
postProcessEnumRefs(processed);
423+
updateValueTypeProperty(processed);
417424
return processed;
418425
}
419426

@@ -454,6 +461,19 @@ private void postProcessEnumRefs(final Map<String, Object> models) {
454461
var.isPrimitiveType = true;
455462
}
456463
}
464+
for (CodegenProperty var : model.vars) {
465+
if (enumRefs.containsKey(var.dataType)) {
466+
// Handle any enum properties referred to by $ref.
467+
// This is different in C# than most other generators, because enums in C# are compiled to integral types,
468+
// while enums in many other languages are true objects.
469+
CodegenModel refModel = enumRefs.get(var.dataType);
470+
var.allowableValues = refModel.allowableValues;
471+
var.isEnum = true;
472+
473+
// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
474+
var.isPrimitiveType = true;
475+
}
476+
}
457477

458478
// We're looping all models here.
459479
if (model.isEnum) {
@@ -541,6 +561,23 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
541561
}
542562
}
543563

564+
/**
565+
* Update property if it is a C# value type
566+
*
567+
* @param models list of all models
568+
*/
569+
protected void updateValueTypeProperty(Map<String, Object> models) {
570+
for (Map.Entry<String, Object> entry : models.entrySet()) {
571+
String openAPIName = entry.getKey();
572+
CodegenModel model = ModelUtils.getModelByName(openAPIName, models);
573+
if (model != null) {
574+
for (CodegenProperty var : model.vars) {
575+
var.vendorExtensions.put("isValueType", isValueType(var));
576+
}
577+
}
578+
}
579+
}
580+
544581
@Override
545582
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
546583
super.postProcessOperationsWithModels(objs, allModels);
@@ -1055,6 +1092,17 @@ public boolean isDataTypeString(String dataType) {
10551092
"double".equals(dataType) || "decimal".equals(dataType) || "float".equals(dataType);
10561093
}
10571094

1095+
/**
1096+
* Return true if the property being passed is a C# value type
1097+
*
1098+
* @param var property
1099+
* @return true if property is a value type
1100+
*/
1101+
1102+
protected boolean isValueType(CodegenProperty var) {
1103+
return (valueTypes.contains(var.dataType) || var.isEnum ) ;
1104+
}
1105+
10581106
@Override
10591107
public void setParameterExampleValue(CodegenParameter codegenParameter) {
10601108

modules/openapi-generator/src/main/resources/aspnetcore/2.1/model.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ namespace {{modelPackage}}
8686
return {{#vars}}{{^isContainer}}
8787
(
8888
{{name}} == other.{{name}} ||
89-
{{name}} != null &&
89+
{{^vendorExtensions.isValueType}}{{name}} != null &&{{/vendorExtensions.isValueType}}
9090
{{name}}.Equals(other.{{name}})
9191
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}}
9292
(
9393
{{name}} == other.{{name}} ||
94-
{{name}} != null &&
94+
{{^vendorExtensions.isValueType}}{{name}} != null &&
9595
other.{{name}} != null &&
96-
{{name}}.SequenceEqual(other.{{name}})
96+
{{/vendorExtensions.isValueType}}{{name}}.SequenceEqual(other.{{name}})
9797
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}false{{/vars}};
9898
}
9999

@@ -108,7 +108,7 @@ namespace {{modelPackage}}
108108
var hashCode = 41;
109109
// Suitable nullity checks etc, of course :)
110110
{{#vars}}
111-
if ({{name}} != null)
111+
{{^vendorExtensions.isValueType}}if ({{name}} != null){{/vendorExtensions.isValueType}}
112112
hashCode = hashCode * 59 + {{name}}.GetHashCode();
113113
{{/vars}}
114114
return hashCode;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,19 @@
182182
return {{#vars}}{{#parent}}base.Equals(input) && {{/parent}}{{^isContainer}}
183183
(
184184
this.{{name}} == input.{{name}} ||
185-
{{^isEnum}}
185+
{{^vendorExtensions.isValueType}}
186186
(this.{{name}} != null &&
187187
this.{{name}}.Equals(input.{{name}}))
188-
{{/isEnum}}
189-
{{#isEnum}}
188+
{{/vendorExtensions.isValueType}}
189+
{{#vendorExtensions.isValueType}}
190190
this.{{name}}.Equals(input.{{name}})
191-
{{/isEnum}}
191+
{{/vendorExtensions.isValueType}}
192192
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}}
193193
(
194194
this.{{name}} == input.{{name}} ||
195-
this.{{name}} != null &&
195+
{{^vendorExtensions.isValueType}}this.{{name}} != null &&
196196
input.{{name}} != null &&
197-
this.{{name}}.SequenceEqual(input.{{name}})
197+
{{/vendorExtensions.isValueType}}this.{{name}}.SequenceEqual(input.{{name}})
198198
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}{{#parent}}base.Equals(input){{/parent}}{{^parent}}false{{/parent}}{{/vars}};
199199
{{/useCompareNetObjects}}
200200
}
@@ -214,13 +214,13 @@
214214
int hashCode = 41;
215215
{{/parent}}
216216
{{#vars}}
217-
{{^isEnum}}
217+
{{^vendorExtensions.isValueType}}
218218
if (this.{{name}} != null)
219219
hashCode = hashCode * 59 + this.{{name}}.GetHashCode();
220-
{{/isEnum}}
221-
{{#isEnum}}
220+
{{/vendorExtensions.isValueType}}
221+
{{#vendorExtensions.isValueType}}
222222
hashCode = hashCode * 59 + this.{{name}}.GetHashCode();
223-
{{/isEnum}}
223+
{{/vendorExtensions.isValueType}}
224224
{{/vars}}
225225
return hashCode;
226226
}

samples/client/petstore/csharp-netcore/OpenAPIClient/docs/EnumTest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Name | Type | Description | Notes
77
**EnumStringRequired** | **string** | |
88
**EnumInteger** | **int** | | [optional]
99
**EnumNumber** | **double** | | [optional]
10-
**OuterEnum** | [**OuterEnum**](OuterEnum.md) | | [optional]
10+
**OuterEnum** | **OuterEnum** | | [optional]
1111

1212
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1313

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/ApiResponse.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ public override int GetHashCode()
116116
unchecked // Overflow is fine, just wrap
117117
{
118118
int hashCode = 41;
119-
if (this.Code != null)
120-
hashCode = hashCode * 59 + this.Code.GetHashCode();
119+
hashCode = hashCode * 59 + this.Code.GetHashCode();
121120
if (this.Type != null)
122121
hashCode = hashCode * 59 + this.Type.GetHashCode();
123122
if (this.Message != null)

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Cat.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public override int GetHashCode()
106106
unchecked // Overflow is fine, just wrap
107107
{
108108
int hashCode = base.GetHashCode();
109-
if (this.Declawed != null)
110-
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
109+
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
111110
return hashCode;
112111
}
113112
}

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/CatAllOf.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public override int GetHashCode()
9898
unchecked // Overflow is fine, just wrap
9999
{
100100
int hashCode = 41;
101-
if (this.Declawed != null)
102-
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
101+
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
103102
return hashCode;
104103
}
105104
}

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Category.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public override int GetHashCode()
121121
unchecked // Overflow is fine, just wrap
122122
{
123123
int hashCode = 41;
124-
if (this.Id != null)
125-
hashCode = hashCode * 59 + this.Id.GetHashCode();
124+
hashCode = hashCode * 59 + this.Id.GetHashCode();
126125
if (this.Name != null)
127126
hashCode = hashCode * 59 + this.Name.GetHashCode();
128127
return hashCode;

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/EnumTest.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public enum EnumNumberEnum
142142
[DataMember(Name="enum_number", EmitDefaultValue=false)]
143143
public EnumNumberEnum? EnumNumber { get; set; }
144144
/// <summary>
145+
/// Gets or Sets OuterEnum
146+
/// </summary>
147+
[DataMember(Name="outerEnum", EmitDefaultValue=false)]
148+
public OuterEnum? OuterEnum { get; set; }
149+
/// <summary>
145150
/// Initializes a new instance of the <see cref="EnumTest" /> class.
146151
/// </summary>
147152
[JsonConstructorAttribute]
@@ -163,12 +168,6 @@ protected EnumTest() { }
163168
this.OuterEnum = outerEnum;
164169
}
165170

166-
/// <summary>
167-
/// Gets or Sets OuterEnum
168-
/// </summary>
169-
[DataMember(Name="outerEnum", EmitDefaultValue=false)]
170-
public OuterEnum OuterEnum { get; set; }
171-
172171
/// <summary>
173172
/// Returns the string presentation of the object
174173
/// </summary>
@@ -228,8 +227,7 @@ public override int GetHashCode()
228227
hashCode = hashCode * 59 + this.EnumStringRequired.GetHashCode();
229228
hashCode = hashCode * 59 + this.EnumInteger.GetHashCode();
230229
hashCode = hashCode * 59 + this.EnumNumber.GetHashCode();
231-
if (this.OuterEnum != null)
232-
hashCode = hashCode * 59 + this.OuterEnum.GetHashCode();
230+
hashCode = hashCode * 59 + this.OuterEnum.GetHashCode();
233231
return hashCode;
234232
}
235233
}

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/FormatTest.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,12 @@ public override int GetHashCode()
248248
unchecked // Overflow is fine, just wrap
249249
{
250250
int hashCode = 41;
251-
if (this.Integer != null)
252-
hashCode = hashCode * 59 + this.Integer.GetHashCode();
253-
if (this.Int32 != null)
254-
hashCode = hashCode * 59 + this.Int32.GetHashCode();
255-
if (this.Int64 != null)
256-
hashCode = hashCode * 59 + this.Int64.GetHashCode();
257-
if (this.Number != null)
258-
hashCode = hashCode * 59 + this.Number.GetHashCode();
259-
if (this.Float != null)
260-
hashCode = hashCode * 59 + this.Float.GetHashCode();
261-
if (this.Double != null)
262-
hashCode = hashCode * 59 + this.Double.GetHashCode();
251+
hashCode = hashCode * 59 + this.Integer.GetHashCode();
252+
hashCode = hashCode * 59 + this.Int32.GetHashCode();
253+
hashCode = hashCode * 59 + this.Int64.GetHashCode();
254+
hashCode = hashCode * 59 + this.Number.GetHashCode();
255+
hashCode = hashCode * 59 + this.Float.GetHashCode();
256+
hashCode = hashCode * 59 + this.Double.GetHashCode();
263257
if (this.String != null)
264258
hashCode = hashCode * 59 + this.String.GetHashCode();
265259
if (this.Byte != null)

0 commit comments

Comments
 (0)