Skip to content

Commit ed15fee

Browse files
committed
Claude added some extra code to resolve the code review
1 parent af9a898 commit ed15fee

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +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);
670671
schema.setRequired(component.getRequired());
671672
listIterator.set(schema);
672673
}
@@ -826,6 +827,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
826827
String existing = matchGenerated(model);
827828
if (existing != null) {
828829
Schema schema = new Schema().$ref(existing);
830+
schema.addExtension("x-alias-name", modelName);
829831
schema.setRequired(op.getRequired());
830832
propsToUpdate.put(key, schema);
831833
} else {
@@ -846,6 +848,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
846848
String existing = matchGenerated(innerModel);
847849
if (existing != null) {
848850
Schema schema = new Schema().$ref(existing);
851+
schema.addExtension("x-alias-name", modelName);
849852
schema.setRequired(op.getRequired());
850853
property.setItems(schema);
851854
} else {
@@ -876,6 +879,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
876879
String existing = matchGenerated(innerModel);
877880
if (existing != null) {
878881
Schema schema = new Schema().$ref(existing);
882+
schema.addExtension("x-alias-name", modelName);
879883
schema.setRequired(op.getRequired());
880884
property.setAdditionalProperties(schema);
881885
} else {
@@ -1037,6 +1041,10 @@ private void copyVendorExtensions(Schema source, Schema target) {
10371041
return;
10381042
}
10391043
for (String extName : vendorExtensions.keySet()) {
1044+
// Don't overwrite x-alias-name that was set during deduplication
1045+
if ("x-alias-name".equals(extName) && target.getExtensions() != null && target.getExtensions().containsKey("x-alias-name")) {
1046+
continue;
1047+
}
10401048
target.addExtension(extName, vendorExtensions.get(extName));
10411049
}
10421050
}

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
@@ -1205,4 +1205,51 @@ public void doNotWrapSingleAllOfRefs() {
12051205
assertNotNull(allOfRefWithDescriptionAndReadonly.getAllOf());
12061206
assertEquals(numberRangeRef, ((Schema) allOfRefWithDescriptionAndReadonly.getAllOf().get(0)).get$ref());
12071207
}
1208+
1209+
@Test
1210+
public void testDeduplicationAddsAliasName() {
1211+
// Test that when inline schemas are deduplicated, the x-alias-name extension is set
1212+
OpenAPI openapi = new OpenAPI();
1213+
openapi.setComponents(new Components());
1214+
1215+
// Create two models with identical inline schemas that will be deduplicated
1216+
openapi.getComponents().addSchemas("ModelA", new ObjectSchema()
1217+
.addProperty("name", new StringSchema())
1218+
.addProperty("details", new ObjectSchema()
1219+
.addProperty("field1", new StringSchema())
1220+
.addProperty("field2", new IntegerSchema())));
1221+
1222+
openapi.getComponents().addSchemas("ModelB", new ObjectSchema()
1223+
.addProperty("title", new StringSchema())
1224+
.addProperty("info", new ObjectSchema()
1225+
.addProperty("field1", new StringSchema())
1226+
.addProperty("field2", new IntegerSchema())));
1227+
1228+
new InlineModelResolver().flatten(openapi);
1229+
1230+
// Check ModelA's property reference
1231+
Schema modelA = openapi.getComponents().getSchemas().get("ModelA");
1232+
assertNotNull(modelA);
1233+
Schema detailsRef = (Schema) modelA.getProperties().get("details");
1234+
assertNotNull(detailsRef);
1235+
assertNotNull(detailsRef.get$ref());
1236+
assertEquals("#/components/schemas/ModelA_details", detailsRef.get$ref());
1237+
1238+
// Check ModelB's property reference - should be deduplicated to ModelA_details
1239+
Schema modelB = openapi.getComponents().getSchemas().get("ModelB");
1240+
assertNotNull(modelB);
1241+
Schema infoRef = (Schema) modelB.getProperties().get("info");
1242+
assertNotNull(infoRef);
1243+
assertNotNull(infoRef.get$ref());
1244+
// The ref should point to the first schema created (ModelA_details)
1245+
assertEquals("#/components/schemas/ModelA_details", infoRef.get$ref());
1246+
1247+
// Verify x-alias-name extension is set on the deduplicated reference
1248+
assertNotNull(infoRef.getExtensions());
1249+
assertTrue(infoRef.getExtensions().containsKey("x-alias-name"));
1250+
assertEquals("ModelB_info", infoRef.getExtensions().get("x-alias-name"));
1251+
1252+
// Verify the first reference does not have x-alias-name (it's the original)
1253+
assertNull(detailsRef.getExtensions() != null ? detailsRef.getExtensions().get("x-alias-name") : null);
1254+
}
12081255
}

0 commit comments

Comments
 (0)