Skip to content

Commit 5b22d08

Browse files
authored
[BUG] Multi-Part content type in response ignores properties of composed schema (allOf/oneOf) (#6073)
1 parent 62e5950 commit 5b22d08

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5653,8 +5653,11 @@ public List<CodegenParameter> fromRequestBodyToFormParameters(RequestBody body,
56535653
LOGGER.debug("debugging fromRequestBodyToFormParameters= " + body);
56545654
Schema schema = ModelUtils.getSchemaFromRequestBody(body);
56555655
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
5656-
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
5657-
Map<String, Schema> properties = schema.getProperties();
5656+
List<String> allRequired = new ArrayList<String>();
5657+
Map<String, Schema> properties = new LinkedHashMap<>();
5658+
addProperties(properties, allRequired, schema);
5659+
5660+
if (!properties.isEmpty()) {
56585661
for (Map.Entry<String, Schema> entry : properties.entrySet()) {
56595662
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
56605663
// key => property name

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,4 +2162,73 @@ public void testUseOneOfInterfaces() {
21622162
Schema items = ((ArraySchema) openAPI.getComponents().getSchemas().get("CustomOneOfArraySchema")).getItems();
21632163
Assert.assertEquals(items.getExtensions().get("x-one-of-name"), "CustomOneOfArraySchemaOneOf");
21642164
}
2165+
2166+
@Test
2167+
public void testFormComposedSchema() {
2168+
OpenAPI openAPI = TestUtils.parseContent("openapi: 3.0.1\n" +
2169+
"info:\n" +
2170+
" version: '1.0.0'\n" +
2171+
" title: the title\n" +
2172+
"\n" +
2173+
"paths:\n" +
2174+
" '/users/me':\n" +
2175+
" post:\n" +
2176+
" description: Change user password.\n" +
2177+
" operationId: changeCurrentUserPassword\n" +
2178+
" requestBody:\n" +
2179+
" required: true\n" +
2180+
" content:\n" +
2181+
" multipart/form-data:\n" +
2182+
" schema:\n" +
2183+
" $ref: '#/components/schemas/ChangePasswordRequest'\n" +
2184+
" responses:\n" +
2185+
" '200':\n" +
2186+
" description: Successful operation\n" +
2187+
" content: {}\n" +
2188+
"\n" +
2189+
"components:\n" +
2190+
" schemas:\n" +
2191+
" CommonPasswordRequest:\n" +
2192+
" type: object\n" +
2193+
" required: [ password, passwordConfirmation ]\n" +
2194+
" properties:\n" +
2195+
" password:\n" +
2196+
" type: string\n" +
2197+
" format: password\n" +
2198+
" passwordConfirmation:\n" +
2199+
" type: string\n" +
2200+
" format: password\n" +
2201+
"\n" +
2202+
" ChangePasswordRequest:\n" +
2203+
" type: object\n" +
2204+
" allOf:\n" +
2205+
" - $ref: '#/components/schemas/CommonPasswordRequest'\n" +
2206+
" - type: object\n" +
2207+
" required: [ oldPassword ]\n" +
2208+
" properties:\n" +
2209+
" oldPassword:\n" +
2210+
" type: string\n" +
2211+
" format: password\n");
2212+
2213+
final DefaultCodegen cg = new DefaultCodegen();
2214+
cg.setOpenAPI(openAPI);
2215+
cg.setUseOneOfInterfaces(true);
2216+
cg.preprocessOpenAPI(openAPI);
2217+
2218+
final PathItem path = openAPI.getPaths().get("/users/me");
2219+
final CodegenOperation operation = cg.fromOperation(
2220+
"/users/me",
2221+
"post",
2222+
path.getPost(),
2223+
path.getServers());
2224+
assertEquals(operation.formParams.size(), 3,
2225+
"The list of parameters should include inherited type");
2226+
2227+
final List<String> names = operation.formParams.stream()
2228+
.map(param -> param.paramName)
2229+
.collect(Collectors.toList());
2230+
assertTrue(names.contains("password"));
2231+
assertTrue(names.contains("passwordConfirmation"));
2232+
assertTrue(names.contains("oldPassword"));
2233+
}
21652234
}

0 commit comments

Comments
 (0)