Skip to content

Commit dcb04a9

Browse files
committed
Replace textNode with stringNode
1 parent 854c8e4 commit dcb04a9

File tree

5 files changed

+50
-49
lines changed

5 files changed

+50
-49
lines changed

src/main/java/com/saasquatch/jsonschemainferrer/AdditionalPropertiesPolicies.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static com.saasquatch.jsonschemainferrer.JunkDrawer.stringColToArrayDistinct;
66

77
import com.saasquatch.jsonschemainferrer.annotations.Beta;
8-
import java.util.Objects;
98
import java.util.Set;
109
import java.util.stream.Collectors;
1110
import java.util.stream.Stream;
@@ -56,12 +55,12 @@ public static AdditionalPropertiesPolicy existingTypes() {
5655
final Set<String> existingTypes = stream(schema.path(Consts.Fields.PROPERTIES))
5756
.map(j -> j.path(Consts.Fields.TYPE))
5857
.flatMap(typeNode -> {
59-
if (typeNode.isTextual()) {
60-
return Stream.of(typeNode.textValue());
58+
if (typeNode.isString()) {
59+
return Stream.of(typeNode.stringValue());
6160
} else if (typeNode.isArray()) {
6261
return stream(typeNode)
63-
.map(JsonNode::textValue)
64-
.filter(Objects::nonNull);
62+
.filter(JsonNode::isString)
63+
.map(JsonNode::stringValue);
6564
}
6665
return Stream.empty();
6766
})

src/main/java/com/saasquatch/jsonschemainferrer/BuiltInFormatInferrer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
3030

3131
@Override
3232
public String inferFormat(@Nonnull FormatInferrerInput input) {
33-
final String textValue = input.getSample().textValue();
34-
if (textValue == null) {
33+
if (!input.getSample().isString()) {
3534
return null;
3635
}
36+
final String textValue = input.getSample().stringValue();
3737
try {
38-
//noinspection ResultOfMethodCallIgnored
3938
ZonedDateTime.parse(textValue);
4039
return Consts.Formats.DATE_TIME;
4140
} catch (Exception e) {
4241
// Ignore
4342
}
4443
if (input.getSpecVersion().compareTo(SpecVersion.DRAFT_07) >= 0) {
4544
try {
46-
//noinspection ResultOfMethodCallIgnored
4745
LocalDate.parse(textValue);
4846
return Consts.Formats.DATE;
4947
} catch (Exception e) {
@@ -61,10 +59,10 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
6159
EMAIL {
6260
@Override
6361
public String inferFormat(@Nonnull FormatInferrerInput input) {
64-
final String textValue = input.getSample().textValue();
65-
if (textValue == null) {
62+
if (!input.getSample().isString()) {
6663
return null;
6764
}
65+
final String textValue = input.getSample().stringValue();
6866
if (EmailValidator.getInstance().isValid(textValue)) {
6967
return Consts.Formats.EMAIL;
7068
}
@@ -75,10 +73,10 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
7573
IP {
7674
@Override
7775
public String inferFormat(@Nonnull FormatInferrerInput input) {
78-
final String textValue = input.getSample().textValue();
79-
if (textValue == null) {
76+
if (!input.getSample().isString()) {
8077
return null;
8178
}
79+
final String textValue = input.getSample().stringValue();
8280
if (InetAddressValidator.getInstance().isValidInet4Address(textValue)) {
8381
return Consts.Formats.IPV4;
8482
}

src/main/java/com/saasquatch/jsonschemainferrer/EnumExtractors.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.saasquatch.jsonschemainferrer;
22

33
import static com.saasquatch.jsonschemainferrer.JunkDrawer.isValidEnum;
4+
import static com.saasquatch.jsonschemainferrer.JunkDrawer.textValue;
45

56
import com.saasquatch.jsonschemainferrer.annotations.Beta;
67
import java.util.Arrays;
@@ -36,7 +37,7 @@ public static <E extends Enum<E>> EnumExtractor validEnum(@Nonnull Class<E> enum
3637
Objects.requireNonNull(enumClass);
3738
return input -> {
3839
final Set<? extends JsonNode> enumNodes = input.getSamples().stream()
39-
.filter(j -> isValidEnum(enumClass, j.textValue()))
40+
.filter(j -> isValidEnum(enumClass, textValue(j)))
4041
.collect(Collectors.toSet());
4142
return enumNodes.isEmpty() ? Collections.emptySet() : Collections.singleton(enumNodes);
4243
};

src/test/java/com/saasquatch/jsonschemainferrer/IntegerTypeCriterionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class IntegerTypeCriterionTest {
1616
public void testNonFloatingPoint() {
1717
final Predicate<JsonNode> cr = j -> IntegerTypeCriteria.nonFloatingPoint()
1818
.isInteger(new IntegerTypeCriterionInput(j, SpecVersion.DRAFT_06));
19-
assertFalse(cr.test(jnf.textNode("")));
19+
assertFalse(cr.test(jnf.stringNode("")));
2020
assertTrue(cr.test(jnf.numberNode(1)));
2121
assertTrue(cr.test(jnf.numberNode(1L)));
2222
assertFalse(cr.test(jnf.numberNode(BigDecimal.valueOf(0L))));
@@ -37,7 +37,7 @@ public void testNonFloatingPoint() {
3737
public void testMathematicalInteger() {
3838
final Predicate<JsonNode> cr = j -> IntegerTypeCriteria.mathematicalInteger()
3939
.isInteger(new IntegerTypeCriterionInput(j, SpecVersion.DRAFT_06));
40-
assertFalse(cr.test(jnf.textNode("")));
40+
assertFalse(cr.test(jnf.stringNode("")));
4141
assertTrue(cr.test(jnf.numberNode(1)));
4242
assertTrue(cr.test(jnf.numberNode(1L)));
4343
assertTrue(cr.test(jnf.numberNode(BigDecimal.valueOf(0L))));

src/test/java/com/saasquatch/jsonschemainferrer/JsonSchemaInferrerOptionsTest.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public void testJsonTypeInference() {
7272
public void testFormatInferrers() {
7373
// Fake format inferrer that always uses the string length as the format
7474
final FormatInferrer testStrLenFormatInferrer = input -> {
75-
final String textValue = input.getSample().textValue();
7675
assertNotNull(input.getSpecVersion());
77-
if (textValue == null) {
76+
if (!input.getSample().isString()) {
7877
return null;
7978
}
79+
final String textValue = input.getSample().stringValue();
8080
return String.valueOf(textValue.length());
8181
};
8282
assertSame(FormatInferrers.noOp(), FormatInferrers.chained());
@@ -89,70 +89,72 @@ public void testFormatInferrers() {
8989
.addFormatInferrers(FormatInferrers.dateTime())
9090
.setSpecVersion(SpecVersion.DRAFT_07)
9191
.build()
92-
.inferForSample(jnf.textNode("aaaaaaaaa"))
92+
.inferForSample(jnf.stringNode("aaaaaaaaa"))
9393
.path("format").textValue());
9494
assertEquals("date-time", JsonSchemaInferrer.newBuilder()
9595
.addFormatInferrers(FormatInferrers.dateTime())
9696
.build()
97-
.inferForSample(jnf.textNode(Instant.now().toString()))
97+
.inferForSample(jnf.stringNode(Instant.now().toString()))
9898
.path("format").textValue());
9999
assertNull(JsonSchemaInferrer.newBuilder()
100100
.addFormatInferrers(FormatInferrers.dateTime())
101101
.setSpecVersion(SpecVersion.DRAFT_06)
102102
.build()
103-
.inferForSample(jnf.textNode("1900-01-01"))
103+
.inferForSample(jnf.stringNode("1900-01-01"))
104104
.path("format").textValue());
105105
assertEquals("date", JsonSchemaInferrer.newBuilder()
106106
.addFormatInferrers(FormatInferrers.dateTime())
107107
.setSpecVersion(SpecVersion.DRAFT_07)
108108
.build()
109-
.inferForSample(jnf.textNode("1900-01-01"))
109+
.inferForSample(jnf.stringNode("1900-01-01"))
110110
.path("format").textValue());
111111
assertNull(JsonSchemaInferrer.newBuilder()
112112
.addFormatInferrers(FormatInferrers.dateTime())
113113
.setSpecVersion(SpecVersion.DRAFT_06)
114114
.build()
115-
.inferForSample(jnf.textNode("20:20:39"))
115+
.inferForSample(jnf.stringNode("20:20:39"))
116116
.path("format").textValue());
117117
assertEquals("time", JsonSchemaInferrer.newBuilder()
118118
.addFormatInferrers(FormatInferrers.dateTime())
119119
.setSpecVersion(SpecVersion.DRAFT_07).build()
120-
.inferForSample(jnf.textNode("20:20:39+01:23"))
120+
.inferForSample(jnf.stringNode("20:20:39+01:23"))
121121
.path("format").textValue());
122122
assertEquals("time", JsonSchemaInferrer.newBuilder()
123123
.addFormatInferrers(FormatInferrers.dateTime())
124124
.setSpecVersion(SpecVersion.DRAFT_07).build()
125-
.inferForSample(jnf.textNode("20:20:39+00:00"))
125+
.inferForSample(jnf.stringNode("20:20:39+00:00"))
126126
.path("format").textValue());
127127
{
128128
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
129129
.addFormatInferrers(FormatInferrers.chained(
130130
FormatInferrers.dateTime(), testStrLenFormatInferrer))
131131
.build();
132-
assertEquals("date-time", inferrer.inferForSample(jnf.textNode(Instant.now().toString()))
132+
assertEquals("date-time", inferrer.inferForSample(jnf.stringNode(Instant.now().toString()))
133133
.path("format").textValue());
134-
assertEquals("0", inferrer.inferForSample(jnf.textNode("")).path("format").textValue());
134+
assertEquals("0", inferrer.inferForSample(jnf.stringNode("")).path("format").textValue());
135135
}
136136
{
137137
final JsonSchemaInferrer inferrer =
138138
JsonSchemaInferrer.newBuilder().addFormatInferrers(FormatInferrers.ip()).build();
139139
assertEquals("ipv4",
140-
inferrer.inferForSample(jnf.textNode("12.34.56.78")).path("format").textValue());
140+
inferrer.inferForSample(jnf.stringNode("12.34.56.78")).path("format").textValue());
141141
assertEquals("ipv6",
142-
inferrer.inferForSample(jnf.textNode("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
142+
inferrer.inferForSample(jnf.stringNode("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
143143
.path("format").textValue());
144144
assertEquals("ipv6",
145-
inferrer.inferForSample(jnf.textNode("a::F")).path("format").textValue());
146-
assertEquals("ipv6", inferrer.inferForSample(jnf.textNode("5::")).path("format").textValue());
147-
assertEquals("ipv6", inferrer.inferForSample(jnf.textNode("::")).path("format").textValue());
145+
inferrer.inferForSample(jnf.stringNode("a::F")).path("format").textValue());
146+
assertEquals("ipv6",
147+
inferrer.inferForSample(jnf.stringNode("5::")).path("format").textValue());
148+
assertEquals("ipv6",
149+
inferrer.inferForSample(jnf.stringNode("::")).path("format").textValue());
148150
}
149151
{
150152
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
151153
.addFormatInferrers(FormatInferrers.chained(
152154
FormatInferrers.noOp(), FormatInferrers.noOp(), FormatInferrers.noOp(),
153155
FormatInferrers.ip(), FormatInferrers.email()))
154156
.build();
155-
assertNull(inferrer.inferForSample(jnf.textNode(Instant.now().toString())).get("format"));
157+
assertNull(inferrer.inferForSample(jnf.stringNode(Instant.now().toString())).get("format"));
156158
}
157159
{
158160
final String dateTimeString = Instant.now().toString();
@@ -161,8 +163,8 @@ public void testFormatInferrers() {
161163
testStrLenFormatInferrer, FormatInferrers.dateTime()))
162164
.build();
163165
assertEquals(String.valueOf(dateTimeString.length()),
164-
inferrer.inferForSample(jnf.textNode(dateTimeString)).path("format").textValue());
165-
assertEquals("0", inferrer.inferForSample(jnf.textNode("")).path("format").textValue());
166+
inferrer.inferForSample(jnf.stringNode(dateTimeString)).path("format").textValue());
167+
assertEquals("0", inferrer.inferForSample(jnf.stringNode("")).path("format").textValue());
166168
}
167169
}
168170

@@ -368,7 +370,7 @@ public String generateDescription(@Nonnull TitleDescriptionGeneratorInput input)
368370
@Test
369371
public void testDefault() {
370372
final List<JsonNode> samples =
371-
ImmutableList.of(jnf.textNode("a"), jnf.textNode("b"), jnf.textNode("c"));
373+
ImmutableList.of(jnf.stringNode("a"), jnf.stringNode("b"), jnf.stringNode("c"));
372374
{
373375
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
374376
.setDefaultPolicy(DefaultPolicies.noOp())
@@ -408,7 +410,7 @@ public void testExamples() {
408410
.setExamplesPolicy(ExamplesPolicies.useFirstSamples(3))
409411
.build();
410412
final ObjectNode schema = inferrer.inferForSamples(IntStream.range(0, 5)
411-
.mapToObj(Integer::toString).map(jnf::textNode).collect(Collectors.toList()));
413+
.mapToObj(Integer::toString).map(jnf::stringNode).collect(Collectors.toList()));
412414
assertNull(schema.get("examples"));
413415
}
414416
{
@@ -417,7 +419,7 @@ public void testExamples() {
417419
.setExamplesPolicy(ExamplesPolicies.useFirstSamples(3))
418420
.build();
419421
final ObjectNode schema = inferrer.inferForSamples(IntStream.range(0, 5)
420-
.mapToObj(Integer::toString).map(jnf::textNode).collect(Collectors.toList()));
422+
.mapToObj(Integer::toString).map(jnf::stringNode).collect(Collectors.toList()));
421423
assertEquals(ImmutableSet.of("0", "1", "2"), toStringSet(schema.path("examples")));
422424
}
423425
{
@@ -426,7 +428,7 @@ public void testExamples() {
426428
.setExamplesPolicy(ExamplesPolicies.useFirstSamples(3, "boolean"::equals))
427429
.build();
428430
final ObjectNode schema = inferrer.inferForSamples(IntStream.range(0, 5)
429-
.mapToObj(Integer::toString).map(jnf::textNode).collect(Collectors.toList()));
431+
.mapToObj(Integer::toString).map(jnf::stringNode).collect(Collectors.toList()));
430432
assertNull(schema.get("examples"));
431433
}
432434
{
@@ -441,7 +443,7 @@ public void testExamples() {
441443
.setExamplesPolicy(ExamplesPolicies.useFirstSamples(3, "string"::equals))
442444
.build();
443445
final ObjectNode schema = inferrer.inferForSamples(IntStream.range(0, 5)
444-
.mapToObj(Integer::toString).map(jnf::textNode).collect(Collectors.toList()));
446+
.mapToObj(Integer::toString).map(jnf::stringNode).collect(Collectors.toList()));
445447
assertEquals(ImmutableSet.of("0", "1", "2"), toStringSet(schema.path("examples")));
446448
}
447449
{
@@ -452,8 +454,9 @@ public void testExamples() {
452454
return null;
453455
})
454456
.build();
455-
inferrer.inferForSamples(IntStream.range(0, 5).mapToObj(Integer::toString).map(jnf::textNode)
456-
.collect(Collectors.toList()));
457+
inferrer.inferForSamples(
458+
IntStream.range(0, 5).mapToObj(Integer::toString).map(jnf::stringNode)
459+
.collect(Collectors.toList()));
457460
}
458461
{
459462
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
@@ -477,7 +480,7 @@ public void testMultipleOf() {
477480
ImmutableList.of(jnf.numberNode(0), jnf.numberNode(0), jnf.numberNode(0));
478481
final List<JsonNode> samples4 =
479482
ImmutableList.of(jnf.numberNode(2), jnf.numberNode(4), jnf.numberNode(6.5));
480-
final List<JsonNode> samples5 = ImmutableList.of(jnf.textNode("foo"), jnf.textNode("bar"));
483+
final List<JsonNode> samples5 = ImmutableList.of(jnf.stringNode("foo"), jnf.stringNode("bar"));
481484
{
482485
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
483486
.setMultipleOfPolicy(input -> {
@@ -547,7 +550,7 @@ public void testEnum() {
547550
assertSame(EnumExtractors.noOp(), EnumExtractors.chained());
548551
assertSame(EnumExtractors.noOp(), EnumExtractors.chained(EnumExtractors.noOp()));
549552
final List<JsonNode> timeUnitSamples = Stream.of(TimeUnit.DAYS, TimeUnit.HOURS)
550-
.map(tu -> jnf.textNode(tu.name())).collect(ImmutableList.toImmutableList());
553+
.map(tu -> jnf.stringNode(tu.name())).collect(ImmutableList.toImmutableList());
551554
{
552555
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder().build();
553556
final ObjectNode schema = inferrer.inferForSamples(timeUnitSamples);
@@ -591,8 +594,8 @@ public void testEnum() {
591594
}))
592595
.build();
593596
final ObjectNode schema =
594-
inferrer.inferForSamples(Arrays.asList(jnf.textNode("foo"), jnf.numberNode(123)));
595-
assertEquals(ImmutableSet.of(jnf.textNode("foo"), jnf.numberNode(123)),
597+
inferrer.inferForSamples(Arrays.asList(jnf.stringNode("foo"), jnf.numberNode(123)));
598+
assertEquals(ImmutableSet.of(jnf.stringNode("foo"), jnf.numberNode(123)),
596599
stream(schema.get("enum")).collect(Collectors.toSet()));
597600
}
598601
{
@@ -601,7 +604,7 @@ public void testEnum() {
601604
EnumExtractors.validEnum(DayOfWeek.class)))
602605
.build();
603606
final ObjectNode schema =
604-
inferrer.inferForSamples(Arrays.asList(jnf.textNode("TUESDAY"), jnf.textNode("MARCH")));
607+
inferrer.inferForSamples(Arrays.asList(jnf.stringNode("TUESDAY"), jnf.stringNode("MARCH")));
605608
final JsonNode anyOf = schema.get("anyOf");
606609
assertTrue(anyOf.isArray());
607610
assertTrue(
@@ -669,7 +672,7 @@ public void testArrayLengthFeatures() {
669672
@Test
670673
public void testStringLengthFeatures() {
671674
final List<JsonNode> samples =
672-
ImmutableList.of(jnf.textNode(""), jnf.textNode("a"), jnf.textNode("foobar"));
675+
ImmutableList.of(jnf.stringNode(""), jnf.stringNode("a"), jnf.stringNode("foobar"));
673676
{
674677
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
675678
.setStringLengthFeatures(EnumSet.of(StringLengthFeature.MIN_LENGTH))
@@ -702,7 +705,7 @@ public void testStringLengthFeatures() {
702705
@Test
703706
public void testStringLengthAndFormat() {
704707
final String dateTimeString = Instant.now().toString();
705-
final List<JsonNode> samples = ImmutableList.of(jnf.textNode(dateTimeString));
708+
final List<JsonNode> samples = ImmutableList.of(jnf.stringNode(dateTimeString));
706709
final JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
707710
.setStringLengthFeatures(EnumSet.allOf(StringLengthFeature.class))
708711
.addFormatInferrers(FormatInferrers.dateTime())

0 commit comments

Comments
 (0)