Skip to content

Commit f443adf

Browse files
authored
Map BinaryNodes to type string (#651)
* Map BinaryNodes to type string ..to support using Java models with type byte[]. close #650 * Added a test case for issue #650
1 parent 5b15c46 commit f443adf

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<modelVersion>4.0.0</modelVersion>
2121
<groupId>com.networknt</groupId>
2222
<artifactId>json-schema-validator</artifactId>
23-
<version>1.0.76</version>
23+
<version>1.0.77</version>
2424
<packaging>bundle</packaging>
2525
<description>A json schema validator that supports draft v4, v6, v7, v2019-09 and v2020-12</description>
2626
<url>https://github.com/networknt/json-schema-validator</url>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig co
6969
if (node.isValueNode()) {
7070
if (node.isTextual())
7171
return JsonType.STRING;
72+
if (node.isBinary())
73+
return JsonType.STRING;
7274
if (node.isIntegralNumber())
7375
return JsonType.INTEGER;
7476
if (node.isNumber())
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.networknt.schema;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import java.io.InputStream;
5+
import java.util.Set;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import com.fasterxml.jackson.annotation.JsonInclude;
9+
import com.fasterxml.jackson.databind.DeserializationFeature;
10+
import com.fasterxml.jackson.databind.JsonNode;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.SerializationFeature;
13+
import com.fasterxml.jackson.databind.node.BinaryNode;
14+
15+
/**
16+
*
17+
* created at 07.02.2023
18+
*
19+
* @author k-oliver
20+
* @since 1.0.77
21+
*/
22+
public class Issue650Test {
23+
24+
/**
25+
* Test using a Java model with a byte[] property which jackson converts to a BASE64 encoded string automatically. Then convert into
26+
* a jackson tree. The resulting node is of type {@link BinaryNode}. This test checks if validation handles the {@link BinaryNode} as string
27+
* when validating.
28+
*
29+
* @throws Exception
30+
* @since 1.0.77
31+
*/
32+
@Test
33+
public void testBinaryNode() throws Exception {
34+
final ObjectMapper mapper = new ObjectMapper();
35+
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
36+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
37+
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
38+
39+
// schema with data property of type string:
40+
InputStream schemaInputStream = getClass().getResourceAsStream("/draft7/issue650.json");
41+
JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaInputStream);
42+
43+
// create model first:
44+
Issue650Test.Model model = new Issue650Test.Model();
45+
model.setData("content".getBytes("UTF-8"));
46+
// now convert to tree. The resulting type of the data property is BinaryNode now:
47+
JsonNode node = mapper.valueToTree(model);
48+
49+
// validate:
50+
Set<ValidationMessage> errors = schema.validate(node);
51+
52+
// check result:
53+
Assertions.assertTrue(errors.isEmpty());
54+
}
55+
56+
/**
57+
* created at 07.02.2023
58+
*
59+
* @author Oliver Kelling
60+
* @since 1.0.77
61+
*/
62+
private static class Model {
63+
private byte[] data;
64+
65+
66+
/**
67+
* @return the data
68+
* @since 1.0.77
69+
*/
70+
public byte[] getData() {
71+
return this.data;
72+
}
73+
74+
75+
/**
76+
* @param data the data to set
77+
* @since 1.0.77
78+
*/
79+
public void setData(byte[] data) {
80+
this.data = data;
81+
}
82+
83+
}
84+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"required": [
5+
"data"
6+
],
7+
"properties": {
8+
"data": {
9+
"$id": "#/properties/data",
10+
"type": "string"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)