Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for OpenAPI 3.1 schema validation #956

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/test/java/com/networknt/schema/MetaSchemaValidationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.networknt.schema;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.serialization.JsonMapperFactory;

/**
* Tests for meta schema validating a schema.
*/
public class MetaSchemaValidationTest {
/**
* Validates a OpenAPI 3.1 schema using the OpenAPI 3.1 meta schema.
*
* @throws IOException the exception
*/
@Test
void oas31() throws IOException {
try (InputStream input = MetaSchemaValidationTest.class.getResourceAsStream("/schema/oas/v31/petstore.json")) {
JsonNode inputData = JsonMapperFactory.getInstance().readTree(input);
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPathType(PathType.JSON_POINTER);
JsonSchema schema = JsonSchemaFactory
.getInstance(VersionFlag.V202012,
builder -> builder.schemaMappers(schemaMappers -> schemaMappers
.mapPrefix("https://spec.openapis.org/oas/3.1", "classpath:oas/v31")
.mapPrefix("https://json-schema.org", "classpath:")))
.getSchema(SchemaLocation.of("https://spec.openapis.org/oas/3.1/schema-base/2022-10-07"), config);
Set<ValidationMessage> messages = schema.validate(inputData);
assertEquals(0, messages.size());
}
}
}
25 changes: 25 additions & 0 deletions src/test/resources/oas/v31/dialect/base
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$id": "https://spec.openapis.org/oas/3.1/dialect/base",
"$schema": "https://json-schema.org/draft/2020-12/schema",

"title": "OpenAPI 3.1 Schema Object Dialect",
"description": "A JSON Schema dialect describing schemas found in OpenAPI documents",

"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
"https://json-schema.org/draft/2020-12/vocab/unevaluated": true,
"https://json-schema.org/draft/2020-12/vocab/validation": true,
"https://json-schema.org/draft/2020-12/vocab/meta-data": true,
"https://json-schema.org/draft/2020-12/vocab/format-annotation": true,
"https://json-schema.org/draft/2020-12/vocab/content": true,
"https://spec.openapis.org/oas/3.1/vocab/base": false
},

"$dynamicAnchor": "meta",

"allOf": [
{ "$ref": "https://json-schema.org/draft/2020-12/schema" },
{ "$ref": "https://spec.openapis.org/oas/3.1/meta/base" }
]
}
87 changes: 87 additions & 0 deletions src/test/resources/oas/v31/meta/base
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"$id": "https://spec.openapis.org/oas/3.1/meta/base",
"$schema": "https://json-schema.org/draft/2020-12/schema",

"title": "OAS Base vocabulary",
"description": "A JSON Schema Vocabulary used in the OpenAPI Schema Dialect",

"$vocabulary": {
"https://spec.openapis.org/oas/3.1/vocab/base": true
},

"$dynamicAnchor": "meta",

"type": ["object", "boolean"],
"properties": {
"example": true,
"discriminator": { "$ref": "#/$defs/discriminator" },
"externalDocs": { "$ref": "#/$defs/external-docs" },
"xml": { "$ref": "#/$defs/xml" }
},

"$defs": {
"extensible": {
"patternProperties": {
"^x-": true
}
},

"discriminator": {
"$ref": "#/$defs/extensible",
"type": "object",
"properties": {
"propertyName": {
"type": "string"
},
"mapping": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": ["propertyName"],
"unevaluatedProperties": false
},

"external-docs": {
"$ref": "#/$defs/extensible",
"type": "object",
"properties": {
"url": {
"type": "string",
"format": "uri-reference"
},
"description": {
"type": "string"
}
},
"required": ["url"],
"unevaluatedProperties": false
},

"xml": {
"$ref": "#/$defs/extensible",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"namespace": {
"type": "string",
"format": "uri"
},
"prefix": {
"type": "string"
},
"attribute": {
"type": "boolean"
},
"wrapped": {
"type": "boolean"
}
},
"unevaluatedProperties": false
}
}
}
23 changes: 23 additions & 0 deletions src/test/resources/oas/v31/schema-base/2022-10-07
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$id": "https://spec.openapis.org/oas/3.1/schema-base/2022-10-07",
"$schema": "https://json-schema.org/draft/2020-12/schema",

"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",

"$ref": "https://spec.openapis.org/oas/3.1/schema/2022-10-07",
"properties": {
"jsonSchemaDialect": { "$ref": "#/$defs/dialect" }
},

"$defs": {
"dialect": { "const": "https://spec.openapis.org/oas/3.1/dialect/base" },

"schema": {
"$dynamicAnchor": "meta",
"$ref": "https://spec.openapis.org/oas/3.1/dialect/base",
"properties": {
"$schema": { "$ref": "#/$defs/dialect" }
}
}
}
}
Loading
Loading