Skip to content

Commit 848e15a

Browse files
committed
[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
1 parent 2917ce8 commit 848e15a

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ public String getTypeDeclaration(Schema p) {
753753
Schema inner = ModelUtils.getSchemaItems(p);
754754
return ModelUtils.isSet(p) ? "Set<" + getTypeDeclaration(inner) + ">" : "[" + getTypeDeclaration(inner) + "]";
755755
} else if (ModelUtils.isMapSchema(p)) {
756-
Schema inner = ModelUtils.getAdditionalProperties(p);
756+
Schema inner = unaliasSchema(ModelUtils.getAdditionalProperties(p));
757757
return "[String: " + getItemsTypeDeclaration(inner) + "]";
758758
}
759759
return super.getTypeDeclaration(p);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
import org.openapitools.codegen.DefaultCodegen;
2626
import org.openapitools.codegen.TestUtils;
2727
import org.openapitools.codegen.languages.Swift6ClientCodegen;
28+
import org.openapitools.codegen.utils.ModelUtils;
2829
import org.testng.Assert;
2930
import org.testng.annotations.Test;
3031

32+
import java.util.Map;
33+
3134
@SuppressWarnings("static-method")
3235
public class Swift6ClientCodegenModelTest {
3336

@@ -163,4 +166,26 @@ public void useCustomDateTimeTest() {
163166
Assert.assertFalse(property7.isContainer);
164167
}
165168

169+
@Test(description = "nested map via $ref should resolve to [String: [String: String]], not [String: Dictionary]", enabled = true)
170+
public void nestedMapRefTypeTest() {
171+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/swift6_nested_map.yaml");
172+
final Swift6ClientCodegen codegen = new Swift6ClientCodegen();
173+
codegen.setOpenAPI(openAPI);
174+
codegen.processOpts();
175+
176+
Map<String, Schema> schemas = ModelUtils.getSchemas(openAPI);
177+
Schema emailTemplateSchema = schemas.get("EmailTemplate");
178+
Assert.assertNotNull(emailTemplateSchema, "EmailTemplate schema should exist");
179+
180+
final CodegenModel cm = codegen.fromModel("EmailTemplate", emailTemplateSchema);
181+
182+
CodegenProperty translationProp = cm.vars.stream()
183+
.filter(p -> p.baseName.equals("translationOverridesByLocale"))
184+
.findFirst().orElse(null);
185+
Assert.assertNotNull(translationProp, "translationOverridesByLocale property should exist");
186+
// Must be [String: [String: String]], NOT [String: Dictionary]
187+
Assert.assertEquals(translationProp.dataType, "[String: [String: String]]",
188+
"Nested map via $ref should resolve to [String: [String: String]]");
189+
}
190+
166191
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Nested Map Type Test
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
StringMap:
9+
type: object
10+
additionalProperties:
11+
type: string
12+
description: A map of string to string
13+
EmailTemplate:
14+
type: object
15+
required:
16+
- id
17+
- ejs
18+
properties:
19+
id:
20+
type: string
21+
ejs:
22+
type: string
23+
translationOverridesByLocale:
24+
$ref: '#/components/schemas/NestedStringMap'
25+
NestedStringMap:
26+
type: object
27+
additionalProperties:
28+
$ref: '#/components/schemas/StringMap'
29+
description: A map of string to map of string to string

0 commit comments

Comments
 (0)