Skip to content

Commit 7728325

Browse files
authored
Optimize getValueNodeType (#1048)
1 parent 880329b commit 7728325

File tree

3 files changed

+177
-55
lines changed

3 files changed

+177
-55
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.networknt.schema;
1818

19+
/**
20+
* Indicates the type.
21+
*/
1922
public enum JsonType {
2023
OBJECT("object"),
2124
ARRAY("array"),
@@ -29,14 +32,23 @@ public enum JsonType {
2932
UNKNOWN("unknown"),
3033
UNION("union");
3134

32-
private String type;
35+
private final String type;
3336

37+
/**
38+
* Constructor.
39+
*
40+
* @param typeStr the type value
41+
*/
3442
private JsonType(String typeStr) {
35-
type = typeStr;
43+
this.type = typeStr;
3644
}
3745

46+
/**
47+
* Gets the type value.
48+
*/
49+
@Override
3850
public String toString() {
39-
return type;
51+
return this.type;
4052
}
4153

4254
}

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

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.fasterxml.jackson.databind.node.JsonNodeType;
2021

22+
/**
23+
* Type factory.
24+
*/
2125
public class TypeFactory {
26+
/**
27+
* Gets the {@link JsonType} indicated by the schema node.
28+
*
29+
* @param node the schema node
30+
* @return the json type
31+
*/
2232
public static JsonType getSchemaNodeType(JsonNode node) {
2333
//Single Type Definition
2434
if (node.isTextual()) {
@@ -57,37 +67,38 @@ public static JsonType getSchemaNodeType(JsonNode node) {
5767
return JsonType.UNKNOWN;
5868
}
5969

70+
/**
71+
* Gets the {@link JsonType} of the value node.
72+
*
73+
* @param node the value node
74+
* @param config the config
75+
* @return the json type
76+
*/
6077
public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig config) {
61-
if (node.isContainerNode()) {
62-
if (node.isObject())
63-
return JsonType.OBJECT;
64-
if (node.isArray())
65-
return JsonType.ARRAY;
66-
return JsonType.UNKNOWN;
67-
}
68-
69-
if (node.isValueNode()) {
70-
if (node.isTextual())
71-
return JsonType.STRING;
72-
if (node.isBinary())
73-
return JsonType.STRING;
74-
if (node.isIntegralNumber())
78+
JsonNodeType type = node.getNodeType();
79+
switch (type) {
80+
case OBJECT:
81+
return JsonType.OBJECT;
82+
case ARRAY:
83+
return JsonType.ARRAY;
84+
case STRING:
85+
case BINARY:
86+
return JsonType.STRING;
87+
case NUMBER:
88+
if (node.isIntegralNumber()) {
7589
return JsonType.INTEGER;
76-
if (node.isNumber())
77-
if (config != null && config.isJavaSemantics() && node.canConvertToExactIntegral())
78-
return JsonType.INTEGER;
79-
else if (config != null && config.isLosslessNarrowing() && node.canConvertToExactIntegral())
80-
return JsonType.INTEGER;
81-
else
82-
return JsonType.NUMBER;
83-
if (node.isBoolean())
84-
return JsonType.BOOLEAN;
85-
if (node.isNull())
86-
return JsonType.NULL;
90+
} else if (config != null && (config.isJavaSemantics() || config.isLosslessNarrowing())
91+
&& node.canConvertToExactIntegral()) {
92+
return JsonType.INTEGER;
93+
} else {
94+
return JsonType.NUMBER;
95+
}
96+
case BOOLEAN:
97+
return JsonType.BOOLEAN;
98+
case NULL:
99+
return JsonType.NULL;
100+
default:
87101
return JsonType.UNKNOWN;
88102
}
89-
90-
return JsonType.UNKNOWN;
91103
}
92-
93104
}

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

Lines changed: 123 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,35 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.node.DecimalNode;
20-
import org.junit.jupiter.api.Test;
19+
import static com.networknt.schema.TypeFactory.getSchemaNodeType;
20+
import static com.networknt.schema.TypeFactory.getValueNodeType;
21+
import static org.junit.jupiter.api.Assertions.assertSame;
2122

2223
import java.math.BigDecimal;
24+
import java.nio.charset.StandardCharsets;
2325

24-
import static com.networknt.schema.TypeFactory.getValueNodeType;
25-
import static org.junit.jupiter.api.Assertions.assertSame;
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.fasterxml.jackson.databind.node.DecimalNode;
29+
import com.fasterxml.jackson.databind.node.MissingNode;
30+
import com.fasterxml.jackson.databind.node.TextNode;
31+
import com.networknt.schema.serialization.JsonMapperFactory;
2632

33+
/**
34+
* Test for TypeFactory.
35+
*/
2736
public class TypeFactoryTest {
2837

29-
private static final String[] validIntegralValues = {
30-
"1", "-1", "0E+1", "0E1", "-0E+1", "-0E1", "10.1E+1", "10.1E1", "-10.1E+1", "-10.1E1", "1E+0", "1E-0",
31-
"1E0", "1E18", "9223372036854775807", "-9223372036854775808", "1.0", "1.00", "-1.0", "-1.00"
32-
};
38+
private static final String[] validIntegralValues = { "1", "-1", "0E+1", "0E1", "-0E+1", "-0E1", "10.1E+1",
39+
"10.1E1", "-10.1E+1", "-10.1E1", "1E+0", "1E-0", "1E0", "1E18", "9223372036854775807",
40+
"-9223372036854775808", "1.0", "1.00", "-1.0", "-1.00" };
3341

34-
private static final String[] validNonIntegralNumberValues = {
35-
"1.1", "-1.1", "1.10"
36-
};
42+
private static final String[] validNonIntegralNumberValues = { "1.1", "-1.1", "1.10" };
3743

3844
private final SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
3945

4046
@Test
41-
public void testIntegralValuesWithJavaSemantics() {
47+
void testIntegralValuesWithJavaSemantics() {
4248
schemaValidatorsConfig.setJavaSemantics(true);
4349
for (String validValue : validIntegralValues) {
4450
assertSame(JsonType.INTEGER,
@@ -53,7 +59,7 @@ public void testIntegralValuesWithJavaSemantics() {
5359
}
5460

5561
@Test
56-
public void testIntegralValuesWithoutJavaSemantics() {
62+
void testIntegralValuesWithoutJavaSemantics() {
5763
schemaValidatorsConfig.setJavaSemantics(false);
5864
for (String validValue : validIntegralValues) {
5965
assertSame(JsonType.NUMBER,
@@ -67,33 +73,126 @@ public void testIntegralValuesWithoutJavaSemantics() {
6773
}
6874
}
6975

70-
7176
@Test
72-
public void testWithLosslessNarrowing() {
77+
void testWithLosslessNarrowing() {
7378
schemaValidatorsConfig.setLosslessNarrowing(true);
7479
for (String validValue : validIntegralValues) {
7580
assertSame(JsonType.INTEGER,
76-
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig),
77-
validValue);
81+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig), validValue);
7882

7983
assertSame(JsonType.NUMBER,
80-
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig),
81-
validValue);
84+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig), validValue);
8285
}
8386
}
8487

8588
@Test
86-
public void testWithoutLosslessNarrowing() {
89+
void testWithoutLosslessNarrowing() {
8790
schemaValidatorsConfig.setLosslessNarrowing(false);
8891
for (String validValue : validIntegralValues) {
8992
assertSame(JsonType.NUMBER,
90-
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig),
91-
validValue);
93+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig), validValue);
9294

9395
assertSame(JsonType.NUMBER,
94-
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig),
95-
validValue);
96+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig), validValue);
9697
}
9798

9899
}
100+
101+
@Test
102+
void testObjectValue() {
103+
assertSame(JsonType.OBJECT, getValueNodeType(JsonMapperFactory.getInstance().getNodeFactory().objectNode(),
104+
schemaValidatorsConfig));
105+
}
106+
107+
@Test
108+
void testArrayValue() {
109+
assertSame(JsonType.ARRAY,
110+
getValueNodeType(JsonMapperFactory.getInstance().getNodeFactory().arrayNode(), schemaValidatorsConfig));
111+
}
112+
113+
@Test
114+
void testBooleanValue() {
115+
assertSame(JsonType.BOOLEAN, getValueNodeType(
116+
JsonMapperFactory.getInstance().getNodeFactory().booleanNode(true), schemaValidatorsConfig));
117+
}
118+
119+
@Test
120+
public void testNullValue() {
121+
assertSame(JsonType.NULL,
122+
getValueNodeType(JsonMapperFactory.getInstance().getNodeFactory().nullNode(), schemaValidatorsConfig));
123+
}
124+
125+
@Test
126+
void testMissingValue() {
127+
assertSame(JsonType.UNKNOWN, getValueNodeType(JsonMapperFactory.getInstance().getNodeFactory().missingNode(),
128+
schemaValidatorsConfig));
129+
}
130+
131+
@Test
132+
void testIntegerValue() {
133+
assertSame(JsonType.INTEGER, getValueNodeType(JsonMapperFactory.getInstance().getNodeFactory().numberNode(10),
134+
schemaValidatorsConfig));
135+
}
136+
137+
@Test
138+
void testBinaryValue() {
139+
assertSame(JsonType.STRING, getValueNodeType(
140+
JsonMapperFactory.getInstance().getNodeFactory().binaryNode("test".getBytes(StandardCharsets.UTF_8)),
141+
schemaValidatorsConfig));
142+
}
143+
144+
@Test
145+
void testUnknownSchema() {
146+
assertSame(JsonType.UNKNOWN, getSchemaNodeType(TextNode.valueOf("unexpected")));
147+
}
148+
149+
@Test
150+
void testMissingSchema() {
151+
assertSame(JsonType.UNKNOWN, getSchemaNodeType(MissingNode.getInstance()));
152+
}
153+
154+
@Test
155+
void testStringSchema() {
156+
assertSame(JsonType.STRING, getSchemaNodeType(TextNode.valueOf(JsonType.STRING.toString())));
157+
}
158+
159+
@Test
160+
void testObjectSchema() {
161+
assertSame(JsonType.OBJECT, getSchemaNodeType(TextNode.valueOf(JsonType.OBJECT.toString())));
162+
}
163+
164+
@Test
165+
void testArraySchema() {
166+
assertSame(JsonType.ARRAY, getSchemaNodeType(TextNode.valueOf(JsonType.ARRAY.toString())));
167+
}
168+
169+
@Test
170+
void testBooleanSchema() {
171+
assertSame(JsonType.BOOLEAN, getSchemaNodeType(TextNode.valueOf(JsonType.BOOLEAN.toString())));
172+
}
173+
174+
@Test
175+
void testNumberSchema() {
176+
assertSame(JsonType.NUMBER, getSchemaNodeType(TextNode.valueOf(JsonType.NUMBER.toString())));
177+
}
178+
179+
@Test
180+
void testIntegerSchema() {
181+
assertSame(JsonType.INTEGER, getSchemaNodeType(TextNode.valueOf(JsonType.INTEGER.toString())));
182+
}
183+
184+
@Test
185+
void testAnySchema() {
186+
assertSame(JsonType.ANY, getSchemaNodeType(TextNode.valueOf(JsonType.ANY.toString())));
187+
}
188+
189+
@Test
190+
void testNullSchema() {
191+
assertSame(JsonType.NULL, getSchemaNodeType(TextNode.valueOf(JsonType.NULL.toString())));
192+
}
193+
194+
@Test
195+
void testUnionSchema() {
196+
assertSame(JsonType.UNION, getSchemaNodeType(JsonMapperFactory.getInstance().getNodeFactory().arrayNode()));
197+
}
99198
}

0 commit comments

Comments
 (0)