Skip to content

Commit c3e78a8

Browse files
committed
Fix for issue 1319
Path parameter required value must be true
1 parent e6255f8 commit c3e78a8

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
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;
@@ -1402,6 +1407,9 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
14021407
if(parameterObj != null) {
14031408
parameter = getParameter(parameterObj, String.format("%s.%s", location, parameterName), result);
14041409
if (parameter != null) {
1410+
if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){
1411+
result.warning(location, "For path parameter "+ parameterName + " the required value should be true");
1412+
}
14051413
parameters.put(parameterName, parameter);
14061414
}
14071415
}
@@ -1442,7 +1450,7 @@ public List<Parameter> getParameterList(ArrayNode obj, String location, ParseRes
14421450
});
14431451
return parameters;
14441452
}
1445-
1453+
14461454
private Parameter getParameterDefinition(Parameter parameter) {
14471455
if (parameter.get$ref() == null) {
14481456
return parameter;
@@ -1452,7 +1460,7 @@ private Parameter getParameterDefinition(Parameter parameter) {
14521460
.map(Components::getParameters)
14531461
.map(parameters -> parameters.get(parameterSchemaName))
14541462
.orElse(parameter);
1455-
1463+
14561464
}
14571465

14581466
public Parameter getParameter(ObjectNode obj, String location, ParseResult result) {
@@ -1556,7 +1564,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15561564
} else {
15571565
parameter.setExplode(Boolean.FALSE);
15581566
}
1559-
1567+
15601568

15611569
ObjectNode parameterObject = getObject("schema",obj,false,location,result);
15621570
if (parameterObject!= null) {
@@ -1801,7 +1809,7 @@ public SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseR
18011809

18021810
boolean descriptionRequired, bearerFormatRequired, nameRequired, inRequired, schemeRequired, flowsRequired, openIdConnectRequired;
18031811
descriptionRequired = bearerFormatRequired = nameRequired = inRequired = schemeRequired = flowsRequired = openIdConnectRequired = false;
1804-
1812+
18051813
String value = getString("type", node, true, location, result);
18061814
if (StringUtils.isNotBlank(value)) {
18071815
if (SecurityScheme.Type.APIKEY.toString().equals(value)) {
@@ -1939,7 +1947,7 @@ public OAuthFlow getOAuthFlow(String oAuthFlowType, ObjectNode node, String loca
19391947
authorizationUrlRequired = tokenUrlRequired=true;
19401948
break;
19411949
}
1942-
1950+
19431951
String value = getString("authorizationUrl", node, authorizationUrlRequired, location, result);
19441952
if (StringUtils.isNotBlank(value)) {
19451953
oAuthFlow.setAuthorizationUrl(value);
@@ -2434,7 +2442,7 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
24342442
* Throws a ParseException if no applicable object can be recognized.
24352443
*/
24362444
private Object getDecodedObject( Schema schema, String objectString) throws ParseException {
2437-
Object object =
2445+
Object object =
24382446
objectString == null?
24392447
null :
24402448

@@ -2471,7 +2479,7 @@ private OffsetDateTime toDateTime(String dateString) {
24712479

24722480
return dateTime;
24732481
}
2474-
2482+
24752483

24762484
/**
24772485
* Returns the Date represented by the given RFC3339 full-date string.
@@ -2499,15 +2507,15 @@ private Date toDate( String dateString) {
24992507

25002508
return date;
25012509
}
2502-
2510+
25032511

25042512
/**
25052513
* Returns the byte array represented by the given base64-encoded string.
25062514
* Returns null if this string is not a valid base64 encoding.
25072515
*/
25082516
private byte[] toBytes( String byteString) {
25092517
byte[] bytes;
2510-
2518+
25112519
try {
25122520
bytes = Base64.getDecoder().decode( byteString);
25132521
}
@@ -3032,11 +3040,11 @@ public void extra(String location, String key, JsonNode value) {
30323040
public void missing(String location, String key) {
30333041
missing.add(new Location(location, key));
30343042
}
3035-
3043+
30363044
public void warning(String location, String key) {
30373045
warnings.add(new Location(location, key));
30383046
}
3039-
3047+
30403048
public void unique(String location, String key) {
30413049
unique.add(new Location(location, key));
30423050

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
@@ -1815,7 +1815,17 @@ public void testIssue480() {
18151815
public void checkAllOfAreTaken() {
18161816
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/allOf-example/allOf.yaml");
18171817
assertEquals(2, openAPI.getComponents().getSchemas().size());
1818+
}
18181819

1820+
@Test
1821+
public void checkPathParameterRequiredValue() {
1822+
ParseOptions options = new ParseOptions();
1823+
options.setResolve(true);
1824+
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1319.yaml", null, options);
1825+
assertEquals(2, swaggerParseResult.getMessages().size());
1826+
assertEquals(2, swaggerParseResult.getOpenAPI().getComponents().getSchemas().size());
1827+
assertEquals(2, swaggerParseResult.getOpenAPI().getPaths().size());
1828+
assertEquals(1, swaggerParseResult.getOpenAPI().getComponents().getParameters().size());
18191829
}
18201830

18211831
@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'

0 commit comments

Comments
 (0)