Skip to content

Commit 6d061b3

Browse files
committed
fixes
1 parent f92da87 commit 6d061b3

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

json-java21-schema/src/main/java/io/github/simbo1905/json/schema/JsonSchema.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,9 @@ static boolean scheduleRemoteIfUnseen(Deque<java.net.URI> workStack,
663663

664664
// Detect remote cycles by walking parent chain
665665
if (formsRemoteCycle(parentMap, currentDocUri, targetDocUri)) {
666-
String cycleMessage = "ERROR: CYCLE: remote $ref cycle current=" + currentDocUri + ", target=" + targetDocUri;
666+
String cycleMessage = "ERROR: CYCLE: remote $ref cycle detected current=" + currentDocUri + ", target=" + targetDocUri;
667667
LOG.severe(() -> cycleMessage);
668-
throw new IllegalArgumentException(cycleMessage);
668+
throw new IllegalStateException(cycleMessage);
669669
}
670670

671671
// Check if already built or already in work stack
@@ -1995,9 +1995,9 @@ private static JsonSchema compileInternalWithContext(Session session, JsonValue
19951995
LOG.fine(() -> "Remote ref scheduling from docUri=" + docUri + " to target=" + targetDocUri);
19961996
LOG.finest(() -> "Remote ref parentMap before cycle check: " + session.parentMap);
19971997
if (formsRemoteCycle(session.parentMap, docUri, targetDocUri)) {
1998-
String cycleMessage = "ERROR: CYCLE: remote $ref cycle current=" + docUri + ", target=" + targetDocUri;
1998+
String cycleMessage = "ERROR: CYCLE: remote $ref cycle detected current=" + docUri + ", target=" + targetDocUri;
19991999
LOG.severe(() -> cycleMessage);
2000-
throw new IllegalArgumentException(cycleMessage);
2000+
throw new IllegalStateException(cycleMessage);
20012001
}
20022002
boolean alreadySeen = seenUris.contains(targetDocUri);
20032003
LOG.finest(() -> "Remote ref alreadySeen=" + alreadySeen + " for target=" + targetDocUri);

json-java21-schema/src/test/java/io/github/simbo1905/json/schema/JsonSchemaRemoteRefTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void detects_cross_document_cycle() {
303303
"""),
304304
JsonSchema.Options.DEFAULT,
305305
options
306-
)).isInstanceOf(IllegalArgumentException.class)
306+
)).isInstanceOf(IllegalStateException.class)
307307
.hasMessageContaining("ERROR: CYCLE: remote $ref cycle");
308308
assertThat(logs.lines().stream().anyMatch(line -> line.startsWith("ERROR: CYCLE:"))).isTrue();
309309
}

json-java21-schema/src/test/java/io/github/simbo1905/json/schema/JsonSchemaRemoteServerRefTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ void resolves_pointer_inside_remote_doc_via_http() {
2525
}
2626

2727
@Test
28-
void remote_cycle_handles_gracefully() {
28+
void remote_cycle_detected_and_throws() {
2929
var policy = JsonSchema.FetchPolicy.defaults().withAllowedSchemes(Set.of("http","https"));
3030
var options = JsonSchema.CompileOptions.remoteDefaults(new VirtualThreadHttpFetcher()).withFetchPolicy(policy);
3131

32-
// Compilation should succeed despite the cycle
33-
var compiled = JsonSchema.compile(Json.parse("{\"$ref\":\"" + SERVER.url("/cycle1.json") + "#\"}"), JsonSchema.Options.DEFAULT, options);
34-
35-
// Validation should succeed by gracefully handling the cycle
36-
var result = compiled.validate(Json.parse("\"test\""));
37-
assertThat(result.valid()).isTrue();
32+
// Cycles should be detected and throw an exception regardless of scheme
33+
assertThatThrownBy(() -> JsonSchema.compile(
34+
Json.parse("{\"$ref\":\"" + SERVER.url("/cycle1.json") + "#\"}"),
35+
JsonSchema.Options.DEFAULT,
36+
options
37+
)).isInstanceOf(IllegalStateException.class)
38+
.hasMessageContaining("ERROR: CYCLE: remote $ref cycle");
3839
}
3940
}
4041

0 commit comments

Comments
 (0)