Skip to content

Commit 7089435

Browse files
committed
Make sure to respect inlineSchemaNameMapping
1 parent ed15fee commit 7089435

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ private void flattenComposedChildren(String key, List<Schema> children, boolean
667667
listIterator.set(schema);
668668
} else {
669669
Schema schema = new Schema().$ref(existing);
670-
schema.addExtension("x-alias-name", innerModelName);
670+
schema.addExtension("x-alias-name", applyInlineSchemaNameMapping(innerModelName));
671671
schema.setRequired(component.getRequired());
672672
listIterator.set(schema);
673673
}
@@ -827,7 +827,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
827827
String existing = matchGenerated(model);
828828
if (existing != null) {
829829
Schema schema = new Schema().$ref(existing);
830-
schema.addExtension("x-alias-name", modelName);
830+
schema.addExtension("x-alias-name", applyInlineSchemaNameMapping(modelName));
831831
schema.setRequired(op.getRequired());
832832
propsToUpdate.put(key, schema);
833833
} else {
@@ -848,7 +848,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
848848
String existing = matchGenerated(innerModel);
849849
if (existing != null) {
850850
Schema schema = new Schema().$ref(existing);
851-
schema.addExtension("x-alias-name", modelName);
851+
schema.addExtension("x-alias-name", applyInlineSchemaNameMapping(modelName));
852852
schema.setRequired(op.getRequired());
853853
property.setItems(schema);
854854
} else {
@@ -879,7 +879,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
879879
String existing = matchGenerated(innerModel);
880880
if (existing != null) {
881881
Schema schema = new Schema().$ref(existing);
882-
schema.addExtension("x-alias-name", modelName);
882+
schema.addExtension("x-alias-name", applyInlineSchemaNameMapping(modelName));
883883
schema.setRequired(op.getRequired());
884884
property.setAdditionalProperties(schema);
885885
} else {
@@ -989,6 +989,19 @@ private Schema modelFromProperty(OpenAPI openAPI, Schema object, String path) {
989989
return model;
990990
}
991991

992+
/**
993+
* Apply inlineSchemaNameMapping if configured.
994+
*
995+
* @param name the inline schema name to map
996+
* @return the mapped name if mapping exists, otherwise the original name
997+
*/
998+
private String applyInlineSchemaNameMapping(String name) {
999+
if (inlineSchemaNameMapping.containsKey(name)) {
1000+
return inlineSchemaNameMapping.get(name);
1001+
}
1002+
return name;
1003+
}
1004+
9921005
/**
9931006
* Move schema to components (if new) and return $ref to schema or
9941007
* existing schema.
@@ -1002,8 +1015,7 @@ private Schema makeSchemaInComponents(String name, Schema schema) {
10021015
Schema refSchema;
10031016
if (existing != null) {
10041017
refSchema = new Schema().$ref(existing);
1005-
// Store the name this schema would have had if not deduplicated
1006-
refSchema.addExtension("x-alias-name", name);
1018+
refSchema.addExtension("x-alias-name", applyInlineSchemaNameMapping(name));
10071019
} else {
10081020
if (resolveInlineEnums && schema.getEnum() != null && schema.getEnum().size() > 0) {
10091021
LOGGER.warn("Model " + name + " promoted to its own schema due to resolveInlineEnums=true");

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,4 +1252,51 @@ public void testDeduplicationAddsAliasName() {
12521252
// Verify the first reference does not have x-alias-name (it's the original)
12531253
assertNull(detailsRef.getExtensions() != null ? detailsRef.getExtensions().get("x-alias-name") : null);
12541254
}
1255+
1256+
@Test
1257+
public void testDeduplicationWithInlineSchemaNameMapping() {
1258+
// Test that x-alias-name respects inlineSchemaNameMapping configuration
1259+
OpenAPI openapi = new OpenAPI();
1260+
openapi.setComponents(new Components());
1261+
1262+
// Create two models with identical inline schemas that will be deduplicated
1263+
openapi.getComponents().addSchemas("ModelA", new ObjectSchema()
1264+
.addProperty("name", new StringSchema())
1265+
.addProperty("details", new ObjectSchema()
1266+
.addProperty("field1", new StringSchema())
1267+
.addProperty("field2", new IntegerSchema())));
1268+
1269+
openapi.getComponents().addSchemas("ModelB", new ObjectSchema()
1270+
.addProperty("title", new StringSchema())
1271+
.addProperty("info", new ObjectSchema()
1272+
.addProperty("field1", new StringSchema())
1273+
.addProperty("field2", new IntegerSchema())));
1274+
1275+
// Configure inlineSchemaNameMapping to rename ModelB_info to CustomInfoName
1276+
InlineModelResolver resolver = new InlineModelResolver();
1277+
Map<String, String> inlineSchemaNames = new HashMap<>();
1278+
inlineSchemaNames.put("ModelB_info", "CustomInfoName");
1279+
resolver.setInlineSchemaNameMapping(inlineSchemaNames);
1280+
resolver.flatten(openapi);
1281+
1282+
// Check ModelB's property reference - should be deduplicated to ModelA_details
1283+
Schema modelB = openapi.getComponents().getSchemas().get("ModelB");
1284+
assertNotNull(modelB);
1285+
Schema infoRef = (Schema) modelB.getProperties().get("info");
1286+
assertNotNull(infoRef);
1287+
assertNotNull(infoRef.get$ref());
1288+
// The ref should point to the first schema created (ModelA_details)
1289+
assertEquals("#/components/schemas/ModelA_details", infoRef.get$ref());
1290+
1291+
// Verify x-alias-name extension uses the MAPPED name, not the original name
1292+
assertNotNull(infoRef.getExtensions());
1293+
assertTrue(infoRef.getExtensions().containsKey("x-alias-name"));
1294+
assertEquals("CustomInfoName", infoRef.getExtensions().get("x-alias-name"));
1295+
1296+
// Verify CustomInfoName schema was NOT created (since it was deduplicated)
1297+
assertNull(openapi.getComponents().getSchemas().get("CustomInfoName"));
1298+
1299+
// Verify ModelA_details schema was created (the deduplicated schema)
1300+
assertNotNull(openapi.getComponents().getSchemas().get("ModelA_details"));
1301+
}
12551302
}

0 commit comments

Comments
 (0)