From 848e15ae04e019d1ef64ff21b9c3e277ded866a8 Mon Sep 17 00:00:00 2001 From: winrid Date: Thu, 30 Apr 2026 15:03:09 -0700 Subject: [PATCH] [BUG][SWIFT6] unalias additionalProperties before building map type declaration A nested map property whose additionalProperties is a $ref to an aliased map schema (e.g. StringMap = type: object, additionalProperties: { type: string }) resolved to `[String: Dictionary]` (no generic args, doesn't compile in Swift 6) instead of `[String: [String: String]]`. Wrap ModelUtils.getAdditionalProperties(p) with unaliasSchema(...) in getTypeDeclaration so the alias resolves to the actual schema before recursion. Closes #23666 --- .../languages/Swift6ClientCodegen.java | 2 +- .../swift6/Swift6ClientCodegenModelTest.java | 25 ++++++++++++++++ .../test/resources/3_0/swift6_nested_map.yaml | 29 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/swift6_nested_map.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java index 1816801476bf..f86b96ae479d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java @@ -753,7 +753,7 @@ public String getTypeDeclaration(Schema p) { Schema inner = ModelUtils.getSchemaItems(p); return ModelUtils.isSet(p) ? "Set<" + getTypeDeclaration(inner) + ">" : "[" + getTypeDeclaration(inner) + "]"; } else if (ModelUtils.isMapSchema(p)) { - Schema inner = ModelUtils.getAdditionalProperties(p); + Schema inner = unaliasSchema(ModelUtils.getAdditionalProperties(p)); return "[String: " + getItemsTypeDeclaration(inner) + "]"; } return super.getTypeDeclaration(p); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift6/Swift6ClientCodegenModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift6/Swift6ClientCodegenModelTest.java index 9de8e91b8caa..32de36955ea6 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift6/Swift6ClientCodegenModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift6/Swift6ClientCodegenModelTest.java @@ -25,9 +25,12 @@ import org.openapitools.codegen.DefaultCodegen; import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.languages.Swift6ClientCodegen; +import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; +import java.util.Map; + @SuppressWarnings("static-method") public class Swift6ClientCodegenModelTest { @@ -163,4 +166,26 @@ public void useCustomDateTimeTest() { Assert.assertFalse(property7.isContainer); } + @Test(description = "nested map via $ref should resolve to [String: [String: String]], not [String: Dictionary]", enabled = true) + public void nestedMapRefTypeTest() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/swift6_nested_map.yaml"); + final Swift6ClientCodegen codegen = new Swift6ClientCodegen(); + codegen.setOpenAPI(openAPI); + codegen.processOpts(); + + Map schemas = ModelUtils.getSchemas(openAPI); + Schema emailTemplateSchema = schemas.get("EmailTemplate"); + Assert.assertNotNull(emailTemplateSchema, "EmailTemplate schema should exist"); + + final CodegenModel cm = codegen.fromModel("EmailTemplate", emailTemplateSchema); + + CodegenProperty translationProp = cm.vars.stream() + .filter(p -> p.baseName.equals("translationOverridesByLocale")) + .findFirst().orElse(null); + Assert.assertNotNull(translationProp, "translationOverridesByLocale property should exist"); + // Must be [String: [String: String]], NOT [String: Dictionary] + Assert.assertEquals(translationProp.dataType, "[String: [String: String]]", + "Nested map via $ref should resolve to [String: [String: String]]"); + } + } diff --git a/modules/openapi-generator/src/test/resources/3_0/swift6_nested_map.yaml b/modules/openapi-generator/src/test/resources/3_0/swift6_nested_map.yaml new file mode 100644 index 000000000000..1f1162d141b8 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/swift6_nested_map.yaml @@ -0,0 +1,29 @@ +openapi: 3.0.0 +info: + title: Nested Map Type Test + version: 1.0.0 +paths: {} +components: + schemas: + StringMap: + type: object + additionalProperties: + type: string + description: A map of string to string + EmailTemplate: + type: object + required: + - id + - ejs + properties: + id: + type: string + ejs: + type: string + translationOverridesByLocale: + $ref: '#/components/schemas/NestedStringMap' + NestedStringMap: + type: object + additionalProperties: + $ref: '#/components/schemas/StringMap' + description: A map of string to map of string to string