Skip to content

Commit 153ac7b

Browse files
authored
[swift6] fix map nullable values (#17996) (#23629)
Swift6 generator ignores `nullable: true` on `additionalProperties` values, producing `[String: String]` instead of `[String: String?]`. This causes `DecodingError` at runtime when the JSON payload contains null values inside dictionaries. Mirrors the fix applied to the Kotlin generator in #23074.
1 parent 6134e8e commit 153ac7b

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,17 @@ public String getTypeDeclaration(Schema p) {
754754
return ModelUtils.isSet(p) ? "Set<" + getTypeDeclaration(inner) + ">" : "[" + getTypeDeclaration(inner) + "]";
755755
} else if (ModelUtils.isMapSchema(p)) {
756756
Schema inner = ModelUtils.getAdditionalProperties(p);
757-
return "[String: " + getTypeDeclaration(inner) + "]";
757+
return "[String: " + getItemsTypeDeclaration(inner) + "]";
758758
}
759759
return super.getTypeDeclaration(p);
760760
}
761761

762+
private String getItemsTypeDeclaration(Schema items) {
763+
String itemsTypeDeclaration = getTypeDeclaration(items);
764+
String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
765+
return itemsTypeDeclaration + nullable;
766+
}
767+
762768
@Override
763769
public String getSchemaType(Schema p) {
764770
String openAPIType = super.getSchemaType(p);

modules/openapi-generator/src/test/java/org/openapitools/codegen/swift6/Swift6ClientCodegenTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.swagger.v3.oas.models.OpenAPI;
2121
import io.swagger.v3.oas.models.Operation;
22+
import io.swagger.v3.oas.models.media.Schema;
2223
import org.openapitools.codegen.*;
2324
import org.openapitools.codegen.config.CodegenConfigurator;
2425
import org.openapitools.codegen.languages.Swift6ClientCodegen;
@@ -401,4 +402,20 @@ public void oneOfDiscriminatorFirstDecodingTest() throws IOException {
401402
output.deleteOnExit();
402403
}
403404
}
405+
406+
@Test(description = "Issue #17996")
407+
public void testNullableMap() {
408+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/swift6/issue17996-nullable-map.yaml");
409+
410+
Schema test1 = openAPI.getComponents().getSchemas().get("NullMapNotNullMap");
411+
CodegenModel cm1 = swiftCodegen.fromModel("NullMapNotNullMap", test1);
412+
413+
// Assert the dataType properly generated
414+
CodegenProperty nullableMap = cm1.vars.get(0);
415+
CodegenProperty notNullableMap = cm1.vars.get(1);
416+
CodegenProperty defaultMap = cm1.vars.get(2);
417+
Assert.assertEquals(nullableMap.getDataType(), "[String: String?]");
418+
Assert.assertEquals(notNullableMap.getDataType(), "[String: String]");
419+
Assert.assertEquals(defaultMap.getDataType(), "[String: String]");
420+
}
404421
}
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 17996 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)