Skip to content

Commit 23a2aaf

Browse files
[CSHARP] Fix how the array type is set when using NULLABLE_REFERENCE_TYPES (#22071)
* Fix how the type is calculated for deep inline arrays * Add list alias objects to petstore specification and regenerate samples
1 parent 2afe7d2 commit 23a2aaf

File tree

22 files changed

+249
-6
lines changed

22 files changed

+249
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ public String getTypeDeclaration(Schema p) {
16281628
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
16291629
if (ModelUtils.isArraySchema(target)) {
16301630
Schema<?> items = getSchemaItems(schema);
1631-
return getSchemaType(target) + "<" + getTypeDeclarationForArray(items) + ">";
1631+
return typeMapping.get("array") + "<" + getTypeDeclarationForArray(items) + ">";
16321632
} else if (ModelUtils.isMapSchema(p)) {
16331633
// Should we also support maps of maps?
16341634
Schema<?> inner = ModelUtils.getAdditionalProperties(p);

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,27 @@ public void nullablePropertyWithoutNullableReferenceTypesTest() {
368368
public void nullablePropertyWithNullableReferenceTypesTest() {
369369
final Schema model = new Schema()
370370
.description("a sample model")
371-
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT).nullable(true))
371+
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)
372+
.nullable(true))
372373
.addProperties("urls", new ArraySchema()
373-
.items(new StringSchema()).nullable(true))
374+
.items(new StringSchema())
375+
.nullable(true))
374376
.addProperties("name", new StringSchema().nullable(true))
375-
.addProperties("subObject", new Schema().addProperties("name", new StringSchema()).nullable(true))
377+
.addProperties("subObject", new Schema().addProperties("name", new StringSchema())
378+
.nullable(true))
379+
.addProperties("deepNullableAliasArray", new ArraySchema()
380+
.items(new ArraySchema()
381+
.items(new StringSchema()
382+
.nullable(true))
383+
.nullable(true))
384+
.nullable(true))
385+
.addProperties("deepAliasArray", new ArraySchema()
386+
.items(new ArraySchema()
387+
.items(new StringSchema())))
388+
.addProperties("deepIntermediateNullableAliasArray", new ArraySchema()
389+
.items(new ArraySchema()
390+
.items(new StringSchema())
391+
.nullable(true)))
376392
.addRequiredItem("id");
377393
final DefaultCodegen codegen = new AspNetServerCodegen();
378394
codegen.processOpts();
@@ -385,7 +401,7 @@ public void nullablePropertyWithNullableReferenceTypesTest() {
385401
Assert.assertEquals(cm.name, "sample");
386402
Assert.assertEquals(cm.classname, "Sample");
387403
Assert.assertEquals(cm.description, "a sample model");
388-
Assert.assertEquals(cm.vars.size(), 4);
404+
Assert.assertEquals(cm.vars.size(), 7);
389405

390406
final CodegenProperty property1 = cm.vars.get(0);
391407
Assert.assertEquals(property1.baseName, "id");
@@ -398,7 +414,7 @@ public void nullablePropertyWithNullableReferenceTypesTest() {
398414

399415
final CodegenProperty property2 = cm.vars.get(1);
400416
Assert.assertEquals(property2.baseName, "urls");
401-
Assert.assertEquals(property2.dataType, "List?<string>");
417+
Assert.assertEquals(property2.dataType, "List<string>");
402418
Assert.assertEquals(property2.name, "Urls");
403419
Assert.assertNull(property2.defaultValue);
404420
Assert.assertEquals(property2.baseType, "List?");
@@ -424,6 +440,33 @@ public void nullablePropertyWithNullableReferenceTypesTest() {
424440
Assert.assertEquals(property4.baseType, "Object?");
425441
Assert.assertFalse(property4.required);
426442
Assert.assertFalse(property4.isPrimitiveType);
443+
444+
final CodegenProperty property5 = cm.vars.get(4);
445+
Assert.assertEquals(property5.baseName, "deepNullableAliasArray");
446+
Assert.assertEquals(property5.dataType, "List<List<string?>>");
447+
Assert.assertEquals(property5.name, "DeepNullableAliasArray");
448+
Assert.assertNull(property5.defaultValue);
449+
Assert.assertEquals(property5.baseType, "List?");
450+
Assert.assertEquals(property5.containerType, "array");
451+
Assert.assertFalse(property5.required);
452+
Assert.assertFalse(property5.isPrimitiveType);
453+
Assert.assertTrue(property5.isContainer);
454+
455+
final CodegenProperty property6 = cm.vars.get(5);
456+
Assert.assertEquals(property6.baseName, "deepAliasArray");
457+
Assert.assertEquals(property6.dataType, "List<List<string>>");
458+
Assert.assertEquals(property6.name, "DeepAliasArray");
459+
Assert.assertEquals(property6.baseType, "List");
460+
Assert.assertEquals(property6.containerType, "array");
461+
Assert.assertTrue(property6.isContainer);
462+
463+
final CodegenProperty property7 = cm.vars.get(6);
464+
Assert.assertEquals(property7.baseName, "deepIntermediateNullableAliasArray");
465+
Assert.assertEquals(property7.dataType, "List<List<string>>");
466+
Assert.assertEquals(property7.name, "DeepIntermediateNullableAliasArray");
467+
Assert.assertEquals(property7.baseType, "List");
468+
Assert.assertEquals(property7.containerType, "array");
469+
Assert.assertTrue(property7.isContainer);
427470
}
428471

429472
@Test(description = "convert a model with list property")

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,6 +2935,16 @@ components:
29352935
description: list of named parameters for current message
29362936
additionalProperties:
29372937
type: string
2938+
ListAlias:
2939+
type: array
2940+
items:
2941+
type: string
2942+
DeepListAlias:
2943+
type: array
2944+
items:
2945+
type: array
2946+
items:
2947+
type: string
29382948
TestResult:
29392949
type: object
29402950
allOf:

samples/client/petstore/csharp/generichost/net4.7/FormModels/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,16 @@ components:
26872687
description: list of named parameters for current message
26882688
type: object
26892689
type: object
2690+
ListAlias:
2691+
items:
2692+
type: string
2693+
type: array
2694+
DeepListAlias:
2695+
items:
2696+
items:
2697+
type: string
2698+
type: array
2699+
type: array
26902700
TestResult:
26912701
allOf:
26922702
- $ref: "#/components/schemas/Result"

samples/client/petstore/csharp/generichost/net4.7/Petstore/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,16 @@ components:
28682868
description: list of named parameters for current message
28692869
type: object
28702870
type: object
2871+
ListAlias:
2872+
items:
2873+
type: string
2874+
type: array
2875+
DeepListAlias:
2876+
items:
2877+
items:
2878+
type: string
2879+
type: array
2880+
type: array
28712881
TestResult:
28722882
allOf:
28732883
- $ref: "#/components/schemas/Result"

samples/client/petstore/csharp/generichost/net4.8/FormModels/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,16 @@ components:
26872687
description: list of named parameters for current message
26882688
type: object
26892689
type: object
2690+
ListAlias:
2691+
items:
2692+
type: string
2693+
type: array
2694+
DeepListAlias:
2695+
items:
2696+
items:
2697+
type: string
2698+
type: array
2699+
type: array
26902700
TestResult:
26912701
allOf:
26922702
- $ref: "#/components/schemas/Result"

samples/client/petstore/csharp/generichost/net4.8/Petstore/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,16 @@ components:
28682868
description: list of named parameters for current message
28692869
type: object
28702870
type: object
2871+
ListAlias:
2872+
items:
2873+
type: string
2874+
type: array
2875+
DeepListAlias:
2876+
items:
2877+
items:
2878+
type: string
2879+
type: array
2880+
type: array
28712881
TestResult:
28722882
allOf:
28732883
- $ref: "#/components/schemas/Result"

samples/client/petstore/csharp/generichost/net8/FormModels/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,16 @@ components:
26872687
description: list of named parameters for current message
26882688
type: object
26892689
type: object
2690+
ListAlias:
2691+
items:
2692+
type: string
2693+
type: array
2694+
DeepListAlias:
2695+
items:
2696+
items:
2697+
type: string
2698+
type: array
2699+
type: array
26902700
TestResult:
26912701
allOf:
26922702
- $ref: "#/components/schemas/Result"

samples/client/petstore/csharp/generichost/net8/NullReferenceTypes/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,16 @@ components:
28682868
description: list of named parameters for current message
28692869
type: object
28702870
type: object
2871+
ListAlias:
2872+
items:
2873+
type: string
2874+
type: array
2875+
DeepListAlias:
2876+
items:
2877+
items:
2878+
type: string
2879+
type: array
2880+
type: array
28712881
TestResult:
28722882
allOf:
28732883
- $ref: "#/components/schemas/Result"

samples/client/petstore/csharp/generichost/net8/Petstore/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,16 @@ components:
28682868
description: list of named parameters for current message
28692869
type: object
28702870
type: object
2871+
ListAlias:
2872+
items:
2873+
type: string
2874+
type: array
2875+
DeepListAlias:
2876+
items:
2877+
items:
2878+
type: string
2879+
type: array
2880+
type: array
28712881
TestResult:
28722882
allOf:
28732883
- $ref: "#/components/schemas/Result"

0 commit comments

Comments
 (0)