Consider '$ref' for consumes and produces in CodegenOperation (#180)

This commit is contained in:
Jérémie Bresson 2018-04-21 18:40:52 +02:00 committed by GitHub
parent e1fe9a3b60
commit edbe4902a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -2097,13 +2097,13 @@ public class DefaultCodegen implements CodegenConfig {
op.isDeprecated = operation.getDeprecated(); op.isDeprecated = operation.getDeprecated();
} }
addConsumesInfo(operation, op); addConsumesInfo(openAPI, operation, op);
if (operation.getResponses() != null && !operation.getResponses().isEmpty()) { if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
ApiResponse methodResponse = findMethodResponse(operation.getResponses()); ApiResponse methodResponse = findMethodResponse(operation.getResponses());
for (String key : operation.getResponses().keySet()) { for (String key : operation.getResponses().keySet()) {
ApiResponse response = operation.getResponses().get(key); ApiResponse response = operation.getResponses().get(key);
addProducesInfo(response, op); addProducesInfo(openAPI, response, op);
CodegenResponse r = fromResponse(key, response); CodegenResponse r = fromResponse(key, response);
r.hasMore = true; r.hasMore = true;
if (r.baseType != null && if (r.baseType != null &&
@ -3853,12 +3853,13 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
private void addConsumesInfo(Operation operation, CodegenOperation codegenOperation) { private void addConsumesInfo(OpenAPI openAPI, Operation operation, CodegenOperation codegenOperation) {
if (operation.getRequestBody() == null || operation.getRequestBody().getContent() == null || operation.getRequestBody().getContent().isEmpty()) { RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, operation.getRequestBody());
if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) {
return; return;
} }
Set<String> consumes = operation.getRequestBody().getContent().keySet(); Set<String> consumes = requestBody.getContent().keySet();
List<Map<String, String>> mediaTypeList = new ArrayList<>(); List<Map<String, String>> mediaTypeList = new ArrayList<>();
int count = 0; int count = 0;
for (String key : consumes) { for (String key : consumes) {
@ -3921,7 +3922,8 @@ public class DefaultCodegen implements CodegenConfig {
return ModelUtils.getReferencedSchema(openAPI, schema) != null; 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()) { if (response == null || response.getContent() == null || response.getContent().isEmpty()) {
return; return;
} }

View File

@ -44,6 +44,7 @@ public class DefaultCodegenTest {
@Test @Test
public void testGetConsumesInfoAndGetProducesInfo() throws Exception { public void testGetConsumesInfoAndGetProducesInfo() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet"); final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
OpenAPI openAPI = new OpenAPI(); OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components()); openAPI.setComponents(new Components());
@ -71,6 +72,10 @@ public class DefaultCodegenTest {
Assert.assertTrue(createConsumesInfo.contains("application/xml"), "contains 'application/xml'"); Assert.assertTrue(createConsumesInfo.contains("application/xml"), "contains 'application/xml'");
Set<String> createProducesInfo = DefaultCodegen.getProducesInfo(openAPI, createOperation); Set<String> createProducesInfo = DefaultCodegen.getProducesInfo(openAPI, createOperation);
Assert.assertEquals(createProducesInfo.size(), 0); 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() Operation updateOperationWithRef = new Operation()
.requestBody(new RequestBody().$ref("#/components/requestBodies/MyRequestBody")) .requestBody(new RequestBody().$ref("#/components/requestBodies/MyRequestBody"))
@ -81,6 +86,14 @@ public class DefaultCodegenTest {
Set<String> updateProducesInfo = DefaultCodegen.getProducesInfo(openAPI, updateOperationWithRef); Set<String> updateProducesInfo = DefaultCodegen.getProducesInfo(openAPI, updateOperationWithRef);
Assert.assertEquals(updateProducesInfo.size(), 1); Assert.assertEquals(updateProducesInfo.size(), 1);
Assert.assertTrue(updateProducesInfo.contains("application/xml"), "contains 'application/xml'"); 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 @Test