Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 8523805

Browse files
authored
Merge pull request swagger-api#775 from ymohdriz/branch_v2.0.1_Fix765
Fix for issue 765
2 parents 1c9b412 + f5c8e9a commit 8523805

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class SwaggerConverter implements SwaggerParserExtension {
8080
private List<String> globalConsumes = new ArrayList<>();
8181
private List<String> globalProduces = new ArrayList<>();
8282
private Components components = new Components();
83+
private Map<String, io.swagger.models.parameters.Parameter> globalV2Parameters = new HashMap<>();
8384

8485
@Override
8586
public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auths, ParseOptions options) {
@@ -185,6 +186,7 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
185186
}
186187

187188
if (swagger.getParameters() != null) {
189+
globalV2Parameters.putAll(swagger.getParameters());
188190
swagger.getParameters().forEach((k, v) -> {
189191
if ("body".equals(v.getIn())) {
190192
components.addRequestBodies(k, convertParameterToRequestBody(v));
@@ -513,6 +515,16 @@ public PathItem convert(Path v2Path) {
513515
return v3Path;
514516
}
515517

518+
private boolean isRefABodyParam(io.swagger.models.parameters.Parameter param) {
519+
if (param instanceof RefParameter) {
520+
RefParameter refParameter = (RefParameter) param;
521+
String simpleRef = refParameter.getSimpleRef();
522+
io.swagger.models.parameters.Parameter parameter = globalV2Parameters.get(simpleRef);
523+
return "body".equals(parameter.getIn());
524+
}
525+
return false;
526+
}
527+
516528
public Operation convert(io.swagger.models.Operation v2Operation) {
517529
Operation operation = new Operation();
518530
if (StringUtils.isNotBlank(v2Operation.getDescription())) {
@@ -535,7 +547,14 @@ public Operation convert(io.swagger.models.Operation v2Operation) {
535547
} else if ("body".equals(param.getIn())) {
536548
operation.setRequestBody(convertParameterToRequestBody(param, v2Operation.getConsumes()));
537549
} else {
538-
operation.addParametersItem(convert(param));
550+
Parameter convert = convert(param);
551+
String $ref = convert.get$ref();
552+
if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
553+
operation.setRequestBody(new RequestBody().$ref($ref));
554+
} else {
555+
operation.addParametersItem(convert);
556+
}
557+
//operation.addParametersItem(convert(param));
539558
}
540559
}
541560

modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class V2ConverterTest {
8181
private static final String ISSUE_756_JSON = "issue-756.json";
8282
private static final String ISSUE_758_JSON = "issue-758.json";
8383
private static final String ISSUE_762_JSON = "issue-762.json";
84+
private static final String ISSUE_765_YAML = "issue-765.yaml";
8485

8586
private static final String API_BATCH_PATH = "/api/batch/";
8687
private static final String PETS_PATH = "/pets";
@@ -647,6 +648,15 @@ public void testIssue762() throws Exception {
647648
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
648649
assertNotNull(oas);
649650
}
651+
652+
@Test(description = "requestBody not correctly populated when Parameters is a list of $refs (OAS 2 to 3 conversion)")
653+
public void testIssue765() throws Exception {
654+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_765_YAML);
655+
assertNotNull(oas);
656+
RequestBody requestBody = oas.getPaths().get("/ping/{ActivityTypePath}").getPost().getRequestBody();
657+
assertNotNull(requestBody);
658+
assertEquals("#/components/requestBodies/Block", requestBody.get$ref());
659+
}
650660

651661
@Test(description = "OpenAPI v2 converter - Missing Parameter.style property")
652662
public void testParameterConversion() throws Exception {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
swagger: '2.0'
2+
info:
3+
description: 'Test'
4+
version: 1.0.0
5+
title: OpenAPI Test
6+
license:
7+
name: Apache-2.0
8+
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
9+
host: petstore.swagger.io
10+
basePath: /v2
11+
schemes:
12+
- http
13+
parameters:
14+
ActivityTypePath:
15+
in: path
16+
name: type
17+
required: true
18+
type: string
19+
Block:
20+
in: body
21+
name: block
22+
required: true
23+
schema:
24+
properties:
25+
visual_type:
26+
type: string
27+
type: object
28+
paths:
29+
/ping/{ActivityTypePath}:
30+
post:
31+
summary: test
32+
description: 'test it'
33+
operationId: pingOp
34+
consumes:
35+
- application/json
36+
produces:
37+
- application/json
38+
parameters:
39+
- $ref: '#/parameters/Block'
40+
- $ref: '#/parameters/ActivityTypePath'
41+
responses:
42+
'200':
43+
description: OK

0 commit comments

Comments
 (0)