Skip to content

Commit 9e90c5e

Browse files
committed
tidy
1 parent 22bafb3 commit 9e90c5e

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

json-java21-jtd/src/main/java/json/java21/jtd/Jtd.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public class Jtd {
1717

1818
private static final Logger LOG = Logger.getLogger(Jtd.class.getName());
1919

20+
/// RFC 8927 §2.2.3: Valid primitive types for type schema validation
21+
private static final Set<String> VALID_TYPES = Set.of(
22+
"boolean", "string", "timestamp",
23+
"int8", "uint8", "int16", "uint16", "int32", "uint32",
24+
"float32", "float64"
25+
);
26+
2027
/// Top-level definitions map for ref resolution
2128
private final Map<String, JtdSchema> definitions = new java.util.HashMap<>();
2229

@@ -497,13 +504,7 @@ JtdSchema compileTypeSchema(JsonObject obj) {
497504
String typeStr = str.value();
498505

499506
// RFC 8927 §2.2.3: Validate that type is one of the supported primitive types
500-
Set<String> validTypes = Set.of(
501-
"boolean", "string", "timestamp",
502-
"int8", "uint8", "int16", "uint16", "int32", "uint32",
503-
"float32", "float64"
504-
);
505-
506-
if (!validTypes.contains(typeStr)) {
507+
if (!VALID_TYPES.contains(typeStr)) {
507508
throw new IllegalArgumentException("unknown type: '" + typeStr +
508509
"', expected one of: boolean, string, timestamp, int8, uint8, int16, uint16, int32, uint32, float32, float64");
509510
}
@@ -662,16 +663,16 @@ JtdSchema compileDiscriminatorSchema(JsonObject obj, boolean isRoot) {
662663
void validateDiscriminatorMapping(String mappingKey, JtdSchema variantSchema, String discriminatorKey) {
663664
// Check if this is a nullable schema and unwrap it
664665
JtdSchema unwrappedSchema = variantSchema;
665-
boolean isNullable = false;
666+
boolean wasNullableWrapper = false;
666667

667668
if (variantSchema instanceof JtdSchema.NullableSchema nullableSchema) {
668-
isNullable = true;
669+
wasNullableWrapper = true;
669670
unwrappedSchema = nullableSchema.wrapped();
670671
}
671672

672673
// RFC 8927 §2.2.8: Mapping values must be PropertiesSchema
673674
if (!(unwrappedSchema instanceof JtdSchema.PropertiesSchema)) {
674-
String schemaType = isNullable ? "nullable " + unwrappedSchema.getClass().getSimpleName() :
675+
String schemaType = wasNullableWrapper ? "nullable " + unwrappedSchema.getClass().getSimpleName() :
675676
unwrappedSchema.getClass().getSimpleName();
676677
throw new IllegalArgumentException(
677678
"Discriminator mapping '" + mappingKey + "' must be a properties schema, found: " + schemaType);
@@ -680,7 +681,7 @@ void validateDiscriminatorMapping(String mappingKey, JtdSchema variantSchema, St
680681
JtdSchema.PropertiesSchema propsSchema = (JtdSchema.PropertiesSchema) unwrappedSchema;
681682

682683
// RFC 8927 §2.2.8: Mapped schemas cannot have nullable: true
683-
if (isNullable) {
684+
if (wasNullableWrapper) {
684685
throw new IllegalArgumentException(
685686
"Discriminator mapping '" + mappingKey + "' cannot be nullable");
686687
}

json-java21-jtd/src/test/java/json/java21/jtd/JtdPropertyTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,7 @@ private static Arbitrary<JtdTestSchema> enumSchemaArbitrary() {
264264
// Ensure no duplicates by using distinct values
265265
return Arbitraries.of(ENUM_VALUES).list().ofMinSize(1).ofMaxSize(4).map(values -> {
266266
// Remove duplicates to ensure valid enum schema per RFC 8927
267-
List<String> distinctValues = values.stream().distinct().collect(Collectors.toList());
268-
// Ensure we still have at least one value after deduplication
269-
if (distinctValues.isEmpty()) {
270-
distinctValues = List.of(ENUM_VALUES.get(0)); // Use first value as fallback
271-
}
267+
List<String> distinctValues = values.stream().distinct().toList();
272268
return new EnumSchema(new ArrayList<>(distinctValues));
273269
});
274270
}
@@ -463,12 +459,15 @@ void exhaustiveJtdValidation(@ForAll("jtdSchemas") JtdPropertyTest.JtdTestSchema
463459
final var validationResult = validator.validate(schemaJson, compliantDocument);
464460

465461
if (!validationResult.isValid()) {
466-
LOG.severe(() -> String.format("ERROR: Compliant document failed validation!%nSchema JSON: %s%nDocument JSON: %s%nValidation Errors: %s%nSchema Description: %s%nFull Schema Object: %s",
467-
Json.toDisplayString(schemaJson, 2),
468-
Json.toDisplayString(compliantDocument, 2),
469-
validationResult.errors(),
470-
schemaDescription,
471-
schema));
462+
String errorMessage = String.format(
463+
"ERROR: Compliant document failed validation!%nSchema JSON: %s%nDocument JSON: %s%nValidation Errors: %s%nSchema Description: %s%nFull Schema Object: %s",
464+
Json.toDisplayString(schemaJson, 2),
465+
Json.toDisplayString(compliantDocument, 2),
466+
validationResult.errors(),
467+
schemaDescription,
468+
schema
469+
);
470+
LOG.severe(() -> errorMessage);
472471
}
473472

474473
assertThat(validationResult.isValid()).as("Compliant JTD document should validate for schema %s", schemaDescription).isTrue();

0 commit comments

Comments
 (0)