Skip to content

Commit 395808e

Browse files
committed
test: add tests for InlineModelResolver requestBody with component schema refs
1 parent e2fab1b commit 395808e

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,4 +1205,83 @@ public void doNotWrapSingleAllOfRefs() {
12051205
assertNotNull(allOfRefWithDescriptionAndReadonly.getAllOf());
12061206
assertEquals(numberRangeRef, ((Schema) allOfRefWithDescriptionAndReadonly.getAllOf().get(0)).get$ref());
12071207
}
1208+
1209+
@Test
1210+
public void resolveRequestBodyWithRefToComponentSchema() {
1211+
OpenAPI openAPI = new OpenAPI();
1212+
openAPI.setComponents(new Components());
1213+
1214+
// Create a component schema
1215+
ObjectSchema userSchema = new ObjectSchema()
1216+
.title("User")
1217+
.addProperty("id", new IntegerSchema())
1218+
.addProperty("name", new StringSchema());
1219+
openAPI.getComponents().addSchemas("User", userSchema);
1220+
1221+
// Create an operation with requestBody that references the component schema
1222+
openAPI.path("/test", new PathItem()
1223+
.post(new Operation()
1224+
.operationId("testPost")
1225+
.requestBody(new RequestBody()
1226+
.content(new Content()
1227+
.addMediaType("application/json",
1228+
new MediaType()
1229+
.schema(new Schema().$ref("#/components/schemas/User")))))));
1230+
1231+
new InlineModelResolver().flatten(openAPI);
1232+
1233+
// Verify that the requestBody schema uses the referenced schema name "User"
1234+
// instead of operation-based name "testPost_request"
1235+
RequestBody requestBody = openAPI.getPaths().get("/test").getPost().getRequestBody();
1236+
Schema requestBodySchema = requestBody.getContent().get("application/json").getSchema();
1237+
1238+
// When a requestBody schema has a $ref to a component schema, it should reference that schema directly
1239+
// and not create an inline schema with operation-based naming
1240+
assertNotNull(requestBodySchema.get$ref());
1241+
assertEquals("#/components/schemas/User", requestBodySchema.get$ref());
1242+
1243+
// Verify that no operation-based schema was created
1244+
assertNull(openAPI.getComponents().getSchemas().get("testPost_request"));
1245+
1246+
// Verify that the User schema still exists
1247+
assertNotNull(openAPI.getComponents().getSchemas().get("User"));
1248+
}
1249+
1250+
@Test
1251+
public void resolveRequestBodyWithRefToComponentSchemaWithTitle() {
1252+
OpenAPI openAPI = new OpenAPI();
1253+
openAPI.setComponents(new Components());
1254+
1255+
// Create a component schema with a title
1256+
ObjectSchema petSchema = new ObjectSchema()
1257+
.title("Pet")
1258+
.addProperty("id", new IntegerSchema())
1259+
.addProperty("name", new StringSchema());
1260+
openAPI.getComponents().addSchemas("Pet", petSchema);
1261+
1262+
// Create an operation with requestBody that references the component schema
1263+
openAPI.path("/pets", new PathItem()
1264+
.post(new Operation()
1265+
.operationId("createPet")
1266+
.requestBody(new RequestBody()
1267+
.content(new Content()
1268+
.addMediaType("application/json",
1269+
new MediaType()
1270+
.schema(new Schema().$ref("#/components/schemas/Pet")))))));
1271+
1272+
new InlineModelResolver().flatten(openAPI);
1273+
1274+
// Verify that the requestBody schema uses the referenced schema name "Pet"
1275+
RequestBody requestBody = openAPI.getPaths().get("/pets").getPost().getRequestBody();
1276+
Schema requestBodySchema = requestBody.getContent().get("application/json").getSchema();
1277+
1278+
assertNotNull(requestBodySchema.get$ref());
1279+
assertEquals("#/components/schemas/Pet", requestBodySchema.get$ref());
1280+
1281+
// Verify that no operation-based schema was created
1282+
assertNull(openAPI.getComponents().getSchemas().get("createPet_request"));
1283+
1284+
// Verify that the Pet schema still exists
1285+
assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
1286+
}
12081287
}

0 commit comments

Comments
 (0)