Skip to content

Commit 331ced5

Browse files
afilubaafiluba
andauthored
Cleanup of IOException leftovers after update to jackson3 (#1244)
* fix: adapt to jackson3 exception (JacksonException vs IOException) * fix: let the JacksonException be thrown * test: add invalid json schema test --------- Co-authored-by: afiluba <afiluba@gmail.com>
1 parent a6ae62c commit 331ced5

8 files changed

Lines changed: 32 additions & 58 deletions

File tree

src/main/java/com/networknt/schema/Schema.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,11 +1188,7 @@ public <T> T validate(ExecutionContext executionContext, JsonNode node, OutputFo
11881188
* @return the JsonNode.
11891189
*/
11901190
private JsonNode deserialize(String input, InputFormat inputFormat) {
1191-
try {
1192-
return this.getSchemaContext().getSchemaRegistry().readTree(input, inputFormat);
1193-
} catch (IOException e) {
1194-
throw new UncheckedIOException("Invalid input", e);
1195-
}
1191+
return this.getSchemaContext().getSchemaRegistry().readTree(input, inputFormat);
11961192
}
11971193

11981194
/**

src/main/java/com/networknt/schema/SchemaRegistry.java

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,11 @@ public Dialect getDialect(String dialectId) {
584584
return dialectRegistry.getDialect(key, this);
585585
}
586586

587-
JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
587+
JsonNode readTree(String content, InputFormat inputFormat) {
588588
return this.nodeReader.readTree(content, inputFormat);
589589
}
590590

591-
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
591+
JsonNode readTree(InputStream content, InputFormat inputFormat) {
592592
return this.nodeReader.readTree(content, inputFormat);
593593
}
594594

@@ -616,13 +616,8 @@ public Schema getSchema(final String schema) {
616616
* @return the schema
617617
*/
618618
public Schema getSchema(final String schema, InputFormat inputFormat) {
619-
try {
620-
final JsonNode schemaNode = readTree(schema, inputFormat);
621-
return newSchema(null, schemaNode);
622-
} catch (IOException ioe) {
623-
logger.error("Failed to load json schema!", ioe);
624-
throw new SchemaException(ioe);
625-
}
619+
final JsonNode schemaNode = readTree(schema, inputFormat);
620+
return newSchema(null, schemaNode);
626621
}
627622

628623
/**
@@ -649,13 +644,8 @@ public Schema getSchema(final InputStream schemaStream) {
649644
* @return the schema
650645
*/
651646
public Schema getSchema(final InputStream schemaStream, InputFormat inputFormat) {
652-
try {
653-
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
654-
return newSchema(null, schemaNode);
655-
} catch (IOException ioe) {
656-
logger.error("Failed to load json schema!", ioe);
657-
throw new SchemaException(ioe);
658-
}
647+
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
648+
return newSchema(null, schemaNode);
659649
}
660650

661651
/**
@@ -691,13 +681,8 @@ public Schema getSchema(final SchemaLocation schemaUri, final JsonNode jsonNode)
691681
* @return the schema
692682
*/
693683
public Schema getSchema(final SchemaLocation schemaUri, final String schema, InputFormat inputFormat) {
694-
try {
695-
final JsonNode schemaNode = readTree(schema, inputFormat);
696-
return newSchema(schemaUri, schemaNode);
697-
} catch (IOException ioe) {
698-
logger.error("Failed to load json schema!", ioe);
699-
throw new SchemaException(ioe);
700-
}
684+
final JsonNode schemaNode = readTree(schema, inputFormat);
685+
return newSchema(schemaUri, schemaNode);
701686
}
702687

703688
/**
@@ -709,13 +694,8 @@ public Schema getSchema(final SchemaLocation schemaUri, final String schema, Inp
709694
* @return the schema
710695
*/
711696
public Schema getSchema(final SchemaLocation schemaUri, final InputStream schemaStream, InputFormat inputFormat) {
712-
try {
713-
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
714-
return newSchema(schemaUri, schemaNode);
715-
} catch (IOException ioe) {
716-
logger.error("Failed to load json schema!", ioe);
717-
throw new SchemaException(ioe);
718-
}
697+
final JsonNode schemaNode = readTree(schemaStream, inputFormat);
698+
return newSchema(schemaUri, schemaNode);
719699
}
720700

721701
/**

src/main/java/com/networknt/schema/serialization/BasicNodeReader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

1717
package com.networknt.schema.serialization;
1818

19-
import java.io.IOException;
20-
import java.io.InputStream;
21-
19+
import com.networknt.schema.InputFormat;
2220
import tools.jackson.databind.JsonNode;
2321
import tools.jackson.databind.ObjectMapper;
24-
import com.networknt.schema.InputFormat;
22+
23+
import java.io.InputStream;
2524

2625
/**
2726
* Basic implementation of {@link NodeReader}.
@@ -39,12 +38,12 @@ protected BasicNodeReader() {
3938
}
4039

4140
@Override
42-
public JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
41+
public JsonNode readTree(String content, InputFormat inputFormat) {
4342
return getObjectMapper(inputFormat).readTree(content);
4443
}
4544

4645
@Override
47-
public JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
46+
public JsonNode readTree(InputStream content, InputFormat inputFormat) {
4847
return getObjectMapper(inputFormat).readTree(content);
4948
}
5049

src/main/java/com/networknt/schema/serialization/DefaultNodeReader.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.networknt.schema.serialization;
22

3-
import java.io.IOException;
43
import java.io.InputStream;
54

65
import tools.jackson.databind.JsonNode;
@@ -33,7 +32,7 @@ protected DefaultNodeReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper,
3332
}
3433

3534
@Override
36-
public JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
35+
public JsonNode readTree(String content, InputFormat inputFormat) {
3736
if (this.jsonNodeFactoryFactory == null) {
3837
return getObjectMapper(inputFormat).readTree(content);
3938
} else {
@@ -42,7 +41,7 @@ public JsonNode readTree(String content, InputFormat inputFormat) throws IOExcep
4241
}
4342

4443
@Override
45-
public JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
44+
public JsonNode readTree(InputStream content, InputFormat inputFormat) {
4645
if (this.jsonNodeFactoryFactory == null) {
4746
return getObjectMapper(inputFormat).readTree(content);
4847
} else {

src/main/java/com/networknt/schema/serialization/NodeReader.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.networknt.schema.serialization;
1717

18-
import java.io.IOException;
1918
import java.io.InputStream;
2019

2120
import tools.jackson.databind.JsonNode;
@@ -32,19 +31,17 @@ public interface NodeReader {
3231
* @param content the content
3332
* @param inputFormat the input format
3433
* @return the node
35-
* @throws IOException IOException
3634
*/
37-
JsonNode readTree(String content, InputFormat inputFormat) throws IOException;
35+
JsonNode readTree(String content, InputFormat inputFormat);
3836

3937
/**
4038
* Deserialize content as a tree.
4139
*
4240
* @param content input stream
4341
* @param inputFormat input format
4442
* @return the node
45-
* @throws IOException IOException
4643
*/
47-
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException;
44+
JsonNode readTree(InputStream content, InputFormat inputFormat);
4845

4946
/**
5047
* Creates a builder for {@link NodeReader}.

src/main/java/com/networknt/schema/utils/JsonNodes.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.io.InputStream;
1919

20-
import tools.jackson.core.JacksonException;
2120
import tools.jackson.core.TokenStreamLocation;
2221
import tools.jackson.core.JsonParser;
2322
import tools.jackson.databind.JsonNode;
@@ -89,8 +88,6 @@ public static JsonNode readTree(ObjectMapper objectMapper, String content,
8988
ObjectReader reader = objectMapper.reader(nodeFactory);
9089
JsonNode result = reader.readTree(parser);
9190
return (result != null) ? result : nodeFactory.missingNode();
92-
} catch (JacksonException e) {
93-
throw new IllegalArgumentException("Invalid input", e);
9491
}
9592
}
9693

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

src/test/java/com/networknt/schema/SchemaRegistryTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.networknt.schema.dialect.BasicDialectRegistry;
3131
import com.networknt.schema.dialect.Dialect;
3232
import com.networknt.schema.dialect.Dialects;
33+
import tools.jackson.core.JacksonException;
3334

3435
/**
3536
* Tests for JsonSchemaFactory.
@@ -159,4 +160,12 @@ void noDialectReferredByParentShouldDefaultToDefaultDialect() {
159160
Schema nested = registry.getSchema(SchemaLocation.of("https://example.org/schema"));
160161
assertEquals(Dialects.getDraft4(), nested.getSchemaContext().getDialect());
161162
}
163+
164+
@Test
165+
void invalidJsonSchema() {
166+
SchemaRegistry registry = SchemaRegistry.builder().build();
167+
assertThrows(JacksonException.class, () -> {
168+
registry.getSchema("INVALID_JSON");
169+
});
170+
}
162171
}

src/test/java/com/networknt/schema/serialization/DefaultNodeReaderTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.networknt.schema.serialization;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
import java.io.IOException;
2019

2120
import org.junit.jupiter.api.Test;
2221

@@ -30,7 +29,7 @@
3029
*/
3130
class DefaultNodeReaderTest {
3231
@Test
33-
void location() throws IOException {
32+
void location() {
3433
String schemaData = "{\r\n"
3534
+ " \"$id\": \"https://schema/myschema\",\r\n"
3635
+ " \"properties\": {\r\n"
@@ -58,7 +57,7 @@ void location() throws IOException {
5857
}
5958

6059
@Test
61-
void jsonLocation() throws IOException {
60+
void jsonLocation() {
6261
String schemaData = "{\r\n"
6362
+ " \"$id\": \"https://schema/myschema\",\r\n"
6463
+ " \"properties\": {\r\n"
@@ -81,7 +80,7 @@ void jsonLocation() throws IOException {
8180
}
8281

8382
@Test
84-
void yamlLocation() throws IOException {
83+
void yamlLocation() {
8584
String schemaData = "---\r\n"
8685
+ "\"$id\": 'https://schema/myschema'\r\n"
8786
+ "properties:\r\n"

0 commit comments

Comments
 (0)