Skip to content

Commit 4eb43fb

Browse files
gracekarinafrantuma
authored andcommitted
test for new keys in schema 202012
1 parent ab3ae66 commit 4eb43fb

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public class OpenAPIDeserializer {
133133
"explode", "allowReserved"));
134134

135135
// 3.1
136-
// TODO use a map instead for 3.0 and 3.1. Care about compatibility
137136
protected static Set<String> ROOT_KEYS_31 = new LinkedHashSet<>(Arrays.asList("openapi", "info", "servers", "paths",
138137
"components", "security", "tags", "externalDocs", "webhooks", "jsonSchemaDialect"));
139138
protected static Set<String> RESERVED_KEYWORDS_31 = new LinkedHashSet<>(Arrays.asList("x-oai-","x-oas-"));
@@ -170,7 +169,7 @@ public class OpenAPIDeserializer {
170169
"pattern", "maxItems", "minItems", "uniqueItems", "maxProperties", "minProperties", "required", "enum", "type",
171170
"allOf", "oneOf", "anyOf", "not", "items", "properties", "additionalProperties", "description",
172171
"default", "discriminator", "readOnly", "writeOnly", "xml", "externalDocs", "example", "deprecated",
173-
"const", "examples", "$id", "$comment", "if", "then", "else", "unevaluatedProperties", "prefixItems",
172+
"const", "examples", "$id", "$comment", "if", "then", "else", "unevaluatedProperties","unevaluatedItems", "prefixItems",
174173
"contains","contentEncoding","contentMediaType","$anchor","$schema","contentSchema","propertyNames",
175174
"dependentSchemas","dependentRequired","minContains","maxContains","patternProperties"));
176175
protected static Set<String> EXAMPLE_KEYS_31 = new LinkedHashSet<>(Arrays.asList("$ref", "summary", "description",

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

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

3+
import io.swagger.v3.core.util.Yaml;
4+
import io.swagger.v3.core.util.Yaml31;
35
import io.swagger.v3.oas.models.OpenAPI;
46
import io.swagger.v3.oas.models.media.Schema;
57
import io.swagger.v3.oas.models.security.SecurityRequirement;
@@ -14,6 +16,46 @@
1416

1517
public class OAI31DeserializationTest {
1618

19+
@Test(description = "Test OAS31 new Schema keys deserialization")
20+
public void testSchemaKeysOAS31() {
21+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/petstore-3.1_more.yaml", null, null);
22+
assertNotNull(result.getOpenAPI());
23+
OpenAPI openAPI = result.getOpenAPI();
24+
25+
assertTrue(result.getMessages().size() == 0);
26+
Schema schema2020_12 = openAPI.getComponents().getSchemas().get("Schema2020_12");
27+
assertEquals(schema2020_12.getConst(), "const text");
28+
assertEquals(schema2020_12.get$id(), "schemaId");
29+
assertEquals(schema2020_12.get$comment(), "comment for testing");
30+
assertNotNull(schema2020_12.getIf().getProperties().get("country"));
31+
assertNotNull(schema2020_12.getThen().getProperties().get("maple_trees"));
32+
assertNotNull(schema2020_12.getElse().getProperties().get("accept"));
33+
assertTrue(schema2020_12.getUnevaluatedProperties() instanceof Schema);
34+
Schema unevaluatedProperty = (Schema)schema2020_12.getUnevaluatedProperties();
35+
assertTrue(unevaluatedProperty.getTypes().contains("object"));
36+
assertEquals(schema2020_12.getContentMediaType(), "text/html");
37+
assertEquals(schema2020_12.get$anchor(), "anchor text");
38+
assertEquals(schema2020_12.get$schema(), "https://json-schema.org/draft/2020-12/schema");
39+
assertNotNull(schema2020_12.getExamples().get(0));
40+
assertEquals(schema2020_12.getContentEncoding(), "base64");
41+
assertEquals(schema2020_12.getMinContains(), Integer.valueOf(2));
42+
assertEquals(schema2020_12.getMaxContains(), Integer.valueOf(4));
43+
assertTrue(schema2020_12.getPrefixItems().get(0) instanceof Schema);
44+
Schema prefixItems = (Schema)schema2020_12.getPrefixItems().get(0);
45+
assertEquals(prefixItems.getDescription(), "Name");
46+
assertTrue(schema2020_12.getContains().getTypes().contains("integer"));
47+
assertTrue(schema2020_12.getContentSchema().getTypes().contains("string"));
48+
assertEquals(schema2020_12.getPropertyNames().getPattern(), "^[A-Za-z_][A-Za-z0-9_]*$");
49+
assertTrue(schema2020_12.getDependentSchemas().get("credit_card") instanceof Schema);
50+
Schema dependantSchema = (Schema)schema2020_12.getDependentSchemas().get("credit_card");
51+
assertEquals(dependantSchema.getRequired().get(0), "billing_address");
52+
assertTrue(schema2020_12.getDependentRequired().containsKey("credit_card"));
53+
assertTrue(schema2020_12.getPatternProperties().get("^S_") instanceof Schema);
54+
Schema patternProperties = (Schema)schema2020_12.getPatternProperties().get("^S_");
55+
assertTrue(schema2020_12.getUnevaluatedItems().getTypes().contains("object"));
56+
assertTrue(patternProperties.getTypes().contains("string"));
57+
}
58+
1759
@Test(description = "Test basic OAS31 deserialization/validation")
1860
public void testBasicOAS31() {
1961
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS31.yaml", null, null);
@@ -67,7 +109,6 @@ public void testBasicOAS31() {
67109
//$ref siblings
68110
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").get$ref());
69111
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getProperties());
70-
71112
}
72113

73114
@Test(description = "Test basic OAS30 deserialization/validation if added the fields of OAS3.1")
@@ -558,9 +599,9 @@ public void testSiblingsReferenceJSONSchema5() {
558599
assertNotNull(patientPersonSchema.getUnevaluatedProperties());
559600
assertTrue(patientPersonSchema.getUnevaluatedProperties() instanceof Boolean);
560601
assertFalse(((Boolean)patientPersonSchema.getUnevaluatedProperties()).booleanValue());
561-
562602
//unevaluatedItems
563603
assertNotNull(profile.getUnevaluatedItems());
604+
564605
}
565606

566607
@Test(description = "Test siblings with $ref for if - then - else, dependentRequired, dependentSchemas")
@@ -656,6 +697,7 @@ public void testSiblingsReferenceJSONSchema6() {
656697
//dependentSchemas
657698
assertNotNull(openAPI.getComponents().getSchemas().get("PaymentMethod"));
658699
Schema paymentMethod = openAPI.getComponents().getSchemas().get("PaymentMethod");
700+
659701
}
660702

661703
@Test(description = "Test examples in JSONSchema")

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,78 @@ components:
119119
$ref: "#/components/schemas/Pet"
120120
description: desc
121121
format: int32
122+
Schema2020_12:
123+
type: object
124+
title: schema 2020-12
125+
required:
126+
- country
127+
properties:
128+
country:
129+
enum:
130+
- usa
131+
- canada
132+
- eu
133+
default: eu
134+
type: string
135+
widget: Select
136+
if:
137+
properties:
138+
country:
139+
const: canada
140+
type: string
141+
then:
142+
properties:
143+
maple_trees:
144+
type: number
145+
else:
146+
required:
147+
- accept
148+
properties:
149+
accept:
150+
const: "true"
151+
type: boolean
152+
const: const text
153+
examples:
154+
- sample1
155+
- sample2
156+
$id: schemaId
157+
$comment: comment for testing
158+
propertyNames:
159+
pattern: "^[A-Za-z_][A-Za-z0-9_]*$"
160+
unevaluatedProperties:
161+
type: object
162+
unevaluatedItems:
163+
type: object
164+
prefixItems:
165+
- description: Name
166+
type: string
167+
- description: Age
168+
type: integer
169+
contains:
170+
type: integer
171+
maxContains: 4
172+
minContains: 2
173+
$anchor: anchor text
174+
$schema: https://json-schema.org/draft/2020-12/schema
175+
contentSchema:
176+
type: string
177+
dependentSchemas:
178+
credit_card:
179+
required:
180+
- billing_address
181+
properties:
182+
billing_address:
183+
type: string
184+
dependentRequired:
185+
credit_card:
186+
- billing_address
187+
patternProperties:
188+
^S_:
189+
type: string
190+
^I_:
191+
type: integer
192+
contentEncoding: base64
193+
contentMediaType: text/html
122194
Error:
123195
required:
124196
- code

0 commit comments

Comments
 (0)