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

Commit 159b85d

Browse files
authored
Merge pull request #14
Port pull request swagger-api#775
2 parents 0f2da49 + f5c8e9a commit 159b85d

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) {
@@ -187,6 +188,7 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
187188
}
188189

189190
if (swagger.getParameters() != null) {
191+
globalV2Parameters.putAll(swagger.getParameters());
190192
swagger.getParameters().forEach((k, v) -> {
191193
if ("body".equals(v.getIn())) {
192194
components.addRequestBodies(k, convertParameterToRequestBody(v));
@@ -515,6 +517,16 @@ public PathItem convert(Path v2Path) {
515517
return v3Path;
516518
}
517519

520+
private boolean isRefABodyParam(io.swagger.models.parameters.Parameter param) {
521+
if (param instanceof RefParameter) {
522+
RefParameter refParameter = (RefParameter) param;
523+
String simpleRef = refParameter.getSimpleRef();
524+
io.swagger.models.parameters.Parameter parameter = globalV2Parameters.get(simpleRef);
525+
return "body".equals(parameter.getIn());
526+
}
527+
return false;
528+
}
529+
518530
public Operation convert(io.swagger.models.Operation v2Operation) {
519531
Operation operation = new Operation();
520532
if (StringUtils.isNotBlank(v2Operation.getDescription())) {
@@ -537,7 +549,14 @@ public Operation convert(io.swagger.models.Operation v2Operation) {
537549
} else if ("body".equals(param.getIn())) {
538550
operation.setRequestBody(convertParameterToRequestBody(param, v2Operation.getConsumes()));
539551
} else {
540-
operation.addParametersItem(convert(param));
552+
Parameter convert = convert(param);
553+
String $ref = convert.get$ref();
554+
if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
555+
operation.setRequestBody(new RequestBody().$ref($ref));
556+
} else {
557+
operation.addParametersItem(convert);
558+
}
559+
//operation.addParametersItem(convert(param));
541560
}
542561
}
543562

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
@@ -82,6 +82,7 @@ public class V2ConverterTest {
8282
private static final String ISSUE_756_JSON = "issue-756.json";
8383
private static final String ISSUE_758_JSON = "issue-758.json";
8484
private static final String ISSUE_762_JSON = "issue-762.json";
85+
private static final String ISSUE_765_YAML = "issue-765.yaml";
8586

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

652662
@Test(description = "OpenAPI v2 converter - Missing Parameter.style property")
653663
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)