Skip to content

Commit 99af065

Browse files
gracekarinafrantuma
authored andcommitted
oas 3.1 - info.summary - components.pathItem - discriminator.extension
1 parent 2bd4335 commit 99af065

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class OpenAPIDeserializer {
138138
// TODO use a map instead for 3.0 and 3.1. Care about compatibility
139139
protected static Set<String> ROOT_KEYS_31 = new LinkedHashSet<>(Arrays.asList("openapi", "info", "servers", "paths",
140140
"components", "security", "tags", "externalDocs", "webhooks"));
141-
protected static Set<String> INFO_KEYS_31 = new LinkedHashSet<>(Arrays.asList("title", "description", "termsOfService"
141+
protected static Set<String> INFO_KEYS_31 = new LinkedHashSet<>(Arrays.asList("title","summary", "description", "termsOfService"
142142
, "contact", "license", "version"));
143143
protected static Set<String> CONTACT_KEYS_31 = new LinkedHashSet<>(Arrays.asList("name", "url", "email"));
144144
protected static Set<String> LICENSE_KEYS_31 = new LinkedHashSet<>(Arrays.asList("name", "url", "identifier"));
@@ -163,7 +163,7 @@ public class OpenAPIDeserializer {
163163
protected static Set<String> SECURITY_SCHEME_KEYS_31 = new LinkedHashSet<>(Arrays.asList("$ref", "type", "name", "in"
164164
, "description", "flows", "scheme", "bearerFormat", "openIdConnectUrl"));
165165
protected static Set<String> EXTERNAL_DOCS_KEYS_31 = new LinkedHashSet<>(Arrays.asList("description", "url"));
166-
protected static Set<String> COMPONENTS_KEYS_31 = new LinkedHashSet<>(Arrays.asList("schemas", "responses",
166+
protected static Set<String> COMPONENTS_KEYS_31 = new LinkedHashSet<>(Arrays.asList("schemas", "responses", "pathItems",
167167
"parameters", "examples", "requestBodies", "headers", "securitySchemes", "links", "callbacks"));
168168
protected static Set<String> SCHEMA_KEYS_31 = new LinkedHashSet<>(Arrays.asList("$ref", "title", "multipleOf",
169169
"maximum", "format", "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxLength", "minLength",
@@ -463,6 +463,12 @@ public Components getComponents(ObjectNode obj, String location, ParseResult res
463463
components.setResponses(getResponses(node, String.format("%s.%s", location, "responses"), result,
464464
true));
465465
}
466+
if(result.isOpenapi31()){
467+
node = getObject("pathItems", obj, false, location, result);
468+
if (node != null) {
469+
components.setPathItems(getPathItems(node, String.format("%s.%s", location, "pathItems"), result, true));
470+
}
471+
}
466472

467473
node = getObject("parameters", obj, false, location, result);
468474
if (node != null) {
@@ -758,6 +764,29 @@ public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
758764
return null;
759765
}
760766

767+
public Map<String, PathItem> getPathItems(ObjectNode node, String location, ParseResult result,
768+
boolean underComponents) {
769+
if (node == null) {
770+
return null;
771+
}
772+
Map<String, PathItem> pathItems = new LinkedHashMap<>();
773+
Set<String> keys = getKeys(node);
774+
for (String key : keys) {
775+
if (underComponents) {
776+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
777+
key)) {
778+
result.warning(location, "PathItem key " + key + " doesn't adhere to regular expression " +
779+
"^[a-zA-Z0-9\\.\\-_]+$");
780+
}
781+
}
782+
PathItem pathItem = getPathItem((ObjectNode) node.get(key), location, result);
783+
if (pathItem != null) {
784+
pathItems.put(key, pathItem);
785+
}
786+
}
787+
return pathItems;
788+
}
789+
761790
//Webhooks
762791
public Map<String, PathItem> getWebhooks(ObjectNode obj, String location, ParseResult result) {
763792
final Map<String, PathItem> webhooks = new LinkedHashMap<>();
@@ -1104,6 +1133,13 @@ public Info getInfo(ObjectNode node, String location, ParseResult result) {
11041133
info.setDescription(value);
11051134
}
11061135

1136+
if(result.isOpenapi31()) {
1137+
value = getString("summary", node, false, location, result);
1138+
if (StringUtils.isNotBlank(value)) {
1139+
info.setSummary(value);
1140+
}
1141+
}
1142+
11071143
value = getString("termsOfService", node, false, location, result);
11081144
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
11091145
info.setTermsOfService(value);
@@ -1491,6 +1527,8 @@ private Map<String, String> getLinkParameters(ObjectNode parametersObject, Strin
14911527
return linkParameters;
14921528
}
14931529

1530+
1531+
14941532
public Map<String, Callback> getCallbacks(ObjectNode node, String location, ParseResult result,
14951533
boolean underComponents) {
14961534
if (node == null) {
@@ -1514,6 +1552,7 @@ public Map<String, Callback> getCallbacks(ObjectNode node, String location, Pars
15141552
return callbacks;
15151553
}
15161554

1555+
15171556
public Callback getCallback(ObjectNode node, String location, ParseResult result) {
15181557
if (node == null) {
15191558
return null;
@@ -2390,6 +2429,18 @@ public Discriminator getDiscriminator(ObjectNode node, String location, ParseRes
23902429
discriminator.setMapping(mapping);
23912430
}
23922431

2432+
if(result.isOpenapi31()) {
2433+
Set<String> keys = getKeys(node);
2434+
for (String key : keys) {
2435+
if (key.startsWith("x-")) {
2436+
Map<String, Object> extensions = getExtensions(node);
2437+
if (extensions != null && extensions.size() > 0) {
2438+
discriminator.setExtensions(extensions);
2439+
}
2440+
}
2441+
}
2442+
}
2443+
23932444
return discriminator;
23942445

23952446
}

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.swagger.v3.parser.test;
22

3+
import io.swagger.v3.core.util.Yaml;
34
import io.swagger.v3.core.util.Yaml31;
45
import io.swagger.v3.parser.OpenAPIV3Parser;
56
import io.swagger.v3.parser.core.models.ParseOptions;
67
import io.swagger.v3.parser.core.models.SwaggerParseResult;
78
import org.testng.annotations.Test;
89

910
import static org.testng.Assert.assertNotNull;
11+
import static org.testng.Assert.assertFalse;
1012

1113
public class OAI31DeserializationTest {
1214
@Test
@@ -31,7 +33,6 @@ public void testDeserializeSimpleDefinition() throws Exception {
3133
ParseOptions options = new ParseOptions();
3234
options.setResolve(true);
3335
SwaggerParseResult result = new OpenAPIV3Parser().readContents(json, null, options);
34-
3536
assertNotNull(result.getOpenAPI());
3637
}
3738

@@ -42,4 +43,45 @@ public void testBasic() {
4243
//assertEquals(result.getMessages().size(),1);
4344
assertNotNull(result.getOpenAPI());
4445
}
46+
47+
@Test
48+
public void testInfo() {
49+
String infoYaml = "openapi: 3.1.0\n" +
50+
"info:\n" +
51+
" title: Swagger Petstore\n" +
52+
" summary: test summary in info object\n" +
53+
" description: \"This is a sample server Petstore server. You can find out more about\\\n" +
54+
" \\ Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).\\\n" +
55+
" \\ For this sample, you can use the api key `special-key` to test the authorization\\\n" +
56+
" \\ filters.\"\n" +
57+
" termsOfService: http://swagger.io/terms/\n" +
58+
" contact:\n" +
59+
" email: [email protected]\n" +
60+
" license:\n" +
61+
" name: Apache 2.0\n" +
62+
" url: http://www.apache.org/licenses/LICENSE-2.0.html\n" +
63+
" version: 1.0.0\n" +
64+
"servers:\n" +
65+
"- url: /\n";
66+
SwaggerParseResult result = new OpenAPIV3Parser().readContents( infoYaml, null, null);
67+
assertNotNull(result.getOpenAPI());
68+
assertNotNull(result.getOpenAPI().getInfo().getSummary());
69+
assertFalse(result.getMessages().contains("attribute info.summary is unexpected"));
70+
}
71+
72+
@Test
73+
public void testPathsItemsUnderComponents() {
74+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/petstore-3.1_more.yaml", null, null);
75+
assertNotNull(result.getOpenAPI());
76+
assertNotNull(result.getOpenAPI().getComponents().getPathItems());
77+
assertFalse(result.getMessages().contains("attribute components.pathItems is unexpected"));
78+
79+
}
80+
81+
@Test
82+
public void testDiscriminatorExtensions() {
83+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/petstore-3.1_more.yaml", null, null);
84+
assertNotNull(result.getOpenAPI());
85+
assertNotNull(result.getOpenAPI().getComponents().getSchemas().get("DiscriminatorExtension").getDiscriminator().getExtensions().get("x-extension"));
86+
}
4587
}

modules/swagger-parser-v3/src/test/resources/3.1.0/petstore-3.1_more.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ paths:
2828
operationId: listPets
2929
tags:
3030
- pets
31+
x-extension: test
3132
parameters:
3233
- name: limit
3334
in: query
@@ -128,3 +129,25 @@ components:
128129
format: int32
129130
message:
130131
type: string
132+
DiscriminatorExtension:
133+
oneOf:
134+
- $ref: '#/components/schemas/Cat'
135+
- $ref: '#/components/schemas/Dog'
136+
- $ref: '#/components/schemas/Lizard'
137+
discriminator:
138+
propertyName: petType
139+
x-extension: test
140+
pathItems:
141+
pets:
142+
get:
143+
description: Returns all pets from the system that the user has access to
144+
responses:
145+
'200':
146+
description: A list of pets.
147+
content:
148+
application/json:
149+
schema:
150+
type: array
151+
items:
152+
$ref: '#/components/schemas/pet'
153+

0 commit comments

Comments
 (0)