Skip to content

Commit 36f2fef

Browse files
test: re-enable and strengthen recursive $ref SelfRefTest (#1258)
SelfRefTest has been @disabled since 2016 with the note that a recursive $ref caused a StackOverflowError. That has long since been fixed, but the test stayed disabled and only printed to stdout, so the recursive-$ref path had no coverage. This re-enables it and replaces the println with real assertions: the self-referential "tree" schema loads without StackOverflow, a valid nested tree validates with no errors, and a nested branch missing the required "value" is reported (proving validation follows the recursive $ref). Verified locally with mvn -Dtest=SelfRefTest test.
1 parent f75875e commit 36f2fef

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,49 @@
1616

1717
package com.networknt.schema;
1818

19-
import org.junit.jupiter.api.Disabled;
19+
import org.junit.jupiter.api.Assertions;
2020
import org.junit.jupiter.api.Test;
2121

22+
import java.util.List;
23+
24+
import tools.jackson.databind.JsonNode;
25+
2226
/**
2327
* Created by stevehu on 2016-12-20.
28+
*
29+
* Regression coverage for a recursive {@code $ref} (a self-referential "tree"
30+
* schema). This historically caused a {@code StackOverflowError} while loading
31+
* the schema; the test guards against that and additionally verifies that
32+
* validation actually recurses into nested branches.
2433
*/
2534
class SelfRefTest extends BaseJsonSchemaValidatorTest {
26-
@Disabled("This test currently is failing because of a StackOverflow caused by a recursive $ref.")
27-
@Test()
28-
void testSelfRef() throws Exception {
29-
Schema node = getJsonSchemaFromClasspath("selfRef.json");
30-
System.out.println("node = " + node);
35+
36+
@Test
37+
void recursiveRefLoadsWithoutStackOverflow() {
38+
Schema schema = getJsonSchemaFromClasspath("selfRef.json");
39+
Assertions.assertNotNull(schema);
40+
}
41+
42+
@Test
43+
void recursiveRefValidatesNestedBranches() throws Exception {
44+
Schema schema = getJsonSchemaFromClasspath("selfRef.json");
45+
46+
// A well-formed, deeply nested tree: every node carries the required
47+
// "value", so recursing through "branches" should yield no errors.
48+
JsonNode valid = getJsonNodeFromStringContent(
49+
"{\"name\":\"root\",\"tree\":{\"value\":\"a\",\"branches\":["
50+
+ "{\"value\":\"b\",\"branches\":[{\"value\":\"c\"}]}]}}");
51+
List<Error> validErrors = schema.validate(valid);
52+
Assertions.assertTrue(validErrors.isEmpty(),
53+
"valid recursive tree should not produce errors, got: " + validErrors);
54+
55+
// A nested branch missing the required "value" must be reported, which
56+
// only happens if the validator follows the recursive $ref downward.
57+
JsonNode invalid = getJsonNodeFromStringContent(
58+
"{\"name\":\"root\",\"tree\":{\"value\":\"a\",\"branches\":["
59+
+ "{\"branches\":[{\"value\":\"c\"}]}]}}");
60+
List<Error> invalidErrors = schema.validate(invalid);
61+
Assertions.assertFalse(invalidErrors.isEmpty(),
62+
"missing required 'value' in a nested branch should be reported");
3163
}
32-
}
64+
}

0 commit comments

Comments
 (0)