Skip to content

Commit 6145a0b

Browse files
committed
fix: $RefParser.java replaceWith$Ref when it contains other properties
closes #18
1 parent 6451f1f commit 6145a0b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,11 @@ private void dereference(ExtendedJsonContext jsonContext, Object value, String[]
319319

320320
private void replaceWith$Ref(ExtendedJsonContext jsonContext, String jsonPath, Object resolved) {
321321
Map<String, Object> original = jsonContext.read(jsonPath);
322-
if (resolved instanceof Map) {
323-
for (Map.Entry<String, Object> entry : (original).entrySet()) {
324-
if(!entry.getKey().equals("$ref")) {
325-
((Map) resolved).put(entry.getKey(), entry.getValue());
326-
}
327-
}
328-
}
329-
jsonContext.set(jsonPath, resolved);
322+
Map<String, Object> replacement = new LinkedHashMap<>();
323+
replacement.putAll(original);
324+
replacement.remove("$ref");
325+
replacement.putAll((Map) resolved);
326+
jsonContext.set(jsonPath, replacement);
330327
}
331328

332329
private Object dereference($Ref $ref, ExtendedJsonContext jsonContext, URI currentFileURL) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.zenwave360.jsonrefparser;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.io.File;
7+
8+
public class GH18 {
9+
10+
@Test
11+
public void test() throws Exception {
12+
$RefParser refParser = new $RefParser(new File("src/test/resources/GH-18.json"));
13+
refParser.withOptions(new $RefParserOptions().withOnCircular($RefParserOptions.OnCircular.SKIP));
14+
$Refs refs = refParser.parse().dereference().getRefs();
15+
String schema = refs.schema().toString();
16+
Assert.assertTrue(schema.contains("lorem={title=lorem"));
17+
Assert.assertTrue(schema.contains("ipsum={title=ipsum"));
18+
System.out.println(refs.schema());
19+
}
20+
}

src/test/resources/GH-18.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "myDummySchema",
4+
"properties": {
5+
"lorem": {
6+
"title": "lorem",
7+
"$ref": "https://json.schemastore.org/base.json#/definitions/nullable-boolean"
8+
},
9+
"ipsum": {
10+
"title": "ipsum",
11+
"$ref": "https://json.schemastore.org/base.json#/definitions/nullable-boolean"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)