Skip to content

Commit ee9d1a6

Browse files
authored
Merge pull request #1320 from GORA-SAG/issue-1319
Fix for issue 1319
2 parents 900e8ce + 17b72a7 commit ee9d1a6

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ public class OpenAPIDeserializer {
110110
public SwaggerParseResult deserialize(JsonNode rootNode) {
111111
return deserialize(rootNode, null);
112112
}
113-
113+
114114
public SwaggerParseResult deserialize(JsonNode rootNode, String path) {
115115
SwaggerParseResult result = new SwaggerParseResult();
116116
try {
117-
117+
118118
ParseResult rootParse = new ParseResult();
119119
OpenAPI api = parseRoot(rootNode, rootParse, path);
120120
result.setOpenAPI(api);
@@ -389,7 +389,7 @@ public List<Server> getServersList(ArrayNode obj, String location, ParseResult r
389389
}
390390
return servers;
391391
}
392-
392+
393393
public List<Server> getServersList(ArrayNode obj, String location, ParseResult result) {
394394
return getServersList(obj, location, result, null);
395395
}
@@ -454,7 +454,7 @@ public Server getServer(ObjectNode obj, String location, ParseResult result, Str
454454

455455
return server;
456456
}
457-
457+
458458
boolean isValidURL(String urlString){
459459
try {
460460
URL url = new URL(urlString);
@@ -560,6 +560,11 @@ public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
560560
if (!definedInPathLevel) {
561561
List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj);
562562
operationsInAPath.forEach(operation -> {
563+
operation.getParameters().forEach(parameter -> {
564+
if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){
565+
result.warning(location, "For path parameter "+ parameter.getName() + " the required value should be true");
566+
}
567+
});
563568
if (!isPathParamDefined(pathParam, operation.getParameters())) {
564569
result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level");
565570
return;
@@ -1408,6 +1413,9 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
14081413
if(parameterObj != null) {
14091414
parameter = getParameter(parameterObj, String.format("%s.%s", location, parameterName), result);
14101415
if (parameter != null) {
1416+
if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){
1417+
result.warning(location, "For path parameter "+ parameterName + " the required value should be true");
1418+
}
14111419
parameters.put(parameterName, parameter);
14121420
}
14131421
}
@@ -1448,7 +1456,7 @@ public List<Parameter> getParameterList(ArrayNode obj, String location, ParseRes
14481456
});
14491457
return parameters;
14501458
}
1451-
1459+
14521460
private Parameter getParameterDefinition(Parameter parameter) {
14531461
if (parameter.get$ref() == null) {
14541462
return parameter;
@@ -1458,7 +1466,7 @@ private Parameter getParameterDefinition(Parameter parameter) {
14581466
.map(Components::getParameters)
14591467
.map(parameters -> parameters.get(parameterSchemaName))
14601468
.orElse(parameter);
1461-
1469+
14621470
}
14631471

14641472
public Parameter getParameter(ObjectNode obj, String location, ParseResult result) {
@@ -1562,7 +1570,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15621570
} else {
15631571
parameter.setExplode(Boolean.FALSE);
15641572
}
1565-
1573+
15661574

15671575
ObjectNode parameterObject = getObject("schema",obj,false,location,result);
15681576
if (parameterObject!= null) {
@@ -1811,7 +1819,7 @@ public SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseR
18111819

18121820
boolean descriptionRequired, bearerFormatRequired, nameRequired, inRequired, schemeRequired, flowsRequired, openIdConnectRequired;
18131821
descriptionRequired = bearerFormatRequired = nameRequired = inRequired = schemeRequired = flowsRequired = openIdConnectRequired = false;
1814-
1822+
18151823
String value = getString("type", node, true, location, result);
18161824
if (StringUtils.isNotBlank(value)) {
18171825
if (SecurityScheme.Type.APIKEY.toString().equals(value)) {
@@ -1949,7 +1957,7 @@ public OAuthFlow getOAuthFlow(String oAuthFlowType, ObjectNode node, String loca
19491957
authorizationUrlRequired = tokenUrlRequired=true;
19501958
break;
19511959
}
1952-
1960+
19531961
String value = getString("authorizationUrl", node, authorizationUrlRequired, location, result);
19541962
if (StringUtils.isNotBlank(value)) {
19551963
oAuthFlow.setAuthorizationUrl(value);
@@ -2446,7 +2454,7 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
24462454
* Throws a ParseException if no applicable object can be recognized.
24472455
*/
24482456
private Object getDecodedObject( Schema schema, String objectString) throws ParseException {
2449-
Object object =
2457+
Object object =
24502458
objectString == null?
24512459
null :
24522460

@@ -2483,7 +2491,7 @@ private OffsetDateTime toDateTime(String dateString) {
24832491

24842492
return dateTime;
24852493
}
2486-
2494+
24872495

24882496
/**
24892497
* Returns the Date represented by the given RFC3339 full-date string.
@@ -2511,15 +2519,15 @@ private Date toDate( String dateString) {
25112519

25122520
return date;
25132521
}
2514-
2522+
25152523

25162524
/**
25172525
* Returns the byte array represented by the given base64-encoded string.
25182526
* Returns null if this string is not a valid base64 encoding.
25192527
*/
25202528
private byte[] toBytes( String byteString) {
25212529
byte[] bytes;
2522-
2530+
25232531
try {
25242532
bytes = Base64.getDecoder().decode( byteString);
25252533
}
@@ -3050,11 +3058,11 @@ public void extra(String location, String key, JsonNode value) {
30503058
public void missing(String location, String key) {
30513059
missing.add(new Location(location, key));
30523060
}
3053-
3061+
30543062
public void warning(String location, String key) {
30553063
warnings.add(new Location(location, key));
30563064
}
3057-
3065+
30583066
public void unique(String location, String key) {
30593067
unique.add(new Location(location, key));
30603068

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,17 @@ public void testIssue480() {
18261826
public void checkAllOfAreTaken() {
18271827
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/allOf-example/allOf.yaml");
18281828
assertEquals(2, openAPI.getComponents().getSchemas().size());
1829+
}
18291830

1831+
@Test
1832+
public void checkPathParameterRequiredValue() {
1833+
ParseOptions options = new ParseOptions();
1834+
options.setResolve(true);
1835+
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1319.yaml", null, options);
1836+
assertEquals(2, swaggerParseResult.getMessages().size());
1837+
assertEquals(2, swaggerParseResult.getOpenAPI().getComponents().getSchemas().size());
1838+
assertEquals(2, swaggerParseResult.getOpenAPI().getPaths().size());
1839+
assertEquals(1, swaggerParseResult.getOpenAPI().getComponents().getParameters().size());
18301840
}
18311841

18321842
@Test(description = "Issue #616 Relative references inside of 'allOf'")
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
openapi: 3.0.0
2+
info:
3+
description: issue2137
4+
version: 1.0.1
5+
title: issue2137
6+
paths:
7+
'/users/{user_id}/description':
8+
get:
9+
operationId: description
10+
parameters:
11+
- $ref: '#/components/parameters/UserId'
12+
responses:
13+
'200':
14+
description: OK
15+
content:
16+
application/json:
17+
schema:
18+
$ref: '#/components/schemas/goraUrl'
19+
'/gora/{gora_id}/test':
20+
get:
21+
operationId: test
22+
parameters:
23+
- name: gora_id
24+
description: issue-2137
25+
in: path
26+
required: false
27+
schema:
28+
type: string
29+
responses:
30+
'200':
31+
description: OK
32+
content:
33+
application/json:
34+
schema:
35+
$ref: '#/components/schemas/goraUrl'
36+
components:
37+
parameters:
38+
UserId:
39+
name: user_id
40+
description: Unique identifier of a user
41+
in: path
42+
required: false
43+
schema:
44+
type: string
45+
schemas:
46+
gora:
47+
type: object
48+
description: Information about de product
49+
properties:
50+
text:
51+
type: string
52+
goraUrl:
53+
allOf:
54+
- type: object
55+
required:
56+
- url
57+
properties:
58+
url:
59+
type: string
60+
description: Url with information or picture of the product
61+
- $ref: '#/components/schemas/gora'

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void testIssue887() {
8383
System.out.println(result.getMessages());
8484
assertNotNull(result);
8585
assertNotNull(result.getOpenAPI());
86-
assertEquals(result.getMessages().get(0), "attribute tags.sample is repeated");
86+
assertEquals(result.getMessages().get(1), "attribute tags.sample is repeated");
8787
}
8888

8989
@Test

0 commit comments

Comments
 (0)