mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +00:00
Consider '$ref' for consumes and produces in CodegenOperation (#180)
This commit is contained in:
parent
e1fe9a3b60
commit
edbe4902a4
@ -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<String> consumes = operation.getRequestBody().getContent().keySet();
|
||||
Set<String> consumes = requestBody.getContent().keySet();
|
||||
List<Map<String, String>> 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;
|
||||
}
|
||||
|
@ -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<String> 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<String> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user