Skip to content

Commit 64cc238

Browse files
gracekarinafrantuma
authored andcommitted
oas 3.1 - add test demostrating 3.0 behavior and 3.1
1 parent 898f7c3 commit 64cc238

File tree

2 files changed

+294
-6
lines changed

2 files changed

+294
-6
lines changed

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

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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.oas.models.OpenAPI;
56
import io.swagger.v3.oas.models.media.Schema;
@@ -20,11 +21,7 @@ public void testBasicOAS31() {
2021
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS31.yaml", null, null);
2122
assertNotNull(result.getOpenAPI());
2223
OpenAPI openAPI = result.getOpenAPI();
23-
System.out.println("Messages: \n");
24-
result.getMessages().forEach(System.out::println);
2524

26-
System.out.println("\n\nParsed: \n\n");
27-
Yaml31.prettyPrint(openAPI);
2825
//JsonSchemaDialect
2926
assertNotNull(openAPI.getJsonSchemaDialect());
3027
//change to bad uri and retest to show the error message
@@ -60,7 +57,7 @@ public void testBasicOAS31() {
6057
//const
6158
assertNotNull(((Schema) openAPI.getComponents().getSchemas().get("Pet").getProperties().get("testconst")).getConst());
6259
assertFalse(result.getMessages().contains("attribute components.schemas.Pet.const is unexpected"));
63-
//exclusiveMaximum-exclusiveMinimum
60+
//exclusiveMaximum-exclusiveMinimum are numeric in 3.1
6461
assertTrue(openAPI.getComponents().getSchemas().get("Pets").getExclusiveMaximumValue().intValue()==12);
6562
assertTrue(openAPI.getComponents().getSchemas().get("Pets").getExclusiveMinimumValue().intValue()==1);
6663
//Null type
@@ -75,6 +72,56 @@ public void testBasicOAS31() {
7572

7673
}
7774

75+
@Test(description = "Test basic OAS30 deserialization/validation if added the fields of OAS3.1")
76+
public void testBasicOAS30_With31Fields() {
77+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS30.yaml", null, null);
78+
assertNotNull(result.getOpenAPI());
79+
OpenAPI openAPI = result.getOpenAPI();
80+
81+
//No JsonSchemaDialect
82+
assertNull(openAPI.getJsonSchemaDialect());
83+
assertTrue(result.getMessages().contains("attribute jsonSchemaDialect is unexpected"));
84+
//info: only description is parsed
85+
assertNotNull(openAPI.getInfo().getDescription());
86+
assertEquals(openAPI.getInfo().getDescription(), "description in info object");
87+
assertTrue(result.getMessages().contains("attribute info.summary is unexpected"));
88+
//license: No identifier
89+
assertNull(openAPI.getInfo().getLicense().getIdentifier());
90+
assertTrue(result.getMessages().contains("attribute info.license.identifier is unexpected"));
91+
//No pathItems under components
92+
assertNull(openAPI.getComponents().getPathItems());
93+
assertTrue(result.getMessages().contains("attribute components.pathItems is unexpected"));
94+
//Type array without items but with error message
95+
assertEquals(openAPI.getComponents().getSchemas().get("ArrayWithoutItems").getType(),"array");
96+
assertNull(openAPI.getComponents().getSchemas().get("ArrayWithoutItems").getItems());
97+
assertTrue(result.getMessages().contains("attribute components.schemas.ArrayWithoutItems.items is missing"));
98+
//Type object with items: not allowed, it will internally create an array (New Option setDefaultSchemaTypeObject )
99+
assertNotEquals(openAPI.getComponents().getSchemas().get("ItemsWithoutArrayType").getType(),"object");
100+
assertEquals(openAPI.getComponents().getSchemas().get("ItemsWithoutArrayType").getType(),"array");
101+
assertNotNull(openAPI.getComponents().getSchemas().get("ItemsWithoutArrayType").getItems());
102+
assertTrue(result.getMessages().contains("attribute components.schemas.ItemsWithoutArrayType.item1 is unexpected"));
103+
//Type field as array not deserialized
104+
assertNull(openAPI.getComponents().getSchemas().get("Pet").getTypes());
105+
//JsonSchema
106+
//arbitrary keywords are not allowed
107+
assertNull(openAPI.getComponents().getSchemas().get("Pet").getExtensions());
108+
assertTrue(result.getMessages().contains("attribute components.schemas.Pet.arbitraryKeyword is unexpected"));
109+
//const
110+
assertNull(((Schema) openAPI.getComponents().getSchemas().get("Pet").getProperties().get("testconst")).getConst());
111+
assertTrue(result.getMessages().contains("attribute components.schemas.Pet.const is unexpected"));
112+
//exclusiveMaximum-exclusiveMinimum are boolean in 3.0
113+
assertNull(openAPI.getComponents().getSchemas().get("Pets").getExclusiveMaximum());
114+
assertNull(openAPI.getComponents().getSchemas().get("Pets").getExclusiveMinimum());
115+
//Null type
116+
assertNull(openAPI.getComponents().getSchemas().get("Pets").getTypes());
117+
//default value independence
118+
assertNull(openAPI.getComponents().getSchemas().get("Pets").getDefault());
119+
//not setting the type by default
120+
assertNull(openAPI.getComponents().getSchemas().get("MapAnyValue").getTypes());
121+
//Not webhooks
122+
assertTrue(result.getMessages().contains("attribute webhooks is unexpected"));
123+
}
124+
78125
@Test
79126
public void testDeserializeSimpleDefinition() throws Exception {
80127
String json =
@@ -593,7 +640,6 @@ public void testSiblingsReferenceJSONSchema6() {
593640
SwaggerParseResult result = new OpenAPIV3Parser().readContents( refSibling , null, options);
594641
OpenAPI openAPI = result.getOpenAPI();
595642
assertNotNull(openAPI);
596-
Yaml31.prettyPrint(openAPI);
597643
//if - then - else
598644
assertNotNull(openAPI.getComponents().getSchemas().get("IfTest"));
599645
Schema itTest = openAPI.getComponents().getSchemas().get("IfTest");
@@ -829,4 +875,40 @@ public void testNotDefaultSchemaType() {
829875
assertNull(schema.getItems().getNullable());
830876
assertNotNull(schema.getItems().getExtensions().get("nullable"));
831877
}
878+
879+
@Test(description = "Test how 3.0 spec deserializes with ref schema and other fields")
880+
public void test30NonRefSiblingsBehavior() {
881+
ParseOptions options = new ParseOptions();
882+
String refSibling = "openapi: 3.0.0\n" +
883+
"info:\n" +
884+
" title: siblings JSONSchema\n" +
885+
" version: 1.0.0\n" +
886+
"servers:\n" +
887+
" - url: /\n" +
888+
"paths: { }\n" +
889+
"components:\n" +
890+
" schemas:\n" +
891+
" profile:\n" +
892+
" description: siblings refs\n" +
893+
" required:\n" +
894+
" - login\n" +
895+
" - password\n" +
896+
" maxItems: 2\n" +
897+
" $ref: ./ex.json#user-profile\n" +
898+
" properties:\n" +
899+
" login:\n" +
900+
" type: string\n" +
901+
" password:\n" +
902+
" type: string";
903+
SwaggerParseResult result = new OpenAPIV3Parser().readContents( refSibling , null, options);
904+
OpenAPI openAPI = result.getOpenAPI();
905+
assertNotNull(openAPI);
906+
Schema profile = openAPI.getComponents().getSchemas().get("profile");
907+
assertNotNull(profile.get$ref());
908+
assertNull(profile.getMaxItems());
909+
assertNull(profile.getDescription());
910+
assertNull(profile.getRequired());
911+
assertNull(profile.getProperties());
912+
913+
}
832914
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
openapi: "3.0.0"
2+
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
3+
info:
4+
version: 1.0.0
5+
summary: test summary in info object
6+
description: description in info object
7+
title: Swagger Petstore
8+
license:
9+
name: MIT
10+
identifier: test identifier
11+
servers:
12+
- url: http://petstore.swagger.io/v1
13+
- url: http://{host}.swagger.io/v1
14+
variables:
15+
host:
16+
default: demo
17+
description: this value is assigned by the service provider
18+
enum: []
19+
webhooks:
20+
# Each webhook needs a name
21+
newPet:
22+
# This is a Path Item Object, the only difference is that the request is initiated by the API provider
23+
post:
24+
requestBody:
25+
description: Information about a new pet in the system
26+
content:
27+
application/json:
28+
schema:
29+
$ref: "#/components/schemas/Pet"
30+
description: pet
31+
responses:
32+
"200":
33+
description: Return a 200 status to indicate that the data was received successfully
34+
paths:
35+
/pets:
36+
get:
37+
summary: List all pets
38+
operationId: listPets
39+
tags:
40+
- pets
41+
parameters:
42+
- name: limit
43+
in: query
44+
description: How many items to return at one time (max 100)
45+
required: false
46+
schema:
47+
type: integer
48+
format: int32
49+
responses:
50+
"200":
51+
description: An paged array of pets
52+
headers:
53+
x-next:
54+
description: A link to the next page of responses
55+
schema:
56+
type: string
57+
content:
58+
application/json:
59+
schema:
60+
$ref: "#/components/schemas/Pets"
61+
default:
62+
description: unexpected error
63+
content:
64+
application/json:
65+
schema:
66+
$ref: "#/components/schemas/Error"
67+
description: error
68+
post:
69+
summary: Create a pet
70+
operationId: createPets
71+
tags:
72+
- pets
73+
responses:
74+
"201":
75+
description: Null response
76+
default:
77+
description: unexpected error
78+
content:
79+
application/json:
80+
schema:
81+
$ref: "#/components/schemas/Tag"
82+
/pets/{petId}:
83+
get:
84+
summary: Info for a specific pet
85+
operationId: showPetById
86+
tags:
87+
- pets
88+
parameters:
89+
- name: petId
90+
in: path
91+
required: true
92+
description: The id of the pet to retrieve
93+
schema:
94+
type: string
95+
- $ref: "#/components/parameters/User"
96+
description: user
97+
summary: user
98+
responses:
99+
"200":
100+
description: Expected response to a valid request
101+
content:
102+
application/json:
103+
schema:
104+
$ref: "#/components/schemas/Pets"
105+
default:
106+
description: unexpected error
107+
content:
108+
application/json:
109+
schema:
110+
$ref: "#/components/schemas/Error"
111+
components:
112+
pathItems:
113+
pets:
114+
get:
115+
description: Returns all pets from the system that the user has access to
116+
responses:
117+
'200':
118+
description: A list of pets.
119+
content:
120+
application/json:
121+
schema:
122+
type: array
123+
items:
124+
$ref: '#/components/schemas/pet'
125+
parameters:
126+
User:
127+
in: query
128+
description: user
129+
name: user
130+
schema:
131+
type: string
132+
schemas:
133+
Pet:
134+
type:
135+
- object
136+
- string
137+
- array
138+
required:
139+
- id
140+
- name
141+
properties:
142+
id:
143+
type: integer
144+
format: int64
145+
name:
146+
type: string
147+
writeOnly: true
148+
readOnly: true
149+
testenum:
150+
type: string
151+
enum:
152+
- available
153+
- pending
154+
- sold
155+
default: available
156+
testconst:
157+
type: string
158+
const: pending
159+
tag:
160+
type: string
161+
arbitraryKeyword: test
162+
Pets:
163+
type: 'null'
164+
default: "I'm a string"
165+
exclusiveMaximum: 12
166+
exclusiveMinimum: 1
167+
items:
168+
$ref: "#/components/schemas/Pet"
169+
ArrayWithoutItems:
170+
type: array
171+
ItemsWithoutArrayType:
172+
type: object
173+
items:
174+
item1:
175+
type: object
176+
item2:
177+
type: string
178+
item3: what
179+
Error:
180+
required:
181+
- code
182+
- message
183+
properties:
184+
code:
185+
type: integer
186+
format: int32
187+
message:
188+
type: string
189+
tag:
190+
$ref: "#/components/schemas/Tag"
191+
Tag:
192+
type:
193+
- object
194+
- string
195+
- string
196+
- foo
197+
properties:
198+
id:
199+
type: integer
200+
format: int64
201+
name:
202+
type: string
203+
patternProperties:
204+
'[a-b].*': 1
205+
MapAnyValue:
206+
additionalProperties: { }

0 commit comments

Comments
 (0)