Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class SwaggerConverter implements SwaggerParserExtension {
private List<String> globalConsumes = new ArrayList<>();
private List<String> globalProduces = new ArrayList<>();
private Components components = new Components();
private Map<String, io.swagger.models.parameters.Parameter> globalV2Parameters = new HashMap<>();

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

if (swagger.getParameters() != null) {
globalV2Parameters.putAll(swagger.getParameters());
swagger.getParameters().forEach((k, v) -> {
if ("body".equals(v.getIn())) {
components.addRequestBodies(k, convertParameterToRequestBody(v));
Expand Down Expand Up @@ -513,6 +515,16 @@ public PathItem convert(Path v2Path) {
return v3Path;
}

private boolean isRefABodyParam(io.swagger.models.parameters.Parameter param) {
if (param instanceof RefParameter) {
RefParameter refParameter = (RefParameter) param;
String simpleRef = refParameter.getSimpleRef();
io.swagger.models.parameters.Parameter parameter = globalV2Parameters.get(simpleRef);
return "body".equals(parameter.getIn());
}
return false;
}

public Operation convert(io.swagger.models.Operation v2Operation) {
Operation operation = new Operation();
if (StringUtils.isNotBlank(v2Operation.getDescription())) {
Expand All @@ -535,7 +547,14 @@ public Operation convert(io.swagger.models.Operation v2Operation) {
} else if ("body".equals(param.getIn())) {
operation.setRequestBody(convertParameterToRequestBody(param, v2Operation.getConsumes()));
} else {
operation.addParametersItem(convert(param));
Parameter convert = convert(param);
String $ref = convert.get$ref();
if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
operation.setRequestBody(new RequestBody().$ref($ref));
} else {
operation.addParametersItem(convert);
}
//operation.addParametersItem(convert(param));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class V2ConverterTest {
private static final String ISSUE_756_JSON = "issue-756.json";
private static final String ISSUE_758_JSON = "issue-758.json";
private static final String ISSUE_762_JSON = "issue-762.json";
private static final String ISSUE_765_YAML = "issue-765.yaml";

private static final String API_BATCH_PATH = "/api/batch/";
private static final String PETS_PATH = "/pets";
Expand Down Expand Up @@ -636,6 +637,15 @@ public void testIssue762() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
assertNotNull(oas);
}

@Test(description = "requestBody not correctly populated when Parameters is a list of $refs (OAS 2 to 3 conversion)")
public void testIssue765() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_765_YAML);
assertNotNull(oas);
RequestBody requestBody = oas.getPaths().get("/ping/{ActivityTypePath}").getPost().getRequestBody();
assertNotNull(requestBody);
assertEquals("#/components/requestBodies/Block", requestBody.get$ref());
}

@Test(description = "OpenAPI v2 converter - Missing Parameter.style property")
public void testParameterConversion() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
swagger: '2.0'
info:
description: 'Test'
version: 1.0.0
title: OpenAPI Test
license:
name: Apache-2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: petstore.swagger.io
basePath: /v2
schemes:
- http
parameters:
ActivityTypePath:
in: path
name: type
required: true
type: string
Block:
in: body
name: block
required: true
schema:
properties:
visual_type:
type: string
type: object
paths:
/ping/{ActivityTypePath}:
post:
summary: test
description: 'test it'
operationId: pingOp
consumes:
- application/json
produces:
- application/json
parameters:
- $ref: '#/parameters/Block'
- $ref: '#/parameters/ActivityTypePath'
responses:
'200':
description: OK