From 0b7d1b8e06fb8b4f0cd7fb3ca6aa0ee59b88b91d Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Fri, 14 Jun 2024 09:35:17 +0800 Subject: [PATCH 1/5] Add object reader --- README.md | 8 +- .../networknt/schema/JsonSchemaFactory.java | 55 +++--- .../serialization/DefaultObjectReader.java | 173 ++++++++++++++++++ .../schema/serialization/ObjectReader.java | 57 ++++++ .../DefaultObjectReaderTest.java | 104 +++++++++++ .../networknt/schema/utils/JsonNodesTest.java | 7 +- 6 files changed, 374 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java create mode 100644 src/main/java/com/networknt/schema/serialization/ObjectReader.java create mode 100644 src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java diff --git a/README.md b/README.md index cfb0f92df..ea9e3140c 100644 --- a/README.md +++ b/README.md @@ -353,8 +353,8 @@ Assertions contains the following additional information | Arguments | The arguments used for generating the message. | Type | The keyword that generated the message. | Property | The property name that caused the validation error for example for the `required` keyword. Note that this is not part of the instance location as that points to the instance node. -| Schema Node | The `JsonNode` pointed to by the Schema Location. This is the schema data that caused the input data to fail. It is possible to get the location information by configuring the `JsonSchemaFactory` with the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(schemaNode)`. -| Instance Node | The `JsonNode` pointed to by the Instance Location. This is the input data that failed validation. It is possible to get the location information by configuring the `JsonSchemaFactory` with the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(instanceNode)`. +| Schema Node | The `JsonNode` pointed to by the Schema Location. This is the schema data that caused the input data to fail. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `ObjectReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(schemaNode)`. +| Instance Node | The `JsonNode` pointed to by the Instance Location. This is the input data that failed validation. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `ObjectReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(instanceNode)`. | Details | Additional details that can be set by custom keyword validator implementations. This is not used by the library. Annotations contains the following additional information @@ -367,7 +367,7 @@ Annotations contains the following additional information The library can be configured to store line and column information in the `JsonNode` instances for the instance and schema nodes. This will adversely affect performance and is not configured by default. -This is done by configuring a `LocationJsonNodeFactoryFactory` on the `JsonSchemaFactory`. The `JsonLocation` information can then be retrieved using `JsonNodes.tokenLocationOf(jsonNode)`. +This is done by configuring a `ObjectReader` that uses the `LocationJsonNodeFactoryFactory`on the `JsonSchemaFactory`. The `JsonLocation` information can then be retrieved using `JsonNodes.tokenLocationOf(jsonNode)`. ```java String schemaData = "{\r\n" @@ -383,7 +383,7 @@ String inputData = "{\r\n" + " \"startDate\": \"1\"\r\n" + "}"; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, - builder -> builder.jsonNodeFactoryFactory(LocationJsonNodeFactoryFactory.getInstance())); + builder -> builder.objectReader(ObjectReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config); diff --git a/src/main/java/com/networknt/schema/JsonSchemaFactory.java b/src/main/java/com/networknt/schema/JsonSchemaFactory.java index ce8363dad..b70ed180c 100644 --- a/src/main/java/com/networknt/schema/JsonSchemaFactory.java +++ b/src/main/java/com/networknt/schema/JsonSchemaFactory.java @@ -25,9 +25,8 @@ import com.networknt.schema.resource.SchemaMapper; import com.networknt.schema.resource.SchemaMappers; import com.networknt.schema.serialization.JsonMapperFactory; +import com.networknt.schema.serialization.ObjectReader; import com.networknt.schema.serialization.YamlMapperFactory; -import com.networknt.schema.serialization.node.JsonNodeFactoryFactory; -import com.networknt.schema.utils.JsonNodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +55,7 @@ public class JsonSchemaFactory { public static class Builder { private ObjectMapper jsonMapper = null; private ObjectMapper yamlMapper = null; - private JsonNodeFactoryFactory jsonNodeFactoryFactory = null; + private ObjectReader objectReader = null; private String defaultMetaSchemaIri; private final ConcurrentMap metaSchemas = new ConcurrentHashMap(); private SchemaLoaders.Builder schemaLoadersBuilder = null; @@ -65,25 +64,39 @@ public static class Builder { private JsonMetaSchemaFactory metaSchemaFactory = null; /** - * Configures the {@link JsonNodeFactoryFactory} to use. + * Sets the object reader to read the data. *

- * To get location information from {@link JsonNode} the - * {@link com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory} - * can be used. + * If set this takes precedence over the configured json mapper and yaml mapper. * - * @param jsonNodeFactoryFactory the factory to create json node factories + * @param objectReader the object reader * @return the builder */ - public Builder jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) { - this.jsonNodeFactoryFactory = jsonNodeFactoryFactory; + public Builder objectReader(ObjectReader objectReader) { + this.objectReader = objectReader; return this; } + /** + * Sets the json mapper to read the data. + *

+ * If the object reader is set this will not be used. + * + * @param jsonMapper the json mapper + * @return the builder + */ public Builder jsonMapper(final ObjectMapper jsonMapper) { this.jsonMapper = jsonMapper; return this; } + /** + * Sets the yaml mapper to read the data. + *

+ * If the object reader is set this will not be used. + * + * @param yamlMapper the yaml mapper + * @return the builder + */ public Builder yamlMapper(final ObjectMapper yamlMapper) { this.yamlMapper = yamlMapper; return this; @@ -151,7 +164,7 @@ public JsonSchemaFactory build() { return new JsonSchemaFactory( jsonMapper, yamlMapper, - jsonNodeFactoryFactory, + objectReader, defaultMetaSchemaIri, schemaLoadersBuilder, schemaMappersBuilder, @@ -164,7 +177,7 @@ public JsonSchemaFactory build() { private final ObjectMapper jsonMapper; private final ObjectMapper yamlMapper; - private final JsonNodeFactoryFactory jsonNodeFactoryFactory; + private final ObjectReader objectReader; private final String defaultMetaSchemaIri; private final SchemaLoaders.Builder schemaLoadersBuilder; private final SchemaMappers.Builder schemaMappersBuilder; @@ -180,7 +193,7 @@ public JsonSchemaFactory build() { private JsonSchemaFactory( ObjectMapper jsonMapper, ObjectMapper yamlMapper, - JsonNodeFactoryFactory jsonNodeFactoryFactory, + ObjectReader objectReader, String defaultMetaSchemaIri, SchemaLoaders.Builder schemaLoadersBuilder, SchemaMappers.Builder schemaMappersBuilder, @@ -197,7 +210,7 @@ private JsonSchemaFactory( } this.jsonMapper = jsonMapper; this.yamlMapper = yamlMapper; - this.jsonNodeFactoryFactory = jsonNodeFactoryFactory; + this.objectReader = objectReader; this.defaultMetaSchemaIri = defaultMetaSchemaIri; this.schemaLoadersBuilder = schemaLoadersBuilder; this.schemaMappersBuilder = schemaMappersBuilder; @@ -290,7 +303,7 @@ public static Builder builder(final JsonSchemaFactory blueprint) { .defaultMetaSchemaIri(blueprint.defaultMetaSchemaIri) .jsonMapper(blueprint.jsonMapper) .yamlMapper(blueprint.yamlMapper) - .jsonNodeFactoryFactory(blueprint.jsonNodeFactoryFactory); + .objectReader(blueprint.objectReader); if (blueprint.schemaLoadersBuilder != null) { builder.schemaLoadersBuilder = SchemaLoaders.builder().with(blueprint.schemaLoadersBuilder); } @@ -442,18 +455,18 @@ protected JsonMetaSchema loadMetaSchema(String iri, SchemaValidatorsConfig confi } JsonNode readTree(String content, InputFormat inputFormat) throws IOException { - if (this.jsonNodeFactoryFactory == null) { + if (this.objectReader == null) { return getObjectMapper(inputFormat).readTree(content); } else { - return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory); + return this.objectReader.readTree(content, inputFormat); } } JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException { - if (this.jsonNodeFactoryFactory == null) { + if (this.objectReader == null) { return getObjectMapper(inputFormat).readTree(content); } else { - return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory); + return this.objectReader.readTree(content, inputFormat); } } @@ -627,10 +640,6 @@ ObjectMapper getJsonMapper() { return this.jsonMapper != null ? this.jsonMapper : JsonMapperFactory.getInstance(); } - JsonNodeFactoryFactory getJsonNodeFactoryFactory() { - return this.jsonNodeFactoryFactory; - } - /** * Creates a schema validators config. * diff --git a/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java b/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java new file mode 100644 index 000000000..3cc6bce84 --- /dev/null +++ b/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java @@ -0,0 +1,173 @@ +package com.networknt.schema.serialization; + +import java.io.IOException; +import java.io.InputStream; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.networknt.schema.InputFormat; +import com.networknt.schema.serialization.node.JsonNodeFactoryFactory; +import com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory; +import com.networknt.schema.utils.JsonNodes; + +/** + * Default {@link ObjectReader}. + */ +public class DefaultObjectReader implements ObjectReader { + protected final ObjectMapper jsonMapper; + protected final ObjectMapper yamlMapper; + protected final JsonNodeFactoryFactory jsonNodeFactoryFactory; + + /** + * Constructor. + * + * @param jsonMapper the json mapper + * @param yamlMapper the yaml mapper + * @param jsonNodeFactoryFactory the json node factory factory + */ + protected DefaultObjectReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper, + JsonNodeFactoryFactory jsonNodeFactoryFactory) { + this.jsonMapper = jsonMapper; + this.yamlMapper = yamlMapper; + this.jsonNodeFactoryFactory = jsonNodeFactoryFactory; + } + + @Override + public JsonNode readTree(String content, InputFormat inputFormat) throws IOException { + if (this.jsonNodeFactoryFactory == null) { + return getObjectMapper(inputFormat).readTree(content); + } else { + return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory); + } + } + + @Override + public JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException { + if (this.jsonNodeFactoryFactory == null) { + return getObjectMapper(inputFormat).readTree(content); + } else { + return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory); + } + } + + /** + * Gets the yaml mapper. + * + * @return the yaml mapper + */ + protected ObjectMapper getYamlMapper() { + return this.yamlMapper != null ? this.yamlMapper : YamlMapperFactory.getInstance(); + } + + /** + * Gets the json mapper. + * + * @return the json mapper + */ + protected ObjectMapper getJsonMapper() { + return this.jsonMapper != null ? this.jsonMapper : JsonMapperFactory.getInstance(); + } + + /** + * Gets the object mapper for the input format. + * + * @param inputFormat the input format + * @return the object mapper + */ + protected ObjectMapper getObjectMapper(InputFormat inputFormat) { + if (InputFormat.JSON.equals(inputFormat)) { + return getJsonMapper(); + } else if (InputFormat.YAML.equals(inputFormat)) { + return getYamlMapper(); + } + throw new IllegalArgumentException("Unsupported input format "+inputFormat); + } + + /** + * Gets the builder for {@link DefaultObjectReader}. + * + * @return the builder + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Builder support for {@link ObjectReader}. + * + * @param the super type + */ + public static abstract class BuilderSupport { + protected ObjectMapper jsonMapper = null; + protected ObjectMapper yamlMapper = null; + protected JsonNodeFactoryFactory jsonNodeFactoryFactory = null; + + protected abstract T self(); + + /** + * Sets the json mapper. + * + * @param jsonMapper the json mapper + * @return the builder + */ + public T jsonMapper(ObjectMapper jsonMapper) { + this.jsonMapper = jsonMapper; + return self(); + } + + /** + * Sets the yaml mapper + * + * @param yamlMapper the yaml mapper + * @return the builder + */ + public T yamlMapper(ObjectMapper yamlMapper) { + this.yamlMapper = yamlMapper; + return self(); + } + + /** + * Configures the {@link JsonNodeFactoryFactory} to use. + *

+ * To get location information from {@link JsonNode} the + * {@link com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory} + * can be used. + * + * @param jsonNodeFactoryFactory the factory to create json node factories + * @return the builder + */ + public T jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) { + this.jsonNodeFactoryFactory = jsonNodeFactoryFactory; + return self(); + } + } + + /** + * Builder for {@link DefaultObjectReader}. + */ + public static class Builder extends BuilderSupport { + + @Override + protected Builder self() { + return this; + } + + /** + * Makes the nodes generated location aware. + * + * @return the builder + */ + public Builder locationAware() { + return jsonNodeFactoryFactory(LocationJsonNodeFactoryFactory.getInstance()); + } + + /** + * Builds the {@link ObjectReader. + * + * @return the object reader + */ + public ObjectReader build() { + return new DefaultObjectReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory); + } + } +} diff --git a/src/main/java/com/networknt/schema/serialization/ObjectReader.java b/src/main/java/com/networknt/schema/serialization/ObjectReader.java new file mode 100644 index 000000000..d74cc2420 --- /dev/null +++ b/src/main/java/com/networknt/schema/serialization/ObjectReader.java @@ -0,0 +1,57 @@ +/* + * 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.serialization; + +import java.io.IOException; +import java.io.InputStream; + +import com.fasterxml.jackson.databind.JsonNode; +import com.networknt.schema.InputFormat; + +/** + * Object reader. + */ +public interface ObjectReader { + + /** + * Deserialize content as a tree. + * + * @param content the content + * @param inputFormat the input format + * @return the node + * @throws IOException + */ + JsonNode readTree(String content, InputFormat inputFormat) throws IOException; + + /** + * Deserialize content as a tree. + * + * @param content + * @param inputFormat + * @return the node + * @throws IOException + */ + JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException; + + /** + * Creates a builder for {@link ObjectReader}. + * + * @return the builder + */ + public static DefaultObjectReader.Builder builder() { + return DefaultObjectReader.builder(); + } +} diff --git a/src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java b/src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java new file mode 100644 index 000000000..d536a8cf4 --- /dev/null +++ b/src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java @@ -0,0 +1,104 @@ +/* + * 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.serialization; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.core.JsonLocation; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonNode; +import com.networknt.schema.InputFormat; +import com.networknt.schema.utils.JsonNodes; + +/** + * Test for Default Object Reader. + */ +class DefaultObjectReaderTest { + @Test + void location() throws JsonParseException, IOException { + String schemaData = "{\r\n" + + " \"$id\": \"https://schema/myschema\",\r\n" + + " \"properties\": {\r\n" + + " \"startDate\": {\r\n" + + " \"format\": \"date\",\r\n" + + " \"minLength\": 6\r\n" + + " }\r\n" + + " }\r\n" + + "}"; + JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON); + JsonNode idNode = jsonNode.at("/$id"); + JsonLocation location = JsonNodes.tokenLocationOf(idNode); + assertEquals(2, location.getLineNr()); + assertEquals(10, location.getColumnNr()); + + JsonNode formatNode = jsonNode.at("/properties/startDate/format"); + location = JsonNodes.tokenLocationOf(formatNode); + assertEquals(5, location.getLineNr()); + assertEquals(17, location.getColumnNr()); + + JsonNode minLengthNode = jsonNode.at("/properties/startDate/minLength"); + location = JsonNodes.tokenLocationOf(minLengthNode); + assertEquals(6, location.getLineNr()); + assertEquals(20, location.getColumnNr()); + } + + @Test + void jsonLocation() throws IOException { + String schemaData = "{\r\n" + + " \"$id\": \"https://schema/myschema\",\r\n" + + " \"properties\": {\r\n" + + " \"startDate\": {\r\n" + + " \"format\": \"date\",\r\n" + + " \"minLength\": 6\r\n" + + " }\r\n" + + " }\r\n" + + "}"; + JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON); + + JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format")); + JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength")); + + assertEquals(5, formatSchemaNodeTokenLocation.getLineNr()); + assertEquals(17, formatSchemaNodeTokenLocation.getColumnNr()); + + assertEquals(6, minLengthSchemaNodeTokenLocation.getLineNr()); + assertEquals(20, minLengthSchemaNodeTokenLocation.getColumnNr()); + } + + @Test + void yamlLocation() throws IOException { + String schemaData = "---\r\n" + + "\"$id\": 'https://schema/myschema'\r\n" + + "properties:\r\n" + + " startDate:\r\n" + + " format: 'date'\r\n" + + " minLength: 6\r\n" + + ""; + JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML); + + JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format")); + JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength")); + + assertEquals(5, formatSchemaNodeTokenLocation.getLineNr()); + assertEquals(13, formatSchemaNodeTokenLocation.getColumnNr()); + + assertEquals(6, minLengthSchemaNodeTokenLocation.getLineNr()); + assertEquals(16, minLengthSchemaNodeTokenLocation.getColumnNr()); + } +} diff --git a/src/test/java/com/networknt/schema/utils/JsonNodesTest.java b/src/test/java/com/networknt/schema/utils/JsonNodesTest.java index 71a2abb0c..42173446c 100644 --- a/src/test/java/com/networknt/schema/utils/JsonNodesTest.java +++ b/src/test/java/com/networknt/schema/utils/JsonNodesTest.java @@ -40,6 +40,7 @@ import com.networknt.schema.SpecVersion.VersionFlag; import com.networknt.schema.ValidationMessage; import com.networknt.schema.serialization.JsonMapperFactory; +import com.networknt.schema.serialization.ObjectReader; import com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory; /** * Tests for JsonNodes. @@ -73,7 +74,7 @@ void location() throws JsonParseException, IOException { assertEquals(6, location.getLineNr()); assertEquals(20, location.getColumnNr()); } - + @Test void jsonLocation() { String schemaData = "{\r\n" @@ -89,7 +90,7 @@ void jsonLocation() { + " \"startDate\": \"1\"\r\n" + "}"; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, - builder -> builder.jsonNodeFactoryFactory(LocationJsonNodeFactoryFactory.getInstance())); + builder -> builder.objectReader(ObjectReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config); @@ -138,7 +139,7 @@ void yamlLocation() { + "startDate: '1'\r\n" + ""; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, - builder -> builder.jsonNodeFactoryFactory(LocationJsonNodeFactoryFactory.getInstance())); + builder -> builder.objectReader(ObjectReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.YAML, config); From 4b1e449dd834421d467a50e197009c4b4a3c732a Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Fri, 14 Jun 2024 09:40:53 +0800 Subject: [PATCH 2/5] Deprecate setting jsonMapper and yamlMapper on JsonSchemaFactory --- .../com/networknt/schema/JsonSchemaFactory.java | 8 ++++++++ .../schema/serialization/DefaultObjectReader.java | 2 +- .../schema/AbstractJsonSchemaTestSuite.java | 6 ------ .../java/com/networknt/schema/Issue285Test.java | 1 - .../com/networknt/schema/Issue366FailFastTest.java | 2 +- .../com/networknt/schema/Issue366FailSlowTest.java | 1 - .../java/com/networknt/schema/Issue428Test.java | 2 +- .../java/com/networknt/schema/Issue510Test.java | 13 ------------- .../java/com/networknt/schema/Issue928Test.java | 1 - .../networknt/schema/OpenAPI30JsonSchemaTest.java | 2 +- .../com/networknt/schema/UnknownMetaSchemaTest.java | 6 +++--- .../java/com/networknt/schema/V4JsonSchemaTest.java | 1 - 12 files changed, 15 insertions(+), 30 deletions(-) delete mode 100644 src/test/java/com/networknt/schema/Issue510Test.java diff --git a/src/main/java/com/networknt/schema/JsonSchemaFactory.java b/src/main/java/com/networknt/schema/JsonSchemaFactory.java index b70ed180c..9a29435a9 100644 --- a/src/main/java/com/networknt/schema/JsonSchemaFactory.java +++ b/src/main/java/com/networknt/schema/JsonSchemaFactory.java @@ -67,6 +67,8 @@ public static class Builder { * Sets the object reader to read the data. *

* If set this takes precedence over the configured json mapper and yaml mapper. + *

+ * A location aware object reader can be created using ObjectReader.builder().locationAware().build(). * * @param objectReader the object reader * @return the builder @@ -80,10 +82,13 @@ public Builder objectReader(ObjectReader objectReader) { * Sets the json mapper to read the data. *

* If the object reader is set this will not be used. + *

+ * This is deprecated use a object reader instead. * * @param jsonMapper the json mapper * @return the builder */ + @Deprecated public Builder jsonMapper(final ObjectMapper jsonMapper) { this.jsonMapper = jsonMapper; return this; @@ -93,10 +98,13 @@ public Builder jsonMapper(final ObjectMapper jsonMapper) { * Sets the yaml mapper to read the data. *

* If the object reader is set this will not be used. + *

+ * This is deprecated use a object reader instead. * * @param yamlMapper the yaml mapper * @return the builder */ + @Deprecated public Builder yamlMapper(final ObjectMapper yamlMapper) { this.yamlMapper = yamlMapper; return this; diff --git a/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java b/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java index 3cc6bce84..95ea5ef65 100644 --- a/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java +++ b/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java @@ -162,7 +162,7 @@ public Builder locationAware() { } /** - * Builds the {@link ObjectReader. + * Builds the {@link ObjectReader}. * * @return the object reader */ diff --git a/src/test/java/com/networknt/schema/AbstractJsonSchemaTestSuite.java b/src/test/java/com/networknt/schema/AbstractJsonSchemaTestSuite.java index 4e9e8f1ec..cc066e93b 100644 --- a/src/test/java/com/networknt/schema/AbstractJsonSchemaTestSuite.java +++ b/src/test/java/com/networknt/schema/AbstractJsonSchemaTestSuite.java @@ -16,11 +16,9 @@ package com.networknt.schema; -import com.fasterxml.jackson.databind.ObjectMapper; import com.networknt.schema.SpecVersion.VersionFlag; import com.networknt.schema.regex.JDKRegularExpressionFactory; import com.networknt.schema.regex.JoniRegularExpressionFactory; -import com.networknt.schema.serialization.JsonMapperFactory; import com.networknt.schema.suite.TestCase; import com.networknt.schema.suite.TestSource; import com.networknt.schema.suite.TestSpec; @@ -48,9 +46,6 @@ public abstract class AbstractJsonSchemaTestSuite extends HTTPServiceSupport { - - protected ObjectMapper mapper = JsonMapperFactory.getInstance(); - private static String toForwardSlashPath(Path file) { return file.toString().replace('\\', '/'); } @@ -190,7 +185,6 @@ private JsonSchemaFactory buildValidatorFactory(VersionFlag defaultVersion, Test JsonSchemaFactory base = JsonSchemaFactory.getInstance(specVersion); return JsonSchemaFactory .builder(base) - .jsonMapper(this.mapper) .schemaMappers(schemaMappers -> schemaMappers .mapPrefix("https://", "http://") .mapPrefix("http://json-schema.org", "resource:")) diff --git a/src/test/java/com/networknt/schema/Issue285Test.java b/src/test/java/com/networknt/schema/Issue285Test.java index ce77eeeee..a5d98a2bf 100644 --- a/src/test/java/com/networknt/schema/Issue285Test.java +++ b/src/test/java/com/networknt/schema/Issue285Test.java @@ -15,7 +15,6 @@ public class Issue285Test { private ObjectMapper mapper = new ObjectMapper(); private JsonSchemaFactory schemaFactory = JsonSchemaFactory .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)) - .jsonMapper(mapper) .schemaMappers(schemaMappers -> schemaMappers .mapPrefix("http://json-schema.org", "resource:") .mapPrefix("https://json-schema.org", "resource:")) diff --git a/src/test/java/com/networknt/schema/Issue366FailFastTest.java b/src/test/java/com/networknt/schema/Issue366FailFastTest.java index 55c94abbf..a863ecff9 100644 --- a/src/test/java/com/networknt/schema/Issue366FailFastTest.java +++ b/src/test/java/com/networknt/schema/Issue366FailFastTest.java @@ -27,7 +27,7 @@ private void setupSchema() throws IOException { SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig(); schemaValidatorsConfig.setFailFast(true); JsonSchemaFactory schemaFactory = JsonSchemaFactory - .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).jsonMapper(objectMapper).build(); + .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).build(); schemaValidatorsConfig.setTypeLoose(false); diff --git a/src/test/java/com/networknt/schema/Issue366FailSlowTest.java b/src/test/java/com/networknt/schema/Issue366FailSlowTest.java index 7802d4f38..38f25a7cb 100644 --- a/src/test/java/com/networknt/schema/Issue366FailSlowTest.java +++ b/src/test/java/com/networknt/schema/Issue366FailSlowTest.java @@ -26,7 +26,6 @@ private void setupSchema() throws IOException { SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig(); JsonSchemaFactory schemaFactory = JsonSchemaFactory .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)) - .jsonMapper(objectMapper) .build(); schemaValidatorsConfig.setTypeLoose(false); diff --git a/src/test/java/com/networknt/schema/Issue428Test.java b/src/test/java/com/networknt/schema/Issue428Test.java index c2d33188e..5785cec79 100644 --- a/src/test/java/com/networknt/schema/Issue428Test.java +++ b/src/test/java/com/networknt/schema/Issue428Test.java @@ -15,7 +15,7 @@ public class Issue428Test extends HTTPServiceSupport { protected ObjectMapper mapper = new ObjectMapper(); protected JsonSchemaFactory validatorFactory = JsonSchemaFactory - .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).jsonMapper(mapper).build(); + .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).build(); private void runTestFile(String testCaseFile) throws Exception { final SchemaLocation testCaseFileUri = SchemaLocation.of("classpath:" + testCaseFile); diff --git a/src/test/java/com/networknt/schema/Issue510Test.java b/src/test/java/com/networknt/schema/Issue510Test.java deleted file mode 100644 index 2e2da1c17..000000000 --- a/src/test/java/com/networknt/schema/Issue510Test.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.networknt.schema; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.jupiter.api.Test; - -public class Issue510Test { - @Test - public void testIssue510() { - ObjectMapper objectMapper = new ObjectMapper(); - JsonSchemaFactory schemaFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).jsonMapper(objectMapper).build(); - System.out.println("schemaFactory = " + schemaFactory); - } -} diff --git a/src/test/java/com/networknt/schema/Issue928Test.java b/src/test/java/com/networknt/schema/Issue928Test.java index df933da48..96739aed0 100644 --- a/src/test/java/com/networknt/schema/Issue928Test.java +++ b/src/test/java/com/networknt/schema/Issue928Test.java @@ -10,7 +10,6 @@ public class Issue928Test { private JsonSchemaFactory factoryFor(SpecVersion.VersionFlag version) { return JsonSchemaFactory .builder(JsonSchemaFactory.getInstance(version)) - .jsonMapper(mapper) .schemaMappers(schemaMappers -> schemaMappers.mapPrefix("https://example.org", "classpath:")) .build(); } diff --git a/src/test/java/com/networknt/schema/OpenAPI30JsonSchemaTest.java b/src/test/java/com/networknt/schema/OpenAPI30JsonSchemaTest.java index 157efc336..136f637f0 100644 --- a/src/test/java/com/networknt/schema/OpenAPI30JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/OpenAPI30JsonSchemaTest.java @@ -15,7 +15,7 @@ public class OpenAPI30JsonSchemaTest extends HTTPServiceSupport { protected ObjectMapper mapper = new ObjectMapper(); protected JsonSchemaFactory validatorFactory = JsonSchemaFactory - .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).jsonMapper(mapper).build(); + .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).build(); public OpenAPI30JsonSchemaTest() { } diff --git a/src/test/java/com/networknt/schema/UnknownMetaSchemaTest.java b/src/test/java/com/networknt/schema/UnknownMetaSchemaTest.java index 06c35e4bb..ed4603172 100644 --- a/src/test/java/com/networknt/schema/UnknownMetaSchemaTest.java +++ b/src/test/java/com/networknt/schema/UnknownMetaSchemaTest.java @@ -21,7 +21,7 @@ public void testSchema1() throws IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(this.json); - JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).jsonMapper(mapper).build(); + JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).build(); JsonSchema jsonSchema = factory.getSchema(schema1); Set errors = jsonSchema.validate(jsonNode); @@ -35,7 +35,7 @@ public void testSchema2() throws IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(this.json); - JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).jsonMapper(mapper).build(); + JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).build(); JsonSchema jsonSchema = factory.getSchema(schema2); Set errors = jsonSchema.validate(jsonNode); @@ -48,7 +48,7 @@ public void testSchema3() throws IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(this.json); - JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).jsonMapper(mapper).build(); + JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).build(); JsonSchema jsonSchema = factory.getSchema(schema3); Set errors = jsonSchema.validate(jsonNode); diff --git a/src/test/java/com/networknt/schema/V4JsonSchemaTest.java b/src/test/java/com/networknt/schema/V4JsonSchemaTest.java index 1fc010276..9a71a7a6b 100644 --- a/src/test/java/com/networknt/schema/V4JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V4JsonSchemaTest.java @@ -89,7 +89,6 @@ private Set validateFailingFastSchemaFor(final String schemaF config.setFailFast(true); return JsonSchemaFactory .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)) - .jsonMapper(objectMapper) .build() .getSchema(schema, config) .validate(dataFile); From 82b67b5488efaede6a16b0115cf5efc270355c39 Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:03:33 +0800 Subject: [PATCH 3/5] Fix javadoc --- src/main/java/com/networknt/schema/JsonSchema.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java index f1fbdbe1d..1c3182f47 100644 --- a/src/main/java/com/networknt/schema/JsonSchema.java +++ b/src/main/java/com/networknt/schema/JsonSchema.java @@ -1021,6 +1021,7 @@ public ValidationResult walk(ExecutionContext executionContext, JsonNode node, b /** * Walk the JSON node. * + * @param the result type * @param executionContext the execution context * @param node the input * @param outputFormat the output format @@ -1053,6 +1054,7 @@ public ValidationResult walk(ExecutionContext executionContext, JsonNode node, b /** * Walk the JSON node. * + * @param the result type * @param executionContext the execution context * @param node the input * @param outputFormat the output format @@ -1099,6 +1101,7 @@ public ValidationResult walk(ExecutionContext executionContext, String input, In /** * Walk the input. * + * @param the result type * @param executionContext the execution context * @param input the input * @param inputFormat the input format @@ -1132,6 +1135,7 @@ public ValidationResult walk(ExecutionContext executionContext, String input, In /** * Walk the input. * + * @param the result type * @param executionContext the execution context * @param input the input * @param inputFormat the input format @@ -1160,6 +1164,7 @@ public ValidationResult walk(JsonNode node, boolean validate) { /** * Walk the JSON node. * + * @param the result type * @param node the input * @param validate true to validate the input against the schema * @param outputFormat the output format From 17184cebed558f7ddf223b2d644fe342b414a0fe Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:10:42 +0800 Subject: [PATCH 4/5] Fix javadoc --- .../schema/DisallowUnknownJsonMetaSchemaFactory.java | 5 +++++ .../com/networknt/schema/DisallowUnknownKeywordFactory.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/com/networknt/schema/DisallowUnknownJsonMetaSchemaFactory.java b/src/main/java/com/networknt/schema/DisallowUnknownJsonMetaSchemaFactory.java index 938f4246a..6ca259bef 100644 --- a/src/main/java/com/networknt/schema/DisallowUnknownJsonMetaSchemaFactory.java +++ b/src/main/java/com/networknt/schema/DisallowUnknownJsonMetaSchemaFactory.java @@ -31,6 +31,11 @@ private static class Holder { private static DisallowUnknownJsonMetaSchemaFactory INSTANCE = new DisallowUnknownJsonMetaSchemaFactory(); } + /** + * Gets the instance of {@link DisallowUnknownJsonMetaSchemaFactory}. + * + * @return the json meta schema factory + */ public static DisallowUnknownJsonMetaSchemaFactory getInstance() { return Holder.INSTANCE; } diff --git a/src/main/java/com/networknt/schema/DisallowUnknownKeywordFactory.java b/src/main/java/com/networknt/schema/DisallowUnknownKeywordFactory.java index 3a665055a..e0f3f7e30 100644 --- a/src/main/java/com/networknt/schema/DisallowUnknownKeywordFactory.java +++ b/src/main/java/com/networknt/schema/DisallowUnknownKeywordFactory.java @@ -36,6 +36,11 @@ private static class Holder { private static DisallowUnknownKeywordFactory INSTANCE = new DisallowUnknownKeywordFactory(); } + /** + * Gets the instance of {@link DisallowUnknownKeywordFactory}. + * + * @return the keyword factory + */ public static DisallowUnknownKeywordFactory getInstance() { return Holder.INSTANCE; } From 8953fe8129185af84461bc245d3335b2c31c82b9 Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:59:33 +0800 Subject: [PATCH 5/5] Rename to json node reader --- README.md | 8 ++--- .../networknt/schema/JsonSchemaFactory.java | 32 +++++++++---------- ...Reader.java => DefaultJsonNodeReader.java} | 18 +++++------ ...{ObjectReader.java => JsonNodeReader.java} | 10 +++--- ...st.java => DefaultJsonNodeReaderTest.java} | 8 ++--- .../networknt/schema/utils/JsonNodesTest.java | 6 ++-- 6 files changed, 41 insertions(+), 41 deletions(-) rename src/main/java/com/networknt/schema/serialization/{DefaultObjectReader.java => DefaultJsonNodeReader.java} (87%) rename src/main/java/com/networknt/schema/serialization/{ObjectReader.java => JsonNodeReader.java} (82%) rename src/test/java/com/networknt/schema/serialization/{DefaultObjectReaderTest.java => DefaultJsonNodeReaderTest.java} (89%) diff --git a/README.md b/README.md index ea9e3140c..7ac24f86e 100644 --- a/README.md +++ b/README.md @@ -353,8 +353,8 @@ Assertions contains the following additional information | Arguments | The arguments used for generating the message. | Type | The keyword that generated the message. | Property | The property name that caused the validation error for example for the `required` keyword. Note that this is not part of the instance location as that points to the instance node. -| Schema Node | The `JsonNode` pointed to by the Schema Location. This is the schema data that caused the input data to fail. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `ObjectReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(schemaNode)`. -| Instance Node | The `JsonNode` pointed to by the Instance Location. This is the input data that failed validation. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `ObjectReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(instanceNode)`. +| Schema Node | The `JsonNode` pointed to by the Schema Location. This is the schema data that caused the input data to fail. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `JsonNodeReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(schemaNode)`. +| Instance Node | The `JsonNode` pointed to by the Instance Location. This is the input data that failed validation. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `JsonNodeReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(instanceNode)`. | Details | Additional details that can be set by custom keyword validator implementations. This is not used by the library. Annotations contains the following additional information @@ -367,7 +367,7 @@ Annotations contains the following additional information The library can be configured to store line and column information in the `JsonNode` instances for the instance and schema nodes. This will adversely affect performance and is not configured by default. -This is done by configuring a `ObjectReader` that uses the `LocationJsonNodeFactoryFactory`on the `JsonSchemaFactory`. The `JsonLocation` information can then be retrieved using `JsonNodes.tokenLocationOf(jsonNode)`. +This is done by configuring a `JsonNodeReader` that uses the `LocationJsonNodeFactoryFactory`on the `JsonSchemaFactory`. The `JsonLocation` information can then be retrieved using `JsonNodes.tokenLocationOf(jsonNode)`. ```java String schemaData = "{\r\n" @@ -383,7 +383,7 @@ String inputData = "{\r\n" + " \"startDate\": \"1\"\r\n" + "}"; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, - builder -> builder.objectReader(ObjectReader.builder().locationAware().build())); + builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config); diff --git a/src/main/java/com/networknt/schema/JsonSchemaFactory.java b/src/main/java/com/networknt/schema/JsonSchemaFactory.java index 9a29435a9..89ddffd79 100644 --- a/src/main/java/com/networknt/schema/JsonSchemaFactory.java +++ b/src/main/java/com/networknt/schema/JsonSchemaFactory.java @@ -25,7 +25,7 @@ import com.networknt.schema.resource.SchemaMapper; import com.networknt.schema.resource.SchemaMappers; import com.networknt.schema.serialization.JsonMapperFactory; -import com.networknt.schema.serialization.ObjectReader; +import com.networknt.schema.serialization.JsonNodeReader; import com.networknt.schema.serialization.YamlMapperFactory; import org.slf4j.Logger; @@ -55,7 +55,7 @@ public class JsonSchemaFactory { public static class Builder { private ObjectMapper jsonMapper = null; private ObjectMapper yamlMapper = null; - private ObjectReader objectReader = null; + private JsonNodeReader jsonNodeReader = null; private String defaultMetaSchemaIri; private final ConcurrentMap metaSchemas = new ConcurrentHashMap(); private SchemaLoaders.Builder schemaLoadersBuilder = null; @@ -64,17 +64,17 @@ public static class Builder { private JsonMetaSchemaFactory metaSchemaFactory = null; /** - * Sets the object reader to read the data. + * Sets the json node reader to read the data. *

* If set this takes precedence over the configured json mapper and yaml mapper. *

- * A location aware object reader can be created using ObjectReader.builder().locationAware().build(). + * A location aware object reader can be created using JsonNodeReader.builder().locationAware().build(). * - * @param objectReader the object reader + * @param jsonNodeReader the object reader * @return the builder */ - public Builder objectReader(ObjectReader objectReader) { - this.objectReader = objectReader; + public Builder jsonNodeReader(JsonNodeReader jsonNodeReader) { + this.jsonNodeReader = jsonNodeReader; return this; } @@ -172,7 +172,7 @@ public JsonSchemaFactory build() { return new JsonSchemaFactory( jsonMapper, yamlMapper, - objectReader, + jsonNodeReader, defaultMetaSchemaIri, schemaLoadersBuilder, schemaMappersBuilder, @@ -185,7 +185,7 @@ public JsonSchemaFactory build() { private final ObjectMapper jsonMapper; private final ObjectMapper yamlMapper; - private final ObjectReader objectReader; + private final JsonNodeReader jsonNodeReader; private final String defaultMetaSchemaIri; private final SchemaLoaders.Builder schemaLoadersBuilder; private final SchemaMappers.Builder schemaMappersBuilder; @@ -201,7 +201,7 @@ public JsonSchemaFactory build() { private JsonSchemaFactory( ObjectMapper jsonMapper, ObjectMapper yamlMapper, - ObjectReader objectReader, + JsonNodeReader jsonNodeReader, String defaultMetaSchemaIri, SchemaLoaders.Builder schemaLoadersBuilder, SchemaMappers.Builder schemaMappersBuilder, @@ -218,7 +218,7 @@ private JsonSchemaFactory( } this.jsonMapper = jsonMapper; this.yamlMapper = yamlMapper; - this.objectReader = objectReader; + this.jsonNodeReader = jsonNodeReader; this.defaultMetaSchemaIri = defaultMetaSchemaIri; this.schemaLoadersBuilder = schemaLoadersBuilder; this.schemaMappersBuilder = schemaMappersBuilder; @@ -311,7 +311,7 @@ public static Builder builder(final JsonSchemaFactory blueprint) { .defaultMetaSchemaIri(blueprint.defaultMetaSchemaIri) .jsonMapper(blueprint.jsonMapper) .yamlMapper(blueprint.yamlMapper) - .objectReader(blueprint.objectReader); + .jsonNodeReader(blueprint.jsonNodeReader); if (blueprint.schemaLoadersBuilder != null) { builder.schemaLoadersBuilder = SchemaLoaders.builder().with(blueprint.schemaLoadersBuilder); } @@ -463,18 +463,18 @@ protected JsonMetaSchema loadMetaSchema(String iri, SchemaValidatorsConfig confi } JsonNode readTree(String content, InputFormat inputFormat) throws IOException { - if (this.objectReader == null) { + if (this.jsonNodeReader == null) { return getObjectMapper(inputFormat).readTree(content); } else { - return this.objectReader.readTree(content, inputFormat); + return this.jsonNodeReader.readTree(content, inputFormat); } } JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException { - if (this.objectReader == null) { + if (this.jsonNodeReader == null) { return getObjectMapper(inputFormat).readTree(content); } else { - return this.objectReader.readTree(content, inputFormat); + return this.jsonNodeReader.readTree(content, inputFormat); } } diff --git a/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java b/src/main/java/com/networknt/schema/serialization/DefaultJsonNodeReader.java similarity index 87% rename from src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java rename to src/main/java/com/networknt/schema/serialization/DefaultJsonNodeReader.java index 95ea5ef65..10707edca 100644 --- a/src/main/java/com/networknt/schema/serialization/DefaultObjectReader.java +++ b/src/main/java/com/networknt/schema/serialization/DefaultJsonNodeReader.java @@ -11,9 +11,9 @@ import com.networknt.schema.utils.JsonNodes; /** - * Default {@link ObjectReader}. + * Default {@link JsonNodeReader}. */ -public class DefaultObjectReader implements ObjectReader { +public class DefaultJsonNodeReader implements JsonNodeReader { protected final ObjectMapper jsonMapper; protected final ObjectMapper yamlMapper; protected final JsonNodeFactoryFactory jsonNodeFactoryFactory; @@ -25,7 +25,7 @@ public class DefaultObjectReader implements ObjectReader { * @param yamlMapper the yaml mapper * @param jsonNodeFactoryFactory the json node factory factory */ - protected DefaultObjectReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper, + protected DefaultJsonNodeReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper, JsonNodeFactoryFactory jsonNodeFactoryFactory) { this.jsonMapper = jsonMapper; this.yamlMapper = yamlMapper; @@ -84,7 +84,7 @@ protected ObjectMapper getObjectMapper(InputFormat inputFormat) { } /** - * Gets the builder for {@link DefaultObjectReader}. + * Gets the builder for {@link DefaultJsonNodeReader}. * * @return the builder */ @@ -93,7 +93,7 @@ public static Builder builder() { } /** - * Builder support for {@link ObjectReader}. + * Builder support for {@link JsonNodeReader}. * * @param the super type */ @@ -143,7 +143,7 @@ public T jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) { } /** - * Builder for {@link DefaultObjectReader}. + * Builder for {@link DefaultJsonNodeReader}. */ public static class Builder extends BuilderSupport { @@ -162,12 +162,12 @@ public Builder locationAware() { } /** - * Builds the {@link ObjectReader}. + * Builds the {@link JsonNodeReader}. * * @return the object reader */ - public ObjectReader build() { - return new DefaultObjectReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory); + public JsonNodeReader build() { + return new DefaultJsonNodeReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory); } } } diff --git a/src/main/java/com/networknt/schema/serialization/ObjectReader.java b/src/main/java/com/networknt/schema/serialization/JsonNodeReader.java similarity index 82% rename from src/main/java/com/networknt/schema/serialization/ObjectReader.java rename to src/main/java/com/networknt/schema/serialization/JsonNodeReader.java index d74cc2420..25e60775a 100644 --- a/src/main/java/com/networknt/schema/serialization/ObjectReader.java +++ b/src/main/java/com/networknt/schema/serialization/JsonNodeReader.java @@ -22,9 +22,9 @@ import com.networknt.schema.InputFormat; /** - * Object reader. + * Reader for reading content to {@link JsonNode}. */ -public interface ObjectReader { +public interface JsonNodeReader { /** * Deserialize content as a tree. @@ -47,11 +47,11 @@ public interface ObjectReader { JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException; /** - * Creates a builder for {@link ObjectReader}. + * Creates a builder for {@link JsonNodeReader}. * * @return the builder */ - public static DefaultObjectReader.Builder builder() { - return DefaultObjectReader.builder(); + public static DefaultJsonNodeReader.Builder builder() { + return DefaultJsonNodeReader.builder(); } } diff --git a/src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java b/src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.java similarity index 89% rename from src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java rename to src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.java index d536a8cf4..addf992b7 100644 --- a/src/test/java/com/networknt/schema/serialization/DefaultObjectReaderTest.java +++ b/src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.java @@ -29,7 +29,7 @@ /** * Test for Default Object Reader. */ -class DefaultObjectReaderTest { +class DefaultJsonNodeReaderTest { @Test void location() throws JsonParseException, IOException { String schemaData = "{\r\n" @@ -41,7 +41,7 @@ void location() throws JsonParseException, IOException { + " }\r\n" + " }\r\n" + "}"; - JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON); + JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON); JsonNode idNode = jsonNode.at("/$id"); JsonLocation location = JsonNodes.tokenLocationOf(idNode); assertEquals(2, location.getLineNr()); @@ -69,7 +69,7 @@ void jsonLocation() throws IOException { + " }\r\n" + " }\r\n" + "}"; - JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON); + JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON); JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format")); JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength")); @@ -90,7 +90,7 @@ void yamlLocation() throws IOException { + " format: 'date'\r\n" + " minLength: 6\r\n" + ""; - JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML); + JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML); JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format")); JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength")); diff --git a/src/test/java/com/networknt/schema/utils/JsonNodesTest.java b/src/test/java/com/networknt/schema/utils/JsonNodesTest.java index 42173446c..24c55c700 100644 --- a/src/test/java/com/networknt/schema/utils/JsonNodesTest.java +++ b/src/test/java/com/networknt/schema/utils/JsonNodesTest.java @@ -40,7 +40,7 @@ import com.networknt.schema.SpecVersion.VersionFlag; import com.networknt.schema.ValidationMessage; import com.networknt.schema.serialization.JsonMapperFactory; -import com.networknt.schema.serialization.ObjectReader; +import com.networknt.schema.serialization.JsonNodeReader; import com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory; /** * Tests for JsonNodes. @@ -90,7 +90,7 @@ void jsonLocation() { + " \"startDate\": \"1\"\r\n" + "}"; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, - builder -> builder.objectReader(ObjectReader.builder().locationAware().build())); + builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config); @@ -139,7 +139,7 @@ void yamlLocation() { + "startDate: '1'\r\n" + ""; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, - builder -> builder.objectReader(ObjectReader.builder().locationAware().build())); + builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.YAML, config);