Skip to content

Commit 870bbb5

Browse files
gracekarinafrantuma
authored andcommitted
oas 3.1 - addressed comments in PR
1 parent 787b1ce commit 870bbb5

File tree

5 files changed

+263
-38
lines changed

5 files changed

+263
-38
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/ParseOptions.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class ParseOptions {
1313
private boolean validateInternalRefs = true;
1414
private boolean legacyYamlDeserialization = false;
1515
private boolean resolveRequestBody = false;
16-
16+
17+
private boolean oaiAuthor;
18+
1719
public boolean isResolve() {
1820
return resolve;
1921
}
@@ -37,7 +39,7 @@ public boolean isResolveFully() {
3739
public void setResolveFully(boolean resolveFully) {
3840
this.resolveFully = resolveFully;
3941
}
40-
42+
4143
public boolean isResolveRequestBody() {
4244
return resolveRequestBody;
4345
}
@@ -105,6 +107,14 @@ public void setLegacyYamlDeserialization(boolean legacyYamlDeserialization) {
105107
this.legacyYamlDeserialization = legacyYamlDeserialization;
106108
}
107109

110+
public void setOaiAuthor(boolean oaiAuthor) {
111+
this.oaiAuthor = oaiAuthor;
112+
}
113+
114+
public boolean isOaiAuthor() {
115+
return oaiAuthor;
116+
}
117+
108118
public void setValidateInternalRefs(boolean validateInternalRefs) {
109119
this.validateInternalRefs = validateInternalRefs;
110120
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public SwaggerParseResult parseJsonNode(String path, JsonNode node) {
136136
return new OpenAPIDeserializer().deserialize(node, path,new ParseOptions());
137137
}
138138
public SwaggerParseResult parseJsonNode(String path, JsonNode node, ParseOptions options) {
139-
return new OpenAPIDeserializer().deserialize(node, path, options);
139+
return new OpenAPIDeserializer().deserialize(node, path, options, options.isOaiAuthor());
140140
}
141141

142142
public SwaggerParseResult readContents(String yaml) {
@@ -180,6 +180,7 @@ private SwaggerParseResult readContents(String swaggerAsString, List<Authorizati
180180
}
181181
}
182182
return result;
183+
183184
} catch (JsonProcessingException e) {
184185
LOGGER.warn("Exception while parsing:", e);
185186
final String message = getParseErrorMessage(e.getOriginalMessage(), location);

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,26 @@ public class OpenAPIDeserializer {
267267
private final Set<String> operationIDs = new HashSet<>();
268268
private Map<String,String> localSchemaRefs = new HashMap<>();
269269

270-
public SwaggerParseResult deserialize(JsonNode rootNode) {
271-
return deserialize(rootNode, null);
270+
public SwaggerParseResult deserialize(JsonNode rootNode) {
271+
return deserialize(rootNode, null, false);
272272
}
273273

274274
public SwaggerParseResult deserialize(JsonNode rootNode, String path) {
275275
return deserialize(rootNode,path, new ParseOptions());
276276
}
277277

278278
public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptions options) {
279+
return deserialize(rootNode,path, new ParseOptions(), false);
280+
}
281+
282+
public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptions options, boolean isOaiAuthor) {
279283
basePath = path;
280284
this.rootNode = rootNode;
281285
rootMap = new ObjectMapper().convertValue(rootNode, Map.class);
282286
SwaggerParseResult result = new SwaggerParseResult();
283287
try {
284288
ParseResult rootParse = new ParseResult();
289+
rootParse.setOaiAuthor(isOaiAuthor);
285290
rootParse.setAllowEmptyStrings(options.isAllowEmptyString());
286291
rootParse.setValidateInternalRefs(options.isValidateInternalRefs());
287292
OpenAPI api = parseRoot(rootNode, rootParse, path);
@@ -390,7 +395,11 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
390395
if (result.isOpenapi31()) {
391396
value = getString("jsonSchemaDialect", rootNode, false, location, result);
392397
if (value != null) {
393-
openAPI.setJsonSchemaDialect(value);
398+
if (isValidURL(value)) {
399+
openAPI.setJsonSchemaDialect(value);
400+
}else{
401+
result.warning(location,"jsonSchemaDialect. Invalid url: " + value);
402+
}
394403
}
395404
}
396405

@@ -413,12 +422,11 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
413422
result.invalid();
414423
return null;
415424
}
416-
417425
return openAPI;
418426
}
419427

420428
private void validateReservedKeywords(Map<String, Set<String>> specKeys, String key, String location, ParseResult result) {
421-
if(result.isOpenapi31() && specKeys.get("RESERVED_KEYWORDS").stream()
429+
if(!result.isOaiAuthor() && result.isOpenapi31() && specKeys.get("RESERVED_KEYWORDS").stream()
422430
.filter(rk -> key.startsWith(rk))
423431
.findAny()
424432
.orElse(null) != null){
@@ -2246,9 +2254,9 @@ public SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseR
22462254
}
22472255

22482256
boolean descriptionRequired, bearerFormatRequired, nameRequired, inRequired, schemeRequired, flowsRequired,
2249-
openIdConnectRequired, mutualTLSRequired;
2257+
openIdConnectRequired;
22502258
descriptionRequired = bearerFormatRequired = nameRequired = inRequired = schemeRequired = flowsRequired =
2251-
openIdConnectRequired = mutualTLSRequired = false;
2259+
openIdConnectRequired = false;
22522260

22532261
String value = getString("type", node, true, location, result);
22542262
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
@@ -2266,7 +2274,6 @@ public SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseR
22662274
openIdConnectRequired = true;
22672275
}else if (result.isOpenapi31() && SecurityScheme.Type.MUTUALTLS.toString().equals(value)) {
22682276
securityScheme.setType(SecurityScheme.Type.MUTUALTLS);
2269-
mutualTLSRequired = true;
22702277
} else {
22712278
result.invalidType(location + ".type", "type", "http|apiKey|oauth2|openIdConnect|mutualTLS ", node);
22722279
}
@@ -2493,7 +2500,6 @@ public Discriminator getDiscriminator(ObjectNode node, String location, ParseRes
24932500
if (extensions != null && extensions.size() > 0) {
24942501
discriminator.setExtensions(extensions);
24952502
}
2496-
//validateReservedKeywords(keys,key, location, result);
24972503
}
24982504
}
24992505
}
@@ -2927,11 +2933,8 @@ at the moment path passed as string (basePath) from upper components can be both
29272933
if (!specKeys.get("SCHEMA_KEYS").contains(key) && !key.startsWith("x-")) {
29282934
result.extra(location, key, node.get(key));
29292935
}
2930-
validateReservedKeywords(specKeys, key, location, result);
29312936
}
2932-
29332937
return schema;
2934-
29352938
}
29362939

29372940

@@ -4042,6 +4045,7 @@ public static class ParseResult {
40424045
private boolean validateInternalRefs;
40434046

40444047
private boolean openapi31 = false;
4048+
private boolean oaiAuthor = false;
40454049

40464050
public ParseResult() {
40474051
}
@@ -4108,11 +4112,23 @@ public ParseResult openapi31(boolean openapi31) {
41084112
this.openapi31 = openapi31;
41094113
return this;
41104114
}
4111-
41124115
public boolean isOpenapi31() {
41134116
return this.openapi31;
41144117
}
41154118

4119+
public boolean isOaiAuthor() {
4120+
return this.oaiAuthor;
4121+
}
4122+
4123+
public void setOaiAuthor(boolean oaiAuthor) {
4124+
this.oaiAuthor = oaiAuthor;
4125+
}
4126+
4127+
public ParseResult oaiAuthor(boolean oaiAuthor) {
4128+
this.oaiAuthor = oaiAuthor;
4129+
return this;
4130+
}
4131+
41164132
public List<String> getMessages() {
41174133
List<String> messages = new ArrayList<String>();
41184134
for (Location l : extra.keySet()) {
@@ -4152,7 +4168,7 @@ public List<String> getMessages() {
41524168
}
41534169
for (Location l : reserved) {
41544170
String location = l.location.equals("") ? "" : l.location + ".";
4155-
String message = "attribute " + location + l.key + " is reserved by The OpenAPI Iniciative";
4171+
String message = "attribute " + location + l.key + " is reserved by The OpenAPI Initiative";
41564172
messages.add(message);
41574173
}
41584174
return messages;

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

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,45 @@ public void testDeserializeSimpleDefinition() throws Exception {
4141
@Test
4242
public void testBasic() {
4343
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/basic.yaml", null, null);
44-
//Yaml31.prettyPrint(result);
4544
//assertEquals(result.getMessages().size(),1);
4645
assertNotNull(result.getOpenAPI());
4746
}
4847

48+
@Test
49+
public void testOAS31() {
50+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/oas3.1.yaml", null, null);
51+
assertNotNull(result.getOpenAPI());
52+
}
53+
54+
@Test
55+
public void testJsonSchemaDialectValid() {
56+
String jsonSchemaDialect = "openapi: 3.1.0\n" +
57+
"jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema\n" +
58+
"info:\n" +
59+
" title: Swagger Petstore\n" +
60+
" version: 1.0.0\n" +
61+
"paths: {}";
62+
SwaggerParseResult result = new OpenAPIV3Parser().readContents( jsonSchemaDialect, null, null);
63+
assertNotNull(result.getOpenAPI());
64+
assertNotNull(result.getOpenAPI().getJsonSchemaDialect());
65+
assertEquals(result.getOpenAPI().getJsonSchemaDialect(), "https://json-schema.org/draft/2020-12/schema");
66+
67+
}
68+
69+
@Test
70+
public void testJsonSchemaDialectInvalid() {
71+
String jsonSchemaDialect = "openapi: 3.1.0\n" +
72+
"jsonSchemaDialect: http//\n" +
73+
"info:\n" +
74+
" title: Swagger Petstore\n" +
75+
" version: 1.0.0\n" +
76+
"paths: {}";
77+
SwaggerParseResult result = new OpenAPIV3Parser().readContents( jsonSchemaDialect, null, null);
78+
assertNotNull(result.getOpenAPI());
79+
assertTrue(result.getMessages().contains("jsonSchemaDialect. Invalid url: http//"));
80+
81+
}
82+
4983
@Test
5084
public void testInfo() {
5185
String infoYaml = "openapi: 3.1.0\n" +
@@ -69,7 +103,6 @@ public void testInfo() {
69103
assertNotNull(result.getOpenAPI());
70104
assertNotNull(result.getOpenAPI().getInfo().getSummary());
71105
assertFalse(result.getMessages().contains("attribute info.summary is unexpected"));
72-
Yaml31.prettyPrint(result.getMessages());
73106
}
74107

75108
@Test
@@ -116,28 +149,15 @@ public void testOptionalPathsObject() {
116149
String infoYaml = "openapi: 3.1.0\n" +
117150
"info:\n" +
118151
" title: Swagger Petstore\n" +
119-
" summary: test summary in info object\n" +
120-
" description: \"This is a sample server Petstore server. You can find out more about\\\n" +
121-
" \\ Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).\\\n" +
122-
" \\ For this sample, you can use the api key `special-key` to test the authorization\\\n" +
123-
" \\ filters.\"\n" +
124-
" termsOfService: http://swagger.io/terms/\n" +
125-
" contact:\n" +
126-
" email: [email protected]\n" +
127-
" license:\n" +
128-
" name: Apache 2.0\n" +
129-
" url: http://www.apache.org/licenses/LICENSE-2.0.html\n" +
130-
" version: 1.0.0\n" +
131-
"servers:\n" +
132-
"- url: /\n";
152+
" version: 1.0.0\n";
133153
SwaggerParseResult result = new OpenAPIV3Parser().readContents( infoYaml, null, null);
134154
assertNotNull(result.getOpenAPI());
135155
assertFalse(result.getMessages().contains("attribute paths is missing"));
136156
}
137157

138158
@Test
139159
public void testValidOpenAPIDocument() {
140-
String infoYaml = "openapi: 3.1.0\n" +
160+
String api = "openapi: 3.1.0\n" +
141161
"info:\n" +
142162
" title: Swagger Petstore\n" +
143163
" summary: test summary in info object\n" +
@@ -154,17 +174,27 @@ public void testValidOpenAPIDocument() {
154174
" version: 1.0.0\n" +
155175
"servers:\n" +
156176
"- url: /\n";
157-
SwaggerParseResult result = new OpenAPIV3Parser().readContents( infoYaml, null, null);
177+
SwaggerParseResult result = new OpenAPIV3Parser().readContents( api, null, null);
158178
assertNotNull(result.getOpenAPI());
159179
assertFalse(result.getMessages().contains("attribute paths is missing"));
160180
assertTrue(result.getMessages().contains("The OpenAPI document MUST contain at least one paths field, a components field or a webhooks field"));
161181
}
162182

163183
@Test
164-
public void testReservedExtensions() {
184+
public void testReservedExtensionsOaiAuthorFalse() {
165185
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/basic.yaml", null, null);
166186
assertNotNull(result.getOpenAPI());
167-
assertTrue(result.getMessages().contains("attribute x-oas-internal is reserved by The OpenAPI Iniciative"));
168-
assertTrue(result.getMessages().contains("attribute x-oai-extension is reserved by The OpenAPI Iniciative"));
187+
assertTrue(result.getMessages().contains("attribute x-oas-internal is reserved by The OpenAPI Initiative"));
188+
assertTrue(result.getMessages().contains("attribute x-oai-extension is reserved by The OpenAPI Initiative"));
189+
}
190+
191+
@Test
192+
public void testReservedExtensionsOaiAuthorTrue() {
193+
ParseOptions options = new ParseOptions();
194+
options.setOaiAuthor(true);
195+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/basic.yaml", null, options);
196+
assertNotNull(result.getOpenAPI());
197+
assertFalse(result.getMessages().contains("attribute x-oas-internal is reserved by The OpenAPI Initiative"));
198+
assertFalse(result.getMessages().contains("attribute x-oai-extension is reserved by The OpenAPI Initiative"));
169199
}
170200
}

0 commit comments

Comments
 (0)