diff --git a/README.md b/README.md index cfb0f92df..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 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 `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 `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.jsonNodeFactoryFactory(LocationJsonNodeFactoryFactory.getInstance())); + 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/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; } 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 diff --git a/src/main/java/com/networknt/schema/JsonSchemaFactory.java b/src/main/java/com/networknt/schema/JsonSchemaFactory.java index ce8363dad..89ddffd79 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.JsonNodeReader; 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 JsonNodeReader jsonNodeReader = null; private String defaultMetaSchemaIri; private final ConcurrentMap metaSchemas = new ConcurrentHashMap(); private SchemaLoaders.Builder schemaLoadersBuilder = null; @@ -65,25 +64,47 @@ public static class Builder { private JsonMetaSchemaFactory metaSchemaFactory = null; /** - * Configures the {@link JsonNodeFactoryFactory} to use. + * Sets the json node 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. + *

+ * A location aware object reader can be created using JsonNodeReader.builder().locationAware().build(). * - * @param jsonNodeFactoryFactory the factory to create json node factories + * @param jsonNodeReader the object reader * @return the builder */ - public Builder jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) { - this.jsonNodeFactoryFactory = jsonNodeFactoryFactory; + public Builder jsonNodeReader(JsonNodeReader jsonNodeReader) { + this.jsonNodeReader = jsonNodeReader; return this; } + /** + * 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; } + /** + * 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; @@ -151,7 +172,7 @@ public JsonSchemaFactory build() { return new JsonSchemaFactory( jsonMapper, yamlMapper, - jsonNodeFactoryFactory, + jsonNodeReader, defaultMetaSchemaIri, schemaLoadersBuilder, schemaMappersBuilder, @@ -164,7 +185,7 @@ public JsonSchemaFactory build() { private final ObjectMapper jsonMapper; private final ObjectMapper yamlMapper; - private final JsonNodeFactoryFactory jsonNodeFactoryFactory; + private final JsonNodeReader jsonNodeReader; private final String defaultMetaSchemaIri; private final SchemaLoaders.Builder schemaLoadersBuilder; private final SchemaMappers.Builder schemaMappersBuilder; @@ -180,7 +201,7 @@ public JsonSchemaFactory build() { private JsonSchemaFactory( ObjectMapper jsonMapper, ObjectMapper yamlMapper, - JsonNodeFactoryFactory jsonNodeFactoryFactory, + JsonNodeReader jsonNodeReader, String defaultMetaSchemaIri, SchemaLoaders.Builder schemaLoadersBuilder, SchemaMappers.Builder schemaMappersBuilder, @@ -197,7 +218,7 @@ private JsonSchemaFactory( } this.jsonMapper = jsonMapper; this.yamlMapper = yamlMapper; - this.jsonNodeFactoryFactory = jsonNodeFactoryFactory; + this.jsonNodeReader = jsonNodeReader; this.defaultMetaSchemaIri = defaultMetaSchemaIri; this.schemaLoadersBuilder = schemaLoadersBuilder; this.schemaMappersBuilder = schemaMappersBuilder; @@ -290,7 +311,7 @@ public static Builder builder(final JsonSchemaFactory blueprint) { .defaultMetaSchemaIri(blueprint.defaultMetaSchemaIri) .jsonMapper(blueprint.jsonMapper) .yamlMapper(blueprint.yamlMapper) - .jsonNodeFactoryFactory(blueprint.jsonNodeFactoryFactory); + .jsonNodeReader(blueprint.jsonNodeReader); if (blueprint.schemaLoadersBuilder != null) { builder.schemaLoadersBuilder = SchemaLoaders.builder().with(blueprint.schemaLoadersBuilder); } @@ -442,18 +463,18 @@ protected JsonMetaSchema loadMetaSchema(String iri, SchemaValidatorsConfig confi } JsonNode readTree(String content, InputFormat inputFormat) throws IOException { - if (this.jsonNodeFactoryFactory == null) { + if (this.jsonNodeReader == null) { return getObjectMapper(inputFormat).readTree(content); } else { - return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory); + return this.jsonNodeReader.readTree(content, inputFormat); } } JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException { - if (this.jsonNodeFactoryFactory == null) { + if (this.jsonNodeReader == null) { return getObjectMapper(inputFormat).readTree(content); } else { - return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory); + return this.jsonNodeReader.readTree(content, inputFormat); } } @@ -627,10 +648,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/DefaultJsonNodeReader.java b/src/main/java/com/networknt/schema/serialization/DefaultJsonNodeReader.java new file mode 100644 index 000000000..10707edca --- /dev/null +++ b/src/main/java/com/networknt/schema/serialization/DefaultJsonNodeReader.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 JsonNodeReader}. + */ +public class DefaultJsonNodeReader implements JsonNodeReader { + 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 DefaultJsonNodeReader(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 DefaultJsonNodeReader}. + * + * @return the builder + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Builder support for {@link JsonNodeReader}. + * + * @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 DefaultJsonNodeReader}. + */ + 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 JsonNodeReader}. + * + * @return the object reader + */ + public JsonNodeReader build() { + return new DefaultJsonNodeReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory); + } + } +} diff --git a/src/main/java/com/networknt/schema/serialization/JsonNodeReader.java b/src/main/java/com/networknt/schema/serialization/JsonNodeReader.java new file mode 100644 index 000000000..25e60775a --- /dev/null +++ b/src/main/java/com/networknt/schema/serialization/JsonNodeReader.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; + +/** + * Reader for reading content to {@link JsonNode}. + */ +public interface JsonNodeReader { + + /** + * 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 JsonNodeReader}. + * + * @return the builder + */ + public static DefaultJsonNodeReader.Builder builder() { + return DefaultJsonNodeReader.builder(); + } +} 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); diff --git a/src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.java b/src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.java new file mode 100644 index 000000000..addf992b7 --- /dev/null +++ b/src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.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 DefaultJsonNodeReaderTest { + @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 = JsonNodeReader.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 = 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")); + + 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 = 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")); + + 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..24c55c700 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.JsonNodeReader; 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.jsonNodeReader(JsonNodeReader.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.jsonNodeReader(JsonNodeReader.builder().locationAware().build())); SchemaValidatorsConfig config = new SchemaValidatorsConfig(); config.setPathType(PathType.JSON_POINTER); JsonSchema schema = factory.getSchema(schemaData, InputFormat.YAML, config);