Skip to content

Commit 67eacbc

Browse files
gracekarinafrantuma
authored andcommitted
oas 3.1 - test for oas31 support implementation review
1 parent 6b678d1 commit 67eacbc

File tree

3 files changed

+143
-10
lines changed

3 files changed

+143
-10
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public ServerVariable getServerVariable(ObjectNode obj, String location, ParseRe
810810
//PathsObject
811811
public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
812812
final Paths paths = new Paths();
813-
if (getPathItems(obj, location, result, paths)) {
813+
if (getPathItems(obj, location, result, paths, false)) {
814814
return paths;
815815
}
816816
return null;
@@ -842,13 +842,13 @@ public Map<String, PathItem> getPathItems(ObjectNode node, String location, Pars
842842
//Webhooks
843843
public Map<String, PathItem> getWebhooks(ObjectNode obj, String location, ParseResult result) {
844844
final Map<String, PathItem> webhooks = new LinkedHashMap<>();
845-
if (getPathItems(obj, location, result, webhooks)) {
845+
if (getPathItems(obj, location, result, webhooks, true)) {
846846
return webhooks;
847847
}
848848
return null;
849849
}
850850

851-
protected boolean getPathItems(ObjectNode obj, String location, ParseResult result, Map<String, PathItem> paths) {
851+
protected boolean getPathItems(ObjectNode obj, String location, ParseResult result, Map<String, PathItem> paths, boolean isWebhook) {
852852
if (obj == null) {
853853
return false;
854854
}
@@ -864,7 +864,7 @@ protected boolean getPathItems(ObjectNode obj, String location, ParseResult resu
864864
if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) {
865865
result.invalidType(location, pathName, "object", pathValue);
866866
} else {
867-
if (!pathName.startsWith("/")) {
867+
if (!pathName.startsWith("/") && !isWebhook ) {
868868
result.warning(location, " Resource " + pathName + " should start with /");
869869
}
870870
ObjectNode path = (ObjectNode) pathValue;
@@ -2180,7 +2180,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
21802180

21812181
return header;
21822182
}
2183-
2183+
//TODO rename method as is used by different objects not only to get examples
21842184
public Object getAnyExample(String nodeKey, ObjectNode node, String location, ParseResult result) {
21852185
JsonNode example = node.get(nodeKey);
21862186
if (example != null) {
@@ -3743,11 +3743,17 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
37433743
}
37443744

37453745

3746+
37463747
String value = getString("title", node, false, location, result);
37473748
if (StringUtils.isNotBlank(value)) {
37483749
schema.setTitle(value);
37493750
}
37503751

3752+
if (node.get("default") != null) {
3753+
//TODO rename method
3754+
schema.setDefault(getAnyExample("default",node,location,result));
3755+
}
3756+
37513757
ObjectNode discriminatorNode = getObject("discriminator", node, false, location, result);
37523758
if (discriminatorNode != null) {
37533759
schema.setDiscriminator(getDiscriminator(discriminatorNode, location, result));

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

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.swagger.v3.parser.test;
22

3-
import io.swagger.v3.core.util.Yaml;
43
import io.swagger.v3.core.util.Yaml31;
54
import io.swagger.v3.oas.models.OpenAPI;
65
import io.swagger.v3.oas.models.media.Schema;
@@ -10,12 +9,69 @@
109
import io.swagger.v3.parser.core.models.SwaggerParseResult;
1110
import org.testng.annotations.Test;
1211

13-
import java.util.ArrayList;
1412
import java.util.List;
1513

1614
import static org.testng.Assert.*;
1715

1816
public class OAI31DeserializationTest {
17+
18+
@Test(description = "Test basic OAS31 deserialization/validation")
19+
public void testBasicOAS31() {
20+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS31.yaml", null, null);
21+
assertNotNull(result.getOpenAPI());
22+
OpenAPI openAPI = result.getOpenAPI();
23+
System.out.println("Messages: "+result.getMessages());
24+
Yaml31.prettyPrint(openAPI);
25+
//JsonSchemaDialect
26+
assertNotNull(openAPI.getJsonSchemaDialect());
27+
//change to bad uri and retest to show the error message
28+
assertEquals(openAPI.getJsonSchemaDialect(), "https://json-schema.org/draft/2020-12/schema");
29+
//info: description and summary
30+
assertNotNull(openAPI.getInfo().getSummary());
31+
assertEquals(openAPI.getInfo().getSummary(), "test summary in info object");
32+
assertNotNull(openAPI.getInfo().getDescription());
33+
assertEquals(openAPI.getInfo().getDescription(), "description in info object");
34+
//license: identifier
35+
assertNotNull(openAPI.getInfo().getLicense().getIdentifier());
36+
assertEquals(openAPI.getInfo().getLicense().getIdentifier(), "test identifier");
37+
//pathItems under components
38+
assertNotNull(openAPI.getComponents().getPathItems());
39+
assertNotNull(openAPI.getComponents().getPathItems().get("pets"));
40+
//Type array without items
41+
assertTrue(openAPI.getComponents().getSchemas().get("ArrayWithoutItems").getTypes().contains("array"));
42+
assertNull(openAPI.getComponents().getSchemas().get("ArrayWithoutItems").getItems());
43+
assertFalse(result.getMessages().contains("attribute components.schemas.ArrayWithoutItems.items is missing"));
44+
//Type object with items
45+
assertTrue(openAPI.getComponents().getSchemas().get("ItemsWithoutArrayType").getTypes().contains("object"));
46+
assertNotNull(openAPI.getComponents().getSchemas().get("ItemsWithoutArrayType").getItems());
47+
assertFalse(result.getMessages().contains("attribute components.schemas.ItemsWithoutArrayType.item1 is unexpected"));
48+
//Type as array
49+
assertTrue(openAPI.getComponents().getSchemas().get("Pet").getTypes().size() == 3);
50+
assertTrue(openAPI.getComponents().getSchemas().get("Pet").getTypes().contains("array"));
51+
assertTrue(openAPI.getComponents().getSchemas().get("Pet").getTypes().contains("string"));
52+
assertTrue(openAPI.getComponents().getSchemas().get("Pet").getTypes().contains("object"));
53+
//JsonSchema
54+
//arbitrary keywords are now allowed
55+
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getExtensions().get("arbitraryKeyword"));
56+
assertFalse(result.getMessages().contains("attribute components.schemas.Pet.arbitraryKeyword is unexpected"));
57+
//const
58+
assertNotNull(((Schema) openAPI.getComponents().getSchemas().get("Pet").getProperties().get("testconst")).getConst());
59+
assertFalse(result.getMessages().contains("attribute components.schemas.Pet.const is unexpected"));
60+
//exclusiveMaximum-exclusiveMinimum
61+
assertTrue(openAPI.getComponents().getSchemas().get("Pets").getExclusiveMaximumValue().intValue()==12);
62+
assertTrue(openAPI.getComponents().getSchemas().get("Pets").getExclusiveMinimumValue().intValue()==1);
63+
//Null type
64+
assertTrue(openAPI.getComponents().getSchemas().get("Pets").getTypes().contains("null"));
65+
//default value independence
66+
assertEquals(openAPI.getComponents().getSchemas().get("Pets").getDefault(), "I'm a string");
67+
//not setting the type by default
68+
assertNull(openAPI.getComponents().getSchemas().get("MapAnyValue").getTypes());
69+
//$ref siblings
70+
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").get$ref());
71+
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getProperties());
72+
73+
}
74+
1975
@Test
2076
public void testDeserializeSimpleDefinition() throws Exception {
2177
String json =
@@ -41,6 +97,7 @@ public void testDeserializeSimpleDefinition() throws Exception {
4197
assertNotNull(result.getOpenAPI());
4298
}
4399

100+
44101
@Test
45102
public void testBasic() {
46103
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/basic.yaml", null, null);
@@ -306,7 +363,7 @@ public void testSiblingsReferenceJSONSchema2() {
306363
assertTrue(profile.getPatternProperties().containsKey("^S_"));
307364
}
308365

309-
@Test(description = "Test siblings with $ref for patternProperties, pattern, additionalProperties")
366+
@Test(description = "Test siblings with $ref for patternProperties, pattern, additionalProperties,exclusiveMaximum,exclusiveMinimum")
310367
public void testSiblingsReferenceJSONSchema3() {
311368
ParseOptions options = new ParseOptions();
312369
String refSibling = "openapi: 3.1.0\n" +
@@ -721,9 +778,46 @@ public void testNotDefaultSchemaType() {
721778
SwaggerParseResult result = new OpenAPIV3Parser().readContents(defaultSchemaType, null, options);
722779
OpenAPI openAPI = result.getOpenAPI();
723780
assertNotNull(openAPI);
781+
782+
//map_any_value as object when it should be null
724783
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value"));
725784
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value") instanceof Schema);
726785
Schema mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value");
727786
assertNull(mapProperty.getType());
787+
788+
//map_any_value_with_desc as object when it should be null
789+
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc"));
790+
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc") instanceof Schema);
791+
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc");
792+
assertNull(mapProperty.getType());
793+
794+
//map_any_value_nullable as object when it should be null
795+
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable"));
796+
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable") instanceof Schema);
797+
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable");
798+
assertNull(mapProperty.getType());
799+
800+
//array_any_value as array when it should be null
801+
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value"));
802+
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value") instanceof Schema);
803+
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value");
804+
assertNull(mapProperty.getType());
805+
806+
//array_any_value_with_desc as array when it should be null
807+
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc"));
808+
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc") instanceof Schema);
809+
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc");
810+
assertNull(mapProperty.getType());
811+
812+
//array_any_value_nullable
813+
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable"));
814+
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable") instanceof Schema);
815+
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable");
816+
assertNull(mapProperty.getType());
817+
assertNull(mapProperty.getItems().getNullable());
818+
Yaml31.prettyPrint(mapProperty);
819+
assertNotNull(mapProperty.getItems().getExtensions().get("nullable"));
820+
//TODO check the toString Method difference between SOUT and YAML31 prettyprint
821+
System.out.println(mapProperty);
728822
}
729823
}

modules/swagger-parser-v3/src/test/resources/3.1.0/test/basicOAS31.yaml

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
openapi: "3.1.0"
2+
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
23
info:
34
version: 1.0.0
5+
summary: test summary in info object
6+
description: description in info object
47
title: Swagger Petstore
58
license:
69
name: MIT
7-
identifier: test
10+
identifier: test identifier
811
servers:
912
- url: http://petstore.swagger.io/v1
1013
webhooks:
@@ -100,6 +103,19 @@ paths:
100103
schema:
101104
$ref: "#/components/schemas/Error"
102105
components:
106+
pathItems:
107+
pets:
108+
get:
109+
description: Returns all pets from the system that the user has access to
110+
responses:
111+
'200':
112+
description: A list of pets.
113+
content:
114+
application/json:
115+
schema:
116+
type: array
117+
items:
118+
$ref: '#/components/schemas/pet'
103119
parameters:
104120
User:
105121
in: query
@@ -112,6 +128,7 @@ components:
112128
type:
113129
- object
114130
- string
131+
- array
115132
required:
116133
- id
117134
- name
@@ -134,10 +151,24 @@ components:
134151
tag:
135152
type: string
136153
arbitraryKeyword: test
154+
$ref: ./ex.json#user-profile
137155
Pets:
138-
type: array
156+
type: 'null'
157+
default: "I'm a string"
158+
exclusiveMaximum: 12
159+
exclusiveMinimum: 1
139160
items:
140161
$ref: "#/components/schemas/Pet"
162+
ArrayWithoutItems:
163+
type: array
164+
ItemsWithoutArrayType:
165+
type: object
166+
items:
167+
item1:
168+
type: object
169+
item2:
170+
type: string
171+
item3: what
141172
Error:
142173
required:
143174
- code
@@ -162,3 +193,5 @@ components:
162193
format: int64
163194
name:
164195
type: string
196+
MapAnyValue:
197+
additionalProperties: { }

0 commit comments

Comments
 (0)