Skip to content

Commit 94e2a37

Browse files
authored
Merge pull request #303 from skrysmanski/fix/missing-consumes-when-ref
Fixed consumes list not being resolved when request body is specified via reference (rather than inline)
2 parents 3cc1eb5 + bb18488 commit 94e2a37

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
18901890
codegenOperation.getVendorExtensions().put(CodegenConstants.IS_DEPRECATED_EXT_NAME, operation.getDeprecated());
18911891
}
18921892

1893-
addConsumesInfo(operation, codegenOperation);
1893+
addConsumesInfo(operation, codegenOperation, openAPI);
18941894

18951895
if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
18961896
ApiResponse methodResponse = findMethodResponse(operation.getResponses());
@@ -3805,11 +3805,21 @@ private List<Schema> getInterfaces(ComposedSchema composed) {
38053805
}
38063806
}
38073807

3808-
protected void addConsumesInfo(Operation operation, CodegenOperation codegenOperation) {
3809-
if(operation.getRequestBody() == null || operation.getRequestBody().getContent() == null || operation.getRequestBody().getContent().isEmpty()) {
3808+
protected void addConsumesInfo(Operation operation, CodegenOperation codegenOperation, OpenAPI openAPI) {
3809+
RequestBody body = operation.getRequestBody();
3810+
if (body == null) {
3811+
return;
3812+
}
3813+
if (StringUtils.isNotBlank(body.get$ref())) {
3814+
String bodyName = OpenAPIUtil.getSimpleRef(body.get$ref());
3815+
body = openAPI.getComponents().getRequestBodies().get(bodyName);
3816+
}
3817+
3818+
if (body.getContent() == null || body.getContent().isEmpty()) {
38103819
return;
38113820
}
3812-
Set<String> consumes = operation.getRequestBody().getContent().keySet();
3821+
3822+
Set<String> consumes = body.getContent().keySet();
38133823
List<Map<String, String>> mediaTypeList = new ArrayList<>();
38143824
int count = 0;
38153825
for (String key : consumes) {

src/test/java/io/swagger/codegen/v3/generators/DefaultCodegenConfigTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
import io.swagger.v3.oas.models.media.Schema;
1818
import io.swagger.v3.oas.models.parameters.Parameter;
1919
import io.swagger.v3.oas.models.parameters.RequestBody;
20+
import io.swagger.v3.parser.OpenAPIV3Parser;
2021

2122
import org.testng.Assert;
22-
import org.testng.ITestContext;
2323
import org.testng.annotations.DataProvider;
2424
import org.testng.annotations.Test;
2525

2626
import java.math.BigDecimal;
27-
import java.util.HashMap;
2827
import java.util.HashSet;
2928
import java.util.List;
3029

@@ -168,6 +167,25 @@ public Object[][] provideData_testGetCollectionFormat() {
168167
};
169168
}
170169

170+
/**
171+
* Tests that {@link DefaultCodegenConfig#fromOperation(String, String, Operation, java.util.Map, OpenAPI)} correctly
172+
* resolves the consumes list when the request body is specified via reference rather than inline.
173+
*/
174+
@Test
175+
public void testRequestBodyRefConsumesList() {
176+
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/3_0_0/requestBodyRefTest.json");
177+
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();
178+
final String path = "/test/requestBodyRefTest";
179+
final Operation op = openAPI.getPaths().get(path).getPost();
180+
final CodegenOperation codegenOp = codegen.fromOperation(path, "post", op, openAPI.getComponents().getSchemas(), openAPI);
181+
182+
Assert.assertTrue(codegenOp.getHasConsumes());
183+
Assert.assertNotNull(codegenOp.consumes);
184+
Assert.assertEquals(codegenOp.consumes.size(), 2);
185+
Assert.assertEquals(codegenOp.consumes.get(0).get("mediaType"), "application/json");
186+
Assert.assertEquals(codegenOp.consumes.get(1).get("mediaType"), "application/xml");
187+
}
188+
171189
private static class P_DefaultCodegenConfig extends DefaultCodegenConfig{
172190
@Override
173191
public String getArgumentsLocation() {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Test Api",
5+
"version": "3.0.0"
6+
},
7+
"paths": {
8+
"/test/requestBodyRefTest": {
9+
"post": {
10+
"summary": "Test method",
11+
"requestBody": {
12+
"$ref": "#/components/requestBodies/Pet"
13+
},
14+
"responses": {
15+
"200": {
16+
"description": "Success"
17+
}
18+
}
19+
}
20+
}
21+
},
22+
"components": {
23+
"requestBodies": {
24+
"Pet": {
25+
"content": {
26+
"application/json": {
27+
"schema": {
28+
"type": "string"
29+
}
30+
},
31+
"application/xml": {
32+
"schema": {
33+
"type": "string"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)