Skip to content

Commit 6d41824

Browse files
authored
Merge branch '2.0' into branch_v2.0.1_Fix745
2 parents 0e63c59 + 82e0008 commit 6d41824

File tree

45 files changed

+1369
-114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1369
-114
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
9595
<dependency>
9696
<groupId>io.swagger.parser.v3</groupId>
9797
<artifactId>swagger-parser</artifactId>
98-
<version>2.0.1</version>
98+
<version>2.0.2-SNAPSHOT</version>
9999
</dependency>
100100

101101
```
@@ -106,7 +106,7 @@ or
106106
<dependency>
107107
<groupId>io.swagger.parser.v3</groupId>
108108
<artifactId>swagger-parser</artifactId>
109-
<version>2.0.1</version>
109+
<version>2.0.2-SNAPSHOT</version>
110110
</dependency>
111111

112112
```

modules/swagger-parser-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.1</version>
6+
<version>2.0.2-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

modules/swagger-parser-v2-converter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.1</version>
6+
<version>2.0.2-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

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

Lines changed: 65 additions & 27 deletions
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) {
@@ -115,6 +116,7 @@ public List<io.swagger.models.auth.AuthorizationValue> convert(List<Authorizatio
115116
v.setType(auth.getType());
116117
v.setValue(auth.getValue());
117118
v.setKeyName(auth.getKeyName());
119+
convertedAuth.add(v);
118120
}
119121
}
120122

@@ -141,7 +143,9 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
141143
openAPI.setExternalDocs(convert(swagger.getExternalDocs()));
142144
}
143145

144-
openAPI.setInfo(convert(swagger.getInfo()));
146+
if (swagger.getInfo() != null) {
147+
openAPI.setInfo(convert(swagger.getInfo()));
148+
}
145149

146150
openAPI.setServers(convert(swagger.getSchemes(), swagger.getHost(), swagger.getBasePath()));
147151

@@ -185,6 +189,7 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
185189
}
186190

187191
if (swagger.getParameters() != null) {
192+
globalV2Parameters.putAll(swagger.getParameters());
188193
swagger.getParameters().forEach((k, v) -> {
189194
if ("body".equals(v.getIn())) {
190195
components.addRequestBodies(k, convertParameterToRequestBody(v));
@@ -295,29 +300,35 @@ private SecurityScheme convertOauth2SecurityScheme(SecuritySchemeDefinition defi
295300
OAuthFlow oAuthFlow = new OAuthFlow();
296301

297302
securityScheme.setType(SecurityScheme.Type.OAUTH2);
298-
299-
switch (oAuth2Definition.getFlow()) {
300-
case "implicit":
301-
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
302-
oAuthFlows.setImplicit(oAuthFlow);
303-
break;
304-
case "password":
305-
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
306-
oAuthFlows.setPassword(oAuthFlow);
307-
break;
308-
case "application":
309-
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
310-
oAuthFlows.setClientCredentials(oAuthFlow);
311-
break;
312-
case "accessCode":
313-
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
314-
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
315-
oAuthFlows.setAuthorizationCode(oAuthFlow);
316-
break;
303+
String flow = oAuth2Definition.getFlow();
304+
305+
if (flow != null) {
306+
switch (flow) {
307+
case "implicit":
308+
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
309+
oAuthFlows.setImplicit(oAuthFlow);
310+
break;
311+
case "password":
312+
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
313+
oAuthFlows.setPassword(oAuthFlow);
314+
break;
315+
case "application":
316+
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
317+
oAuthFlows.setClientCredentials(oAuthFlow);
318+
break;
319+
case "accessCode":
320+
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
321+
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
322+
oAuthFlows.setAuthorizationCode(oAuthFlow);
323+
break;
324+
}
317325
}
318326

319327
Scopes scopes = new Scopes();
320-
oAuth2Definition.getScopes().forEach((k, v) -> scopes.addString(k, v));
328+
Map<String, String> oAuth2Scopes = oAuth2Definition.getScopes();
329+
if (oAuth2Scopes != null) {
330+
oAuth2Scopes.forEach((k, v) -> scopes.addString(k, v));
331+
}
321332
oAuthFlow.setScopes(scopes);
322333

323334
securityScheme.setFlows(oAuthFlows);
@@ -508,6 +519,16 @@ public PathItem convert(Path v2Path) {
508519
return v3Path;
509520
}
510521

522+
private boolean isRefABodyParam(io.swagger.models.parameters.Parameter param) {
523+
if (param instanceof RefParameter) {
524+
RefParameter refParameter = (RefParameter) param;
525+
String simpleRef = refParameter.getSimpleRef();
526+
io.swagger.models.parameters.Parameter parameter = globalV2Parameters.get(simpleRef);
527+
return "body".equals(parameter.getIn());
528+
}
529+
return false;
530+
}
531+
511532
public Operation convert(io.swagger.models.Operation v2Operation) {
512533
Operation operation = new Operation();
513534
if (StringUtils.isNotBlank(v2Operation.getDescription())) {
@@ -530,7 +551,14 @@ public Operation convert(io.swagger.models.Operation v2Operation) {
530551
} else if ("body".equals(param.getIn())) {
531552
operation.setRequestBody(convertParameterToRequestBody(param, v2Operation.getConsumes()));
532553
} else {
533-
operation.addParametersItem(convert(param));
554+
Parameter convert = convert(param);
555+
String $ref = convert.get$ref();
556+
if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
557+
operation.setRequestBody(new RequestBody().$ref($ref));
558+
} else {
559+
operation.addParametersItem(convert);
560+
}
561+
//operation.addParametersItem(convert(param));
534562
}
535563
}
536564

@@ -810,6 +838,9 @@ private Header convertHeader(Property property) {
810838
}
811839

812840
private Schema convert(Property schema) {
841+
if (schema == null) {
842+
return null;
843+
}
813844
Schema result;
814845

815846
if (schema instanceof RefProperty) {
@@ -1005,7 +1036,7 @@ public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
10051036

10061037
if (sp.getEnum() != null) {
10071038
for (String e : sp.getEnum()) {
1008-
switch (sp.getType()) {
1039+
switch (sp.getType() == null ? SchemaTypeUtil.OBJECT_TYPE : sp.getType()) {
10091040
case SchemaTypeUtil.INTEGER_TYPE:
10101041
schema.addEnumItemObject(Integer.parseInt(e));
10111042
break;
@@ -1046,6 +1077,9 @@ public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
10461077
}
10471078

10481079
public Schema convert(io.swagger.models.Model v2Model) {
1080+
if (v2Model == null) {
1081+
return null;
1082+
}
10491083
Schema result;
10501084

10511085
if (v2Model instanceof ArrayModel) {
@@ -1056,11 +1090,15 @@ public Schema convert(io.swagger.models.Model v2Model) {
10561090
result = arraySchema;
10571091
} else if (v2Model instanceof ComposedModel) {
10581092
ComposedModel composedModel = (ComposedModel) v2Model;
1059-
1060-
ComposedSchema composed = Json.mapper().convertValue(v2Model, ComposedSchema.class);
1061-
1093+
ComposedSchema composed = new ComposedSchema();
1094+
composed.setDescription(composedModel.getDescription());
1095+
composed.setExample(composedModel.getExample());
1096+
if (composedModel.getExternalDocs() != null) {
1097+
composed.setExternalDocs(convert(composedModel.getExternalDocs()));
1098+
}
1099+
composed.setTitle(composedModel.getTitle());
1100+
composed.setExtensions(convert(composedModel.getVendorExtensions()));
10621101
composed.setAllOf(composedModel.getAllOf().stream().map(this::convert).collect(Collectors.toList()));
1063-
10641102
result = composed;
10651103
} else {
10661104
String v2discriminator = null;

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.swagger.v3.oas.models.security.SecurityScheme;
1717
import io.swagger.v3.oas.models.tags.Tag;
1818
import io.swagger.v3.parser.converter.SwaggerConverter;
19+
import io.swagger.v3.parser.core.models.AuthorizationValue;
1920
import io.swagger.v3.parser.core.models.ParseOptions;
2021
import io.swagger.v3.parser.core.models.SwaggerParseResult;
2122
import org.testng.annotations.Test;
@@ -26,6 +27,7 @@
2627
import java.nio.file.Files;
2728
import java.nio.file.Paths;
2829
import java.util.ArrayList;
30+
import java.util.Arrays;
2931
import java.util.List;
3032
import java.util.Map;
3133

@@ -78,6 +80,13 @@ public class V2ConverterTest {
7880
private static final String ISSUE_676_JSON = "issue-676.json";
7981
private static final String ISSUE_708_YAML = "issue-708.yaml";
8082
private static final String ISSUE_745_YAML = "issue-745.yaml";
83+
private static final String ISSUE_755_YAML = "issue-755.yaml";
84+
private static final String ISSUE_740_YAML = "issue-740.yaml";
85+
private static final String ISSUE_756_JSON = "issue-756.json";
86+
private static final String ISSUE_758_JSON = "issue-758.json";
87+
private static final String ISSUE_762_JSON = "issue-762.json";
88+
private static final String ISSUE_765_YAML = "issue-765.yaml";
89+
private static final String ISSUE_768_JSON = "issue-786.json";
8190

8291
private static final String API_BATCH_PATH = "/api/batch/";
8392
private static final String PETS_PATH = "/pets";
@@ -429,7 +438,7 @@ public void testIssue30() throws Exception {
429438
assertNotNull(oas);
430439
}
431440

432-
@Test(description = "No Servers - without host, basePath, scheme")
441+
@Test(description = "Expect a default server object when a swagger without host, basePath and scheme is converted to openAPI")
433442
public void testIssue31() throws Exception {
434443
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_31_JSON);
435444
assertNotNull(oas.getServers());
@@ -622,6 +631,52 @@ public void testIssue745() throws Exception {
622631
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_745_YAML);
623632
assertTrue(oas.getServers().get(0).getUrl().startsWith("//"));
624633
}
634+
635+
@Test(description = "OpenAPIParser.readLocation fails when fetching valid Swagger 2.0 resource with AuthorizationValues provided")
636+
public void testIssue785() {
637+
AuthorizationValue apiKey = new AuthorizationValue("api_key", "special-key", "header");
638+
List<AuthorizationValue> authorizationValues = Arrays.asList(apiKey);
639+
SwaggerConverter converter = new SwaggerConverter();
640+
List<io.swagger.models.auth.AuthorizationValue> convertedAuthList = converter.convert(authorizationValues);
641+
assertEquals(convertedAuthList.size(), authorizationValues.size());
642+
}
643+
644+
@Test(description = "OpenAPI v2 converter - Migrate a schema with AllOf")
645+
public void testIssue740() throws Exception {
646+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_740_YAML);
647+
assertNotNull(oas);
648+
Schema schema = oas.getComponents().getSchemas().get("Action");
649+
assertTrue(schema instanceof ComposedSchema);
650+
ComposedSchema composedSchema = (ComposedSchema) schema;
651+
assertEquals(composedSchema.getAllOf().size(), 2);
652+
}
653+
654+
@Test(description = "OpenAPI v2 converter - no model in body parameter")
655+
public void testIssue756() throws Exception {
656+
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_756_JSON);
657+
assertNotNull(oas);
658+
}
659+
660+
@Test(description = "OpenAPI v2 converter - NPE when 'enum' field is available and 'type' field is missing in query parameter")
661+
public void testIssue758() throws Exception {
662+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_758_JSON);
663+
assertNotNull(oas);
664+
}
665+
666+
@Test(description = "OpenAPI v2 Converter: NPE when type is array and 'items' field is missing in array property")
667+
public void testIssue762() throws Exception {
668+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
669+
assertNotNull(oas);
670+
}
671+
672+
@Test(description = "requestBody not correctly populated when Parameters is a list of $refs (OAS 2 to 3 conversion)")
673+
public void testIssue765() throws Exception {
674+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_765_YAML);
675+
assertNotNull(oas);
676+
RequestBody requestBody = oas.getPaths().get("/ping/{ActivityTypePath}").getPost().getRequestBody();
677+
assertNotNull(requestBody);
678+
assertEquals("#/components/requestBodies/Block", requestBody.get$ref());
679+
}
625680

626681
@Test(description = "OpenAPI v2 converter - Missing Parameter.style property")
627682
public void testParameterConversion() throws Exception {
@@ -657,6 +712,20 @@ public void testSwaggerParseResultHasMessage() throws Exception {
657712
assertNotNull(result.getMessages());
658713
}
659714

715+
716+
@Test(description = "OpenAPI v2 converter - Migrate minLength, maxLength and pattern of String property")
717+
public void testIssue786() throws Exception {
718+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_768_JSON);
719+
assertNotNull(oas);
720+
}
721+
722+
723+
@Test(description = "OpenAPI v2 converter - Conversion of a spec without a info section")
724+
public void testIssue755() throws Exception {
725+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_755_YAML);
726+
assertNotNull(oas);
727+
}
728+
660729
private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
661730
SwaggerConverter converter = new SwaggerConverter();
662731
String swaggerAsString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(file).toURI())));
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
swagger: '2.0'
2+
info:
3+
description: 'Test'
4+
version: 1.0.0
5+
title: OpenAPI Test Issue 740
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+
paths:
14+
/ping:
15+
post:
16+
summary: test
17+
operationId: pingOperation
18+
consumes:
19+
- application/json
20+
produces:
21+
- application/json
22+
parameters:
23+
- in: body
24+
name: body
25+
required: true
26+
schema:
27+
$ref: '#/definitions/Entity'
28+
responses:
29+
'200':
30+
description: OK
31+
schema:
32+
$ref: '#/definitions/Action'
33+
definitions:
34+
Entity:
35+
type: "object"
36+
properties:
37+
id:
38+
type: "string"
39+
example: "1438752"
40+
description: "The ID of the entity."
41+
readOnly: true
42+
title: "Entity"
43+
description: "A base object for all API entities."
44+
Action:
45+
title: "Action"
46+
allOf:
47+
- $ref: "#/definitions/Entity"
48+
- type: "object"
49+
required:
50+
- "action_type"
51+
discriminator: "action_type"
52+
properties:
53+
name:
54+
type: "string"
55+
description: "The name of the action."
56+
readOnly: true
57+
action_type:
58+
$ref: "#/definitions/ActionType"
59+
ActionType:
60+
type: "string"
61+
title: "ActionType"
62+
description: "Some enum"
63+
enum:
64+
- "value1"
65+
- "value2"
66+
x-swagger-router-model: "ActionTypeDto"

0 commit comments

Comments
 (0)