Skip to content

Commit 32356e9

Browse files
authored
Add test for OpenAPI 3.1 schema validation (#956)
1 parent 937105f commit 32356e9

File tree

6 files changed

+2855
-0
lines changed

6 files changed

+2855
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.util.Set;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import com.fasterxml.jackson.databind.JsonNode;
27+
import com.networknt.schema.SpecVersion.VersionFlag;
28+
import com.networknt.schema.serialization.JsonMapperFactory;
29+
30+
/**
31+
* Tests for meta schema validating a schema.
32+
*/
33+
public class MetaSchemaValidationTest {
34+
/**
35+
* Validates a OpenAPI 3.1 schema using the OpenAPI 3.1 meta schema.
36+
*
37+
* @throws IOException the exception
38+
*/
39+
@Test
40+
void oas31() throws IOException {
41+
try (InputStream input = MetaSchemaValidationTest.class.getResourceAsStream("/schema/oas/v31/petstore.json")) {
42+
JsonNode inputData = JsonMapperFactory.getInstance().readTree(input);
43+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
44+
config.setPathType(PathType.JSON_POINTER);
45+
JsonSchema schema = JsonSchemaFactory
46+
.getInstance(VersionFlag.V202012,
47+
builder -> builder.schemaMappers(schemaMappers -> schemaMappers
48+
.mapPrefix("https://spec.openapis.org/oas/3.1", "classpath:oas/v31")
49+
.mapPrefix("https://json-schema.org", "classpath:")))
50+
.getSchema(SchemaLocation.of("https://spec.openapis.org/oas/3.1/schema-base/2022-10-07"), config);
51+
Set<ValidationMessage> messages = schema.validate(inputData);
52+
assertEquals(0, messages.size());
53+
}
54+
}
55+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$id": "https://spec.openapis.org/oas/3.1/dialect/base",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
5+
"title": "OpenAPI 3.1 Schema Object Dialect",
6+
"description": "A JSON Schema dialect describing schemas found in OpenAPI documents",
7+
8+
"$vocabulary": {
9+
"https://json-schema.org/draft/2020-12/vocab/core": true,
10+
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
11+
"https://json-schema.org/draft/2020-12/vocab/unevaluated": true,
12+
"https://json-schema.org/draft/2020-12/vocab/validation": true,
13+
"https://json-schema.org/draft/2020-12/vocab/meta-data": true,
14+
"https://json-schema.org/draft/2020-12/vocab/format-annotation": true,
15+
"https://json-schema.org/draft/2020-12/vocab/content": true,
16+
"https://spec.openapis.org/oas/3.1/vocab/base": false
17+
},
18+
19+
"$dynamicAnchor": "meta",
20+
21+
"allOf": [
22+
{ "$ref": "https://json-schema.org/draft/2020-12/schema" },
23+
{ "$ref": "https://spec.openapis.org/oas/3.1/meta/base" }
24+
]
25+
}

src/test/resources/oas/v31/meta/base

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"$id": "https://spec.openapis.org/oas/3.1/meta/base",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
5+
"title": "OAS Base vocabulary",
6+
"description": "A JSON Schema Vocabulary used in the OpenAPI Schema Dialect",
7+
8+
"$vocabulary": {
9+
"https://spec.openapis.org/oas/3.1/vocab/base": true
10+
},
11+
12+
"$dynamicAnchor": "meta",
13+
14+
"type": ["object", "boolean"],
15+
"properties": {
16+
"example": true,
17+
"discriminator": { "$ref": "#/$defs/discriminator" },
18+
"externalDocs": { "$ref": "#/$defs/external-docs" },
19+
"xml": { "$ref": "#/$defs/xml" }
20+
},
21+
22+
"$defs": {
23+
"extensible": {
24+
"patternProperties": {
25+
"^x-": true
26+
}
27+
},
28+
29+
"discriminator": {
30+
"$ref": "#/$defs/extensible",
31+
"type": "object",
32+
"properties": {
33+
"propertyName": {
34+
"type": "string"
35+
},
36+
"mapping": {
37+
"type": "object",
38+
"additionalProperties": {
39+
"type": "string"
40+
}
41+
}
42+
},
43+
"required": ["propertyName"],
44+
"unevaluatedProperties": false
45+
},
46+
47+
"external-docs": {
48+
"$ref": "#/$defs/extensible",
49+
"type": "object",
50+
"properties": {
51+
"url": {
52+
"type": "string",
53+
"format": "uri-reference"
54+
},
55+
"description": {
56+
"type": "string"
57+
}
58+
},
59+
"required": ["url"],
60+
"unevaluatedProperties": false
61+
},
62+
63+
"xml": {
64+
"$ref": "#/$defs/extensible",
65+
"type": "object",
66+
"properties": {
67+
"name": {
68+
"type": "string"
69+
},
70+
"namespace": {
71+
"type": "string",
72+
"format": "uri"
73+
},
74+
"prefix": {
75+
"type": "string"
76+
},
77+
"attribute": {
78+
"type": "boolean"
79+
},
80+
"wrapped": {
81+
"type": "boolean"
82+
}
83+
},
84+
"unevaluatedProperties": false
85+
}
86+
}
87+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$id": "https://spec.openapis.org/oas/3.1/schema-base/2022-10-07",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
5+
"description": "The description of OpenAPI v3.1.x documents using the OpenAPI JSON Schema dialect, as defined by https://spec.openapis.org/oas/v3.1.0",
6+
7+
"$ref": "https://spec.openapis.org/oas/3.1/schema/2022-10-07",
8+
"properties": {
9+
"jsonSchemaDialect": { "$ref": "#/$defs/dialect" }
10+
},
11+
12+
"$defs": {
13+
"dialect": { "const": "https://spec.openapis.org/oas/3.1/dialect/base" },
14+
15+
"schema": {
16+
"$dynamicAnchor": "meta",
17+
"$ref": "https://spec.openapis.org/oas/3.1/dialect/base",
18+
"properties": {
19+
"$schema": { "$ref": "#/$defs/dialect" }
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)