Skip to content
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
6 changes: 1 addition & 5 deletions src/main/java/com/networknt/schema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,7 @@ public <T> T validate(ExecutionContext executionContext, JsonNode node, OutputFo
* @return the JsonNode.
*/
private JsonNode deserialize(String input, InputFormat inputFormat) {
try {
return this.getSchemaContext().getSchemaRegistry().readTree(input, inputFormat);
} catch (IOException e) {
throw new UncheckedIOException("Invalid input", e);
}
return this.getSchemaContext().getSchemaRegistry().readTree(input, inputFormat);
}

/**
Expand Down
40 changes: 10 additions & 30 deletions src/main/java/com/networknt/schema/SchemaRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,11 @@ public Dialect getDialect(String dialectId) {
return dialectRegistry.getDialect(key, this);
}

JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
JsonNode readTree(String content, InputFormat inputFormat) {
return this.nodeReader.readTree(content, inputFormat);
}

JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
JsonNode readTree(InputStream content, InputFormat inputFormat) {
return this.nodeReader.readTree(content, inputFormat);
}

Expand Down Expand Up @@ -616,13 +616,8 @@ public Schema getSchema(final String schema) {
* @return the schema
*/
public Schema getSchema(final String schema, InputFormat inputFormat) {
try {
final JsonNode schemaNode = readTree(schema, inputFormat);
return newSchema(null, schemaNode);
} catch (IOException ioe) {
logger.error("Failed to load json schema!", ioe);
throw new SchemaException(ioe);
}
final JsonNode schemaNode = readTree(schema, inputFormat);
return newSchema(null, schemaNode);
}
Comment thread
justin-tay marked this conversation as resolved.

/**
Expand All @@ -649,13 +644,8 @@ public Schema getSchema(final InputStream schemaStream) {
* @return the schema
*/
public Schema getSchema(final InputStream schemaStream, InputFormat inputFormat) {
try {
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
return newSchema(null, schemaNode);
} catch (IOException ioe) {
logger.error("Failed to load json schema!", ioe);
throw new SchemaException(ioe);
}
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
return newSchema(null, schemaNode);
}

/**
Expand Down Expand Up @@ -691,13 +681,8 @@ public Schema getSchema(final SchemaLocation schemaUri, final JsonNode jsonNode)
* @return the schema
*/
public Schema getSchema(final SchemaLocation schemaUri, final String schema, InputFormat inputFormat) {
try {
final JsonNode schemaNode = readTree(schema, inputFormat);
return newSchema(schemaUri, schemaNode);
} catch (IOException ioe) {
logger.error("Failed to load json schema!", ioe);
throw new SchemaException(ioe);
}
final JsonNode schemaNode = readTree(schema, inputFormat);
return newSchema(schemaUri, schemaNode);
}

/**
Expand All @@ -709,13 +694,8 @@ public Schema getSchema(final SchemaLocation schemaUri, final String schema, Inp
* @return the schema
*/
public Schema getSchema(final SchemaLocation schemaUri, final InputStream schemaStream, InputFormat inputFormat) {
try {
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
return newSchema(schemaUri, schemaNode);
} catch (IOException ioe) {
logger.error("Failed to load json schema!", ioe);
throw new SchemaException(ioe);
}
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
return newSchema(schemaUri, schemaNode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.networknt.schema.serialization;

import java.io.IOException;
import java.io.InputStream;

import com.networknt.schema.InputFormat;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import com.networknt.schema.InputFormat;

import java.io.InputStream;

/**
* Basic implementation of {@link NodeReader}.
Expand All @@ -39,12 +38,12 @@ protected BasicNodeReader() {
}

@Override
public JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
public JsonNode readTree(String content, InputFormat inputFormat) {
return getObjectMapper(inputFormat).readTree(content);
}

@Override
public JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
public JsonNode readTree(InputStream content, InputFormat inputFormat) {
return getObjectMapper(inputFormat).readTree(content);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.networknt.schema.serialization;

import java.io.IOException;
import java.io.InputStream;

import tools.jackson.databind.JsonNode;
Expand Down Expand Up @@ -33,7 +32,7 @@ protected DefaultNodeReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper,
}

@Override
public JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
public JsonNode readTree(String content, InputFormat inputFormat) {
if (this.jsonNodeFactoryFactory == null) {
return getObjectMapper(inputFormat).readTree(content);
} else {
Expand All @@ -42,7 +41,7 @@ public JsonNode readTree(String content, InputFormat inputFormat) throws IOExcep
}

@Override
public JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
public JsonNode readTree(InputStream content, InputFormat inputFormat) {
if (this.jsonNodeFactoryFactory == null) {
return getObjectMapper(inputFormat).readTree(content);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.networknt.schema.serialization;

import java.io.IOException;
import java.io.InputStream;

import tools.jackson.databind.JsonNode;
Expand All @@ -32,19 +31,17 @@ public interface NodeReader {
* @param content the content
* @param inputFormat the input format
* @return the node
* @throws IOException IOException
*/
JsonNode readTree(String content, InputFormat inputFormat) throws IOException;
JsonNode readTree(String content, InputFormat inputFormat);

/**
* Deserialize content as a tree.
*
* @param content input stream
* @param inputFormat input format
* @return the node
* @throws IOException IOException
*/
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException;
JsonNode readTree(InputStream content, InputFormat inputFormat);

Comment thread
justin-tay marked this conversation as resolved.
/**
* Creates a builder for {@link NodeReader}.
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/networknt/schema/utils/JsonNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.InputStream;

import tools.jackson.core.JacksonException;
import tools.jackson.core.TokenStreamLocation;
import tools.jackson.core.JsonParser;
import tools.jackson.databind.JsonNode;
Expand Down Expand Up @@ -89,8 +88,6 @@ public static JsonNode readTree(ObjectMapper objectMapper, String content,
ObjectReader reader = objectMapper.reader(nodeFactory);
JsonNode result = reader.readTree(parser);
return (result != null) ? result : nodeFactory.missingNode();
} catch (JacksonException e) {
throw new IllegalArgumentException("Invalid input", e);
}
}

Expand All @@ -109,8 +106,6 @@ public static JsonNode readTree(ObjectMapper objectMapper, InputStream inputStre
ObjectReader reader = objectMapper.reader(nodeFactory);
JsonNode result = reader.readTree(parser);
return (result != null) ? result : nodeFactory.missingNode();
} catch (JacksonException e) {
throw new IllegalArgumentException("Invalid input", e);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/networknt/schema/SchemaRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.networknt.schema.dialect.BasicDialectRegistry;
import com.networknt.schema.dialect.Dialect;
import com.networknt.schema.dialect.Dialects;
import tools.jackson.core.JacksonException;

/**
* Tests for JsonSchemaFactory.
Expand Down Expand Up @@ -159,4 +160,12 @@ void noDialectReferredByParentShouldDefaultToDefaultDialect() {
Schema nested = registry.getSchema(SchemaLocation.of("https://example.org/schema"));
assertEquals(Dialects.getDraft4(), nested.getSchemaContext().getDialect());
}

@Test
void invalidJsonSchema() {
SchemaRegistry registry = SchemaRegistry.builder().build();
assertThrows(JacksonException.class, () -> {
registry.getSchema("INVALID_JSON");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.networknt.schema.serialization;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;

import org.junit.jupiter.api.Test;

Expand All @@ -30,7 +29,7 @@
*/
class DefaultNodeReaderTest {
@Test
void location() throws IOException {
void location() {
String schemaData = "{\r\n"
+ " \"$id\": \"https://schema/myschema\",\r\n"
+ " \"properties\": {\r\n"
Expand Down Expand Up @@ -58,7 +57,7 @@ void location() throws IOException {
}

@Test
void jsonLocation() throws IOException {
void jsonLocation() {
String schemaData = "{\r\n"
+ " \"$id\": \"https://schema/myschema\",\r\n"
+ " \"properties\": {\r\n"
Expand All @@ -81,7 +80,7 @@ void jsonLocation() throws IOException {
}

@Test
void yamlLocation() throws IOException {
void yamlLocation() {
String schemaData = "---\r\n"
+ "\"$id\": 'https://schema/myschema'\r\n"
+ "properties:\r\n"
Expand Down
Loading