Skip to content

Commit 475d8e9

Browse files
committed
adjusted csharp codegen to allow for nullable properties (on value types only unless nullable reference types is enabled).
Edited tests to compensate for the actual data types. They were optional/not-required in the yaml, but came back with non-nullable types.
1 parent 045a9c1 commit 475d8e9

537 files changed

Lines changed: 5972 additions & 1806 deletions

File tree

Some content is hidden

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,15 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
487487
}
488488

489489
Double maximum = asDouble(property.maximum);
490-
if (property.dataType.equals("int") && maximum != null) {
490+
if (property.dataType.startsWith("int") && maximum != null) {
491491
if ((!property.exclusiveMaximum && asInteger(property.maximum) == null) || (property.exclusiveMaximum && asInteger((maximum + 1) + "") == null)) {
492492
property.dataType = "long";
493493
property.datatypeWithEnum = "long";
494494
}
495495
}
496496

497497
Double minimum = asDouble(property.minimum);
498-
if (property.dataType.equals("int") && minimum != null) {
498+
if (property.dataType.startsWith("int") && minimum != null) {
499499
if ((!property.exclusiveMinimum && asInteger(property.minimum) == null) || (property.exclusiveMinimum && asInteger((minimum - 1) + "") == null)) {
500500
property.dataType = "long";
501501
property.datatypeWithEnum = "long";

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,10 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
674674
postProcessEmitDefaultValue(property.vendorExtensions);
675675

676676
super.postProcessModelProperty(model, property);
677+
678+
if (!GENERICHOST.equals(getLibrary()) && !property.dataType.endsWith("?") && !property.required && property.defaultValue == null && (this.getValueTypes().contains(property.dataType) || (nullReferenceTypesFlag && !property.isArray && !property.isMap))) {
679+
property.dataType = property.dataType + "?";
680+
}
677681
}
678682

679683
@Override

modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpClientCodegenTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testUnsigned() {
6868

6969
final CodegenProperty property1 = cm1.allVars.get(2);
7070
Assert.assertEquals(property1.baseName, "unsigned_integer");
71-
Assert.assertEquals(property1.dataType, "uint");
71+
Assert.assertEquals(property1.dataType, "uint?");
7272
Assert.assertEquals(property1.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
7373
Assert.assertTrue(property1.isPrimitiveType);
7474
Assert.assertTrue(property1.isInteger);
@@ -78,7 +78,7 @@ public void testUnsigned() {
7878

7979
final CodegenProperty property2 = cm1.allVars.get(4);
8080
Assert.assertEquals(property2.baseName, "unsigned_long");
81-
Assert.assertEquals(property2.dataType, "ulong");
81+
Assert.assertEquals(property2.dataType, "ulong?");
8282
Assert.assertEquals(property2.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
8383
Assert.assertTrue(property2.isPrimitiveType);
8484
Assert.assertTrue(property2.isLong);

modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpModelTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public void simpleModelTest() {
195195

196196
final CodegenProperty property3 = cm.vars.get(2);
197197
Assert.assertEquals(property3.baseName, "createdAt");
198-
Assert.assertEquals(property3.dataType, "DateTime");
198+
Assert.assertEquals(property3.dataType, "DateTime?");
199199
Assert.assertEquals(property3.name, "CreatedAt");
200200
Assert.assertNull(property3.defaultValue);
201201
Assert.assertEquals(property3.baseType, "DateTime");
@@ -245,7 +245,7 @@ public void nonNullablePropertyTest() {
245245

246246
final CodegenProperty property3 = cm.vars.get(2);
247247
Assert.assertEquals(property3.baseName, "name");
248-
Assert.assertEquals(property3.dataType, "string");
248+
Assert.assertEquals(property3.dataType, "string?");
249249
Assert.assertEquals(property3.name, "Name");
250250
Assert.assertNull(property3.defaultValue);
251251
Assert.assertEquals(property3.baseType, "string");
@@ -296,7 +296,7 @@ public void nullablePropertyTest() {
296296

297297
final CodegenProperty property3 = cm.vars.get(2);
298298
Assert.assertEquals(property3.baseName, "name");
299-
Assert.assertEquals(property3.dataType, "string");
299+
Assert.assertEquals(property3.dataType, "string?");
300300
Assert.assertEquals(property3.name, "Name");
301301
Assert.assertNull(property3.defaultValue);
302302
Assert.assertEquals(property3.baseType, "string");
@@ -516,7 +516,7 @@ public void complexPropertyTest() {
516516

517517
final CodegenProperty property1 = cm.vars.get(0);
518518
Assert.assertEquals(property1.baseName, "children");
519-
Assert.assertEquals(property1.dataType, "Children");
519+
Assert.assertEquals(property1.dataType, "Children?");
520520
Assert.assertEquals(property1.name, "Children");
521521
Assert.assertEquals(property1.baseType, "Children");
522522
Assert.assertFalse(property1.required);

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/Bird.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**Size** | **string** | | [optional]
8-
**Color** | **string** | | [optional]
7+
**Size** | **string?** | | [optional]
8+
**Color** | **string?** | | [optional]
99

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

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/Category.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**Id** | **long** | | [optional]
8-
**Name** | **string** | | [optional]
7+
**Id** | **long?** | | [optional]
8+
**Name** | **string?** | | [optional]
99

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

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/DataQuery.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**Id** | **long** | Query | [optional]
7+
**Id** | **long?** | Query | [optional]
88
**Outcomes** | **List<Query.OutcomesEnum>** | | [optional]
9-
**Suffix** | **string** | test suffix | [optional]
10-
**Text** | **string** | Some text containing white spaces | [optional]
11-
**Date** | **DateTime** | A date | [optional]
9+
**Suffix** | **string?** | test suffix | [optional]
10+
**Text** | **string?** | Some text containing white spaces | [optional]
11+
**Date** | **DateTime?** | A date | [optional]
1212

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

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/DefaultValue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Name | Type | Description | Notes
1212
**ArrayString** | **List<string>** | | [optional]
1313
**ArrayStringNullable** | **List<string>** | | [optional]
1414
**ArrayStringExtensionNullable** | **List<string>** | | [optional]
15-
**StringNullable** | **string** | | [optional]
15+
**StringNullable** | **string?** | | [optional]
1616

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

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/NumberPropertiesOnly.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**Number** | **decimal** | | [optional]
8-
**Float** | **float** | | [optional]
9-
**Double** | **double** | | [optional]
7+
**Number** | **decimal?** | | [optional]
8+
**Float** | **float?** | | [optional]
9+
**Double** | **double?** | | [optional]
1010

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

samples/client/echo_api/csharp/restsharp/net8/EchoApi/docs/Pet.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**Id** | **long** | | [optional]
7+
**Id** | **long?** | | [optional]
88
**Name** | **string** | |
9-
**Category** | [**Category**](Category.md) | | [optional]
9+
**Category** | [**Category?**](Category.md) | | [optional]
1010
**PhotoUrls** | **List<string>** | |
1111
**Tags** | [**List<Tag>**](Tag.md) | | [optional]
12-
**Status** | **string** | pet status in the store | [optional]
12+
**Status** | **string?** | pet status in the store | [optional]
1313

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

0 commit comments

Comments
 (0)