Skip to content

Commit 1283d1b

Browse files
[kotlin-spring] fix kotlin map nullable (fix #16501) (#23074)
* [kotlin-spring] fix kotlin map nullable * [kotlin-spring] fix kotlin map nullable add test
1 parent cbdee19 commit 1283d1b

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public String getTypeDeclaration(Schema p) {
394394
inner = new StringSchema().description("TODO default missing map inner type to string");
395395
p.setAdditionalProperties(inner);
396396
}
397-
return getSchemaType(target) + "<kotlin.String, " + getTypeDeclaration(inner) + ">";
397+
return getSchemaType(target) + "<kotlin.String, " + getItemsTypeDeclaration(inner) + ">";
398398
}
399399
return super.getTypeDeclaration(target);
400400
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ public void handleInheritanceWithObjectTypeShouldNotBeAMap() {
383383
Assert.assertTrue(mapSchemaModel.isMap);
384384
}
385385

386+
@Test(description = "Issue #16501")
387+
public void testNullableMap() {
388+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/kotlin/issue16501-nullable-map.yaml");
389+
390+
Schema test1 = openAPI.getComponents().getSchemas().get("NullMapNotNullMap");
391+
CodegenModel cm1 = codegen.fromModel("NullMapNotNullMap", test1);
392+
393+
codegen.postProcessModels(createCodegenModelWrapper(cm1));
394+
395+
// Assert the dataType properly generated
396+
CodegenProperty nullableMap = cm1.vars.get(0);
397+
CodegenProperty notNullableMap = cm1.vars.get(1);
398+
CodegenProperty defaultMap = cm1.vars.get(2);
399+
Assert.assertEquals(nullableMap.getDataType(), "kotlin.collections.Map<kotlin.String, kotlin.String?>");
400+
Assert.assertEquals(notNullableMap.getDataType(), "kotlin.collections.Map<kotlin.String, kotlin.String>");
401+
Assert.assertEquals(defaultMap.getDataType(), "kotlin.collections.Map<kotlin.String, kotlin.String>");
402+
}
403+
386404
@Test
387405
public void handleUseJakartaEeTrue() {
388406
codegen.additionalProperties().put("useJakartaEe", true);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.0
2+
info:
3+
title: 'Issue 16501 Nullable map'
4+
version: latest
5+
components:
6+
schemas:
7+
NullMapNotNullMap:
8+
properties:
9+
nullableMap:
10+
type: object
11+
additionalProperties:
12+
type: string
13+
nullable: true
14+
notNullableMap:
15+
type: object
16+
additionalProperties:
17+
type: string
18+
nullable: false
19+
defaultMap:
20+
type: object
21+
additionalProperties:
22+
type: string

0 commit comments

Comments
 (0)