diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index e1dfe4a27dd..c05177ce77f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -2097,13 +2097,13 @@ public class DefaultCodegen implements CodegenConfig { op.isDeprecated = operation.getDeprecated(); } - addConsumesInfo(operation, op); + addConsumesInfo(openAPI, operation, op); if (operation.getResponses() != null && !operation.getResponses().isEmpty()) { ApiResponse methodResponse = findMethodResponse(operation.getResponses()); for (String key : operation.getResponses().keySet()) { ApiResponse response = operation.getResponses().get(key); - addProducesInfo(response, op); + addProducesInfo(openAPI, response, op); CodegenResponse r = fromResponse(key, response); r.hasMore = true; if (r.baseType != null && @@ -3853,12 +3853,13 @@ public class DefaultCodegen implements CodegenConfig { } } - private void addConsumesInfo(Operation operation, CodegenOperation codegenOperation) { - if (operation.getRequestBody() == null || operation.getRequestBody().getContent() == null || operation.getRequestBody().getContent().isEmpty()) { + private void addConsumesInfo(OpenAPI openAPI, Operation operation, CodegenOperation codegenOperation) { + RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, operation.getRequestBody()); + if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) { return; } - Set consumes = operation.getRequestBody().getContent().keySet(); + Set consumes = requestBody.getContent().keySet(); List> mediaTypeList = new ArrayList<>(); int count = 0; for (String key : consumes) { @@ -3921,7 +3922,8 @@ public class DefaultCodegen implements CodegenConfig { return ModelUtils.getReferencedSchema(openAPI, schema) != null; } - private void addProducesInfo(ApiResponse response, CodegenOperation codegenOperation) { + private void addProducesInfo(OpenAPI openAPI, ApiResponse inputResponse, CodegenOperation codegenOperation) { + ApiResponse response = ModelUtils.getReferencedApiResponse(openAPI, inputResponse); if (response == null || response.getContent() == null || response.getContent().isEmpty()) { return; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 1debc28643b..a9f1a7deaec 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -44,6 +44,7 @@ public class DefaultCodegenTest { @Test public void testGetConsumesInfoAndGetProducesInfo() throws Exception { + final DefaultCodegen codegen = new DefaultCodegen(); final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet"); OpenAPI openAPI = new OpenAPI(); openAPI.setComponents(new Components()); @@ -71,6 +72,10 @@ public class DefaultCodegenTest { Assert.assertTrue(createConsumesInfo.contains("application/xml"), "contains 'application/xml'"); Set createProducesInfo = DefaultCodegen.getProducesInfo(openAPI, createOperation); Assert.assertEquals(createProducesInfo.size(), 0); + CodegenOperation coCreate = codegen.fromOperation("somepath", "post", createOperation, openAPI.getComponents().getSchemas(), openAPI); + Assert.assertTrue(coCreate.hasConsumes); + Assert.assertEquals(coCreate.consumes.size(), 2); + Assert.assertFalse(coCreate.hasProduces); Operation updateOperationWithRef = new Operation() .requestBody(new RequestBody().$ref("#/components/requestBodies/MyRequestBody")) @@ -81,6 +86,14 @@ public class DefaultCodegenTest { Set updateProducesInfo = DefaultCodegen.getProducesInfo(openAPI, updateOperationWithRef); Assert.assertEquals(updateProducesInfo.size(), 1); Assert.assertTrue(updateProducesInfo.contains("application/xml"), "contains 'application/xml'"); + + CodegenOperation coUpdate = codegen.fromOperation("somepath", "post", updateOperationWithRef, openAPI.getComponents().getSchemas(), openAPI); + Assert.assertTrue(coUpdate.hasConsumes); + Assert.assertEquals(coUpdate.consumes.size(), 1); + Assert.assertEquals(coUpdate.consumes.get(0).get("mediaType"), "application/json"); + Assert.assertTrue(coUpdate.hasProduces); + Assert.assertEquals(coUpdate.produces.size(), 1); + Assert.assertEquals(coUpdate.produces.get(0).get("mediaType"), "application/xml"); } @Test