Skip to content

Commit 54db271

Browse files
committed
treats $ref according to JSON Schema Draft 2019-09 and later
This means you can include other keywords alongside `$ref, and they won’t be ignored — they’ll combine with the referenced schema. fixes #11
1 parent 61240b0 commit 54db271

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/main/java/io/zenwave360/jsonrefparser/$RefParser.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ private void dereference(ExtendedJsonContext jsonContext, Object value, String[]
293293
log.trace("{}setting resolved value at {} {}", indent(), innerJsonPath, currentFileURL);
294294
resolved = dereference($ref, jsonContext, currentFileURL);
295295
this.refs.saveOriginalRef($ref, resolved);
296-
jsonContext.set(innerJsonPath, resolved);
296+
// jsonContext.set(innerJsonPath, resolved);
297+
replaceWith$Ref(jsonContext, innerJsonPath, resolved);
297298
}catch (Exception e){
298299
log.error("Error setting jsonPath: {} in {}", innerJsonPath, currentFileURL, e);
299300
throw e;
@@ -312,6 +313,18 @@ private void dereference(ExtendedJsonContext jsonContext, Object value, String[]
312313
}
313314
}
314315

316+
private void replaceWith$Ref(ExtendedJsonContext jsonContext, String jsonPath, Object resolved) {
317+
Map<String, Object> original = jsonContext.read(jsonPath);
318+
if (resolved instanceof Map) {
319+
for (Map.Entry<String, Object> entry : (original).entrySet()) {
320+
if(!entry.getKey().equals("$ref")) {
321+
((Map) resolved).put(entry.getKey(), entry.getValue());
322+
}
323+
}
324+
}
325+
jsonContext.set(jsonPath, resolved);
326+
}
327+
315328
private Object dereference($Ref $ref, ExtendedJsonContext jsonContext, URI currentFileURL) {
316329
this.refs.addRef($ref.getRef());
317330
// resolve external file
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.zenwave360.jsonrefparser;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.util.Map;
7+
8+
public class Issue11Test {
9+
10+
@Test
11+
public void testIssue11() throws IOException {
12+
String schema = "{\n" +
13+
" \"$schema\" : \"http://json-schema.org/draft-07/schema#\",\n" +
14+
" \"type\" : \"object\",\n" +
15+
" \"properties\" : {\n" +
16+
" \"referingProperty\" : {\n" +
17+
" \"$ref\" : \"https://json.schemastore.org/base.json#/definitions/nullable-boolean\",\n" +
18+
" \"title\" : \"referingProperty\",\n" +
19+
" \"description\" : \"A property that is refering to the base.json of json.schemastore.org\"\n" +
20+
" }\n" +
21+
" }\n" +
22+
" }";
23+
24+
$RefParser parser = new $RefParser(schema)
25+
.withOptions(new $RefParserOptions().withOnCircular($RefParserOptions.OnCircular.SKIP));
26+
$Refs refs = parser.parse().dereference().getRefs();
27+
Map<String, Object> properties = (Map<String, Object>) refs.get("properties");
28+
Map<String, Object>referingProperty = (Map<String, Object>) properties.get("referingProperty");
29+
for(var value : referingProperty.entrySet()){
30+
System.out.println(value);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)