Skip to content

Commit 4b60358

Browse files
committed
JDT code tidy-up
1 parent cbe3523 commit 4b60358

File tree

5 files changed

+25
-110
lines changed

5 files changed

+25
-110
lines changed

.tmp/keep

Whitespace-only changes.

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

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import java.util.ArrayList;
77
import java.util.Collections;
8-
import java.util.HashSet;
8+
99
import java.util.List;
1010
import java.util.Map;
1111
import java.util.Set;
@@ -49,12 +49,12 @@ static String enrichedError(String baseMessage, Frame frame, JsonValue contextVa
4949
}
5050

5151
/// Compiles a JTD schema and throws exceptions for invalid schemas
52+
///
5253
/// @param schema The JTD schema as a JsonValue
53-
/// @return Compiled JtdSchema
5454
/// @throws IllegalArgumentException if the schema is invalid
55-
public JtdSchema compile(JsonValue schema) {
55+
public void compile(JsonValue schema) {
5656
definitions.clear();
57-
return compileSchema(schema);
57+
compileSchema(schema);
5858
}
5959

6060
/// Validates a JSON instance against a JTD schema
@@ -324,19 +324,13 @@ JtdSchema compileSchema(JsonValue schema, boolean isRoot) {
324324
}
325325
}
326326

327-
return compileObjectSchema(obj, isRoot);
327+
return compileObjectSchema(obj);
328328
}
329-
330-
/// Compiles an object schema according to RFC 8927 with strict semantics
331-
JtdSchema compileObjectSchema(JsonObject obj) {
332-
return compileObjectSchema(obj, true); // Default to root schema
333-
}
334-
329+
335330
/// Compiles an object schema according to RFC 8927 with strict semantics
336331
/// @param obj The JSON object to compile
337-
/// @param isRoot Whether this is a root-level schema (can contain definitions)
338332
/// @return Compiled JtdSchema
339-
JtdSchema compileObjectSchema(JsonObject obj, boolean isRoot) {
333+
JtdSchema compileObjectSchema(JsonObject obj) {
340334
// Check for mutually-exclusive schema forms
341335
List<String> forms = new ArrayList<>();
342336
Map<String, JsonValue> members = obj.members();
@@ -357,7 +351,7 @@ JtdSchema compileObjectSchema(JsonObject obj, boolean isRoot) {
357351

358352
// RFC 8927: Check for form-specific properties that shouldn't be mixed
359353
if (forms.size() == 1) {
360-
String form = forms.get(0);
354+
String form = forms.getFirst();
361355
switch (form) {
362356
case "elements", "values", "enum", "ref", "type" -> {
363357
// These forms should not have properties-specific attributes
@@ -445,11 +439,11 @@ JtdSchema compileObjectSchema(JsonObject obj, boolean isRoot) {
445439
case "ref" -> compileRefSchema(obj);
446440
case "type" -> compileTypeSchema(obj);
447441
case "enum" -> compileEnumSchema(obj);
448-
case "elements" -> compileElementsSchema(obj, isRoot);
449-
case "properties" -> compilePropertiesSchema(obj, isRoot);
450-
case "optionalProperties" -> compilePropertiesSchema(obj, isRoot); // handled together
451-
case "values" -> compileValuesSchema(obj, isRoot);
452-
case "discriminator" -> compileDiscriminatorSchema(obj, isRoot);
442+
case "elements" -> compileElementsSchema(obj);
443+
case "properties" -> compilePropertiesSchema(obj);
444+
case "optionalProperties" -> compilePropertiesSchema(obj); // handled together
445+
case "values" -> compileValuesSchema(obj);
446+
case "discriminator" -> compileDiscriminatorSchema(obj);
453447
default -> throw new IllegalArgumentException("Unknown schema form: " + form);
454448
};
455449
}
@@ -542,21 +536,13 @@ JtdSchema compileEnumSchema(JsonObject obj) {
542536
}
543537

544538
JtdSchema compileElementsSchema(JsonObject obj) {
545-
return compileElementsSchema(obj, true); // Default to root
546-
}
547-
548-
JtdSchema compileElementsSchema(JsonObject obj, boolean isRoot) {
549539
Map<String, JsonValue> members = obj.members();
550540
JsonValue elementsValue = members.get("elements");
551541
JtdSchema elementsSchema = compileSchema(elementsValue, false); // Elements are nested schemas
552542
return new JtdSchema.ElementsSchema(elementsSchema);
553543
}
554-
544+
555545
JtdSchema compilePropertiesSchema(JsonObject obj) {
556-
return compilePropertiesSchema(obj, true); // Default to root
557-
}
558-
559-
JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
560546
Map<String, JtdSchema> properties = Map.of();
561547
Map<String, JtdSchema> optionalProperties = Map.of();
562548

@@ -568,7 +554,7 @@ JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
568554
if (!(propsValue instanceof JsonObject propsObj)) {
569555
throw new IllegalArgumentException("properties must be an object");
570556
}
571-
properties = parsePropertySchemas(propsObj, false); // Property schemas are nested
557+
properties = parsePropertySchemas(propsObj); // Property schemas are nested
572558
}
573559

574560
// Parse optional properties
@@ -577,7 +563,7 @@ JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
577563
if (!(optPropsValue instanceof JsonObject optPropsObj)) {
578564
throw new IllegalArgumentException("optionalProperties must be an object");
579565
}
580-
optionalProperties = parsePropertySchemas(optPropsObj, false); // Property schemas are nested
566+
optionalProperties = parsePropertySchemas(optPropsObj); // Property schemas are nested
581567
}
582568

583569
// RFC 8927: Check for key overlap between properties and optionalProperties
@@ -603,21 +589,13 @@ JtdSchema compilePropertiesSchema(JsonObject obj, boolean isRoot) {
603589
}
604590

605591
JtdSchema compileValuesSchema(JsonObject obj) {
606-
return compileValuesSchema(obj, true); // Default to root
607-
}
608-
609-
JtdSchema compileValuesSchema(JsonObject obj, boolean isRoot) {
610592
Map<String, JsonValue> members = obj.members();
611593
JsonValue valuesValue = members.get("values");
612594
JtdSchema valuesSchema = compileSchema(valuesValue, false); // Values are nested schemas
613595
return new JtdSchema.ValuesSchema(valuesSchema);
614596
}
615-
597+
616598
JtdSchema compileDiscriminatorSchema(JsonObject obj) {
617-
return compileDiscriminatorSchema(obj, true); // Default to root
618-
}
619-
620-
JtdSchema compileDiscriminatorSchema(JsonObject obj, boolean isRoot) {
621599
Map<String, JsonValue> members = obj.members();
622600
JsonValue discriminatorValue = members.get("discriminator");
623601
if (!(discriminatorValue instanceof JsonString discStr)) {
@@ -704,15 +682,10 @@ void validateDiscriminatorMapping(String mappingKey, JtdSchema variantSchema, St
704682

705683
/// Extracts and stores top-level definitions for ref resolution
706684
private Map<String, JtdSchema> parsePropertySchemas(JsonObject propsObj) {
707-
return parsePropertySchemas(propsObj, true); // Default to root
708-
}
709-
710-
/// Extracts and stores top-level definitions for ref resolution
711-
private Map<String, JtdSchema> parsePropertySchemas(JsonObject propsObj, boolean isRoot) {
712685
Map<String, JtdSchema> schemas = new java.util.HashMap<>();
713686
for (String key : propsObj.members().keySet()) {
714687
JsonValue schemaValue = propsObj.members().get(key);
715-
schemas.put(key, compileSchema(schemaValue, isRoot));
688+
schemas.put(key, compileSchema(schemaValue, false));
716689
}
717690
return schemas;
718691
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,6 @@ boolean validateTimestampWithFrame(Frame frame, java.util.List<String> errors, b
165165
return false;
166166
}
167167

168-
private boolean hasFractionalComponent(Number value) {
169-
return switch (value) {
170-
case null -> false;
171-
case Double d -> d != Math.floor(d);
172-
case Float f -> f != Math.floor(f);
173-
case java.math.BigDecimal bd -> bd.remainder(java.math.BigDecimal.ONE).signum() != 0;
174-
default ->
175-
// Long, Integer, Short, Byte are always integers
176-
false;
177-
};
178-
}
179-
180168
boolean validateIntegerWithFrame(Frame frame, String type, java.util.List<String> errors, boolean verboseErrors) {
181169
JsonValue instance = frame.instance();
182170
if (instance instanceof JsonNumber num) {

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

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import net.jqwik.api.*;
55
import org.junit.jupiter.api.Assertions;
66

7-
import java.math.BigDecimal;
87
import java.util.*;
98
import java.util.stream.Collectors;
109

@@ -14,7 +13,6 @@
1413
/// Generates comprehensive schema/document permutations to validate RFC 8927 compliance
1514
class JtdPropertyTest extends JtdTestBase {
1615

17-
private static final int MAX_DEPTH = 3;
1816
private static final List<String> PROPERTY_NAMES = List.of("alpha", "beta", "gamma", "delta", "epsilon");
1917
private static final List<List<String>> PROPERTY_PAIRS = List.of(List.of("alpha", "beta"), List.of("alpha", "gamma"), List.of("beta", "delta"), List.of("gamma", "epsilon"));
2018
private static final List<String> DISCRIMINATOR_VALUES = List.of("type1", "type2", "type3");
@@ -313,50 +311,6 @@ private static Arbitrary<JtdTestSchema> valuesSchemaArbitrary(int depth) {
313311
return jtdSchemaArbitrary(depth - 1).map(ValuesSchema::new);
314312
}
315313

316-
/// Creates simple PropertiesSchema instances for discriminator mappings without recursion
317-
/// This prevents stack overflow while ensuring RFC 8927 compliance
318-
private static Arbitrary<JtdTestSchema> simplePropertiesSchemaArbitrary() {
319-
// Create primitive schemas that don't recurse
320-
final var primitiveSchemas = Arbitraries.of(new EmptySchema(), new TypeSchema("boolean"), new TypeSchema("string"), new TypeSchema("int32"), new EnumSchema(List.of("red", "green", "blue")));
321-
322-
// ======================== CHANGE: USE NON-DISCRIMINATOR PROPERTY NAMES ========================
323-
// RFC 8927 §2.2.8: Discriminator mapping schemas cannot define the discriminator key
324-
// Use property names that won't conflict with discriminator keys
325-
final var safePropertyNames = List.of("beta", "gamma", "delta", "epsilon"); // Exclude "alpha"
326-
final var safePropertyPairs = List.of(
327-
List.of("beta", "gamma"),
328-
List.of("gamma", "delta"),
329-
List.of("delta", "epsilon")
330-
);
331-
// ==================== END CHANGE: USE NON-DISCRIMINATOR PROPERTY NAMES ====================
332-
333-
return Arbitraries.oneOf(
334-
// Empty properties schema
335-
Arbitraries.of(new PropertiesSchema(Map.of(), Map.of(), false)),
336-
337-
// Single required property with primitive schema
338-
Combinators.combine(Arbitraries.of(safePropertyNames), primitiveSchemas).as((name, schema) -> {
339-
Assertions.assertNotNull(name);
340-
Assertions.assertNotNull(schema);
341-
return new PropertiesSchema(Map.of(name, schema), Map.of(), false);
342-
}),
343-
344-
// Single optional property with primitive schema
345-
Combinators.combine(Arbitraries.of(safePropertyNames), primitiveSchemas).as((name, schema) -> {
346-
Assertions.assertNotNull(name);
347-
Assertions.assertNotNull(schema);
348-
return new PropertiesSchema(Map.of(), Map.of(name, schema), false);
349-
}),
350-
351-
// Required + optional property with primitive schemas
352-
Combinators.combine(Arbitraries.of(safePropertyPairs), primitiveSchemas, primitiveSchemas).as((names, requiredSchema, optionalSchema) -> {
353-
Assertions.assertNotNull(names);
354-
Assertions.assertNotNull(requiredSchema);
355-
Assertions.assertNotNull(optionalSchema);
356-
return new PropertiesSchema(Map.of(names.getFirst(), requiredSchema), Map.of(names.get(1), optionalSchema), false);
357-
}));
358-
}
359-
360314
private static Arbitrary<JtdTestSchema> discriminatorSchemaArbitrary() {
361315

362316
return Combinators.combine(Arbitraries.of(PROPERTY_NAMES), Arbitraries.of(DISCRIMINATOR_VALUES), Arbitraries.of(DISCRIMINATOR_VALUES)).as((discriminatorKey, value1, value2) -> {
@@ -398,7 +352,7 @@ private static Arbitrary<JtdTestSchema> propertiesSchemaForDiscriminatorMapping(
398352
.flatMap(name1 -> effectivePropertyNames.stream()
399353
.filter(name2 -> !name1.equals(name2))
400354
.map(name2 -> List.of(name1, name2)))
401-
.filter(pair -> !pair.get(0).equals(discriminatorKey) && !pair.get(1).equals(discriminatorKey))
355+
.filter(pair -> !pair.getFirst().equals(discriminatorKey) && !pair.get(1).equals(discriminatorKey))
402356
.toList();
403357

404358
return Arbitraries.oneOf(
@@ -431,12 +385,12 @@ private static Arbitrary<JtdTestSchema> nullableSchemaArbitrary(int depth) {
431385

432386
@Provide
433387
Arbitrary<JtdTestSchema> jtdSchemas() {
434-
return jtdSchemaArbitrary(MAX_DEPTH);
388+
return jtdSchemaArbitrary(3);
435389
}
436390

437391
@Property(generation = GenerationMode.AUTO)
438392
void exhaustiveJtdValidation(@ForAll("jtdSchemas") JtdPropertyTest.JtdTestSchema schema) {
439-
LOG.finer(() -> "Executing exhaustiveJtdValidation property test");
393+
LOG.finer(() -> "Executing exhaustiveValidation property test");
440394

441395
final var schemaDescription = describeJtdSchema(schema);
442396

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public void testRefSchemaRecursiveBad() {
443443
}
444444

445445
/// Micro test to debug int32 validation with decimal values
446-
/// Should reject non-integer values like 3.14 for int32 type
446+
/// Should reject noninteger values like 3.14 for int32 type
447447
@Test
448448
public void testInt32RejectsDecimal() {
449449
JsonValue schema = Json.parse("{\"type\": \"int32\"}");
@@ -627,7 +627,7 @@ public void testDiscriminatorInElementsSchema() {
627627
/// Test case from JtdExhaustiveTest property test failure
628628
/// Nested elements containing properties schemas should reject additional properties
629629
/// Schema: {"elements":{"elements":{"properties":{}}}}
630-
/// Document: [[{},{},[{},{extraProperty":"extra-value"}]]
630+
/// Document: [[{},{},[{},{extraProperty":"extra-value"}]
631631
/// This should fail validation but currently passes incorrectly
632632
@Test
633633
public void testNestedElementsPropertiesRejectsAdditionalProperties() {
@@ -901,7 +901,7 @@ public void testInt8RangeValidationWithDoubleValues() {
901901

902902
LOG.fine(() -> "Testing int8 range with Double value: " + outOfRange +
903903
" (JsonNumber.toLong(): " +
904-
((JsonNumber)outOfRange).toLong() + ")");
904+
outOfRange.toLong() + ")");
905905

906906
// This should fail but currently passes due to the bug
907907
assertThat(result.isValid())
@@ -1043,7 +1043,7 @@ public void testJsonNumberToNumberReturnsDouble() {
10431043

10441044
// Verify that JsonNumber works properly for typical JSON numbers
10451045
assertThat(numberValue).isInstanceOf(JsonNumber.class);
1046-
long longValue = ((JsonNumber) numberValue).toLong();
1046+
long longValue = numberValue.toLong();
10471047

10481048
LOG.info(() -> "JsonNumber.toLong() returns: " + longValue +
10491049
" for value: " + numberValue);

0 commit comments

Comments
 (0)