Skip to content

Commit 99d050f

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 e7b5f34 commit 99d050f

File tree

537 files changed

+2905
-1862
lines changed

Some content is hidden

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

537 files changed

+2905
-1862
lines changed

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
@@ -488,15 +488,15 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
488488
}
489489

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

498498
Double minimum = asDouble(property.minimum);
499-
if (property.dataType.equals("int") && minimum != null) {
499+
if (property.dataType.startsWith("int") && minimum != null) {
500500
if ((!property.exclusiveMinimum && asInteger(property.minimum) == null) || (property.exclusiveMinimum && asInteger((minimum - 1) + "") == null)) {
501501
property.dataType = "long";
502502
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
@@ -669,6 +669,10 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
669669
postProcessEmitDefaultValue(property.vendorExtensions);
670670

671671
super.postProcessModelProperty(model, property);
672+
673+
if (!GENERICHOST.equals(getLibrary()) && !property.dataType.endsWith("?") && !property.required && property.defaultValue == null && (this.getValueTypes().contains(property.dataType) || (nullReferenceTypesFlag && !property.isArray && !property.isMap))) {
674+
property.dataType = property.dataType + "?";
675+
}
672676
}
673677

674678
@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
@@ -72,7 +72,7 @@ public void testUnsigned() {
7272

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

8383
final CodegenProperty property2 = cm1.allVars.get(4);
8484
Assert.assertEquals(property2.baseName, "unsigned_long");
85-
Assert.assertEquals(property2.dataType, "ulong");
85+
Assert.assertEquals(property2.dataType, "ulong?");
8686
Assert.assertEquals(property2.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
8787
Assert.assertTrue(property2.isPrimitiveType);
8888
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
@@ -194,7 +194,7 @@ public void simpleModelTest() {
194194

195195
final CodegenProperty property3 = cm.vars.get(2);
196196
Assert.assertEquals(property3.baseName, "createdAt");
197-
Assert.assertEquals(property3.dataType, "DateTime");
197+
Assert.assertEquals(property3.dataType, "DateTime?");
198198
Assert.assertEquals(property3.name, "CreatedAt");
199199
Assert.assertNull(property3.defaultValue);
200200
Assert.assertEquals(property3.baseType, "DateTime");
@@ -243,7 +243,7 @@ public void nonNullablePropertyTest() {
243243

244244
final CodegenProperty property3 = cm.vars.get(2);
245245
Assert.assertEquals(property3.baseName, "name");
246-
Assert.assertEquals(property3.dataType, "string");
246+
Assert.assertEquals(property3.dataType, "string?");
247247
Assert.assertEquals(property3.name, "Name");
248248
Assert.assertNull(property3.defaultValue);
249249
Assert.assertEquals(property3.baseType, "string");
@@ -293,7 +293,7 @@ public void nullablePropertyTest() {
293293

294294
final CodegenProperty property3 = cm.vars.get(2);
295295
Assert.assertEquals(property3.baseName, "name");
296-
Assert.assertEquals(property3.dataType, "string");
296+
Assert.assertEquals(property3.dataType, "string?");
297297
Assert.assertEquals(property3.name, "Name");
298298
Assert.assertNull(property3.defaultValue);
299299
Assert.assertEquals(property3.baseType, "string");
@@ -510,7 +510,7 @@ public void complexPropertyTest() {
510510

511511
final CodegenProperty property1 = cm.vars.get(0);
512512
Assert.assertEquals(property1.baseName, "children");
513-
Assert.assertEquals(property1.dataType, "Children");
513+
Assert.assertEquals(property1.dataType, "Children?");
514514
Assert.assertEquals(property1.name, "Children");
515515
Assert.assertEquals(property1.baseType, "Children");
516516
Assert.assertFalse(property1.required);

samples/client/echo_api/csharp-restsharp/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/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/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/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/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/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)