Skip to content

Commit c26e73c

Browse files
committed
fix: escape keys in JSON paths and add test for patternProperties
closes #22
1 parent 1b2ee8f commit c26e73c

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ protected Resolver getResolver(RefFormat refFormat, URI currentURL) {
418418
}
419419

420420
private String jsonPath(String[] paths) {
421-
return "$" + Arrays.stream(paths).map(path -> "['" + path + "']").collect(Collectors.joining());
421+
return "$" + Arrays.stream(paths).map(path -> "['" + escapeKey(path) + "']").collect(Collectors.joining());
422+
}
423+
424+
private String escapeKey(String key) {
425+
return key.replace("\\", "\\\\").replace("'", "\\'");
422426
}
423427

424428
private String jsonPointer(String[] paths) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.zenwave360.jsonrefparser;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.util.Map;
10+
11+
public class GH22Test {
12+
13+
private static final ObjectMapper mapper = new ObjectMapper();
14+
15+
@Test
16+
public void testLocalSchemaWithPatternProperties() throws IOException {
17+
File file = new File("src/test/resources/schema-with-pattern-properties.json");
18+
final var parser = new $RefParser(file);
19+
final var schema = parser
20+
.parse()
21+
.dereference()
22+
.mergeAllOf()
23+
.getRefs().schema();
24+
25+
Assert.assertNotNull(schema);
26+
27+
// Verify the schema has the expected structure
28+
Assert.assertTrue(schema.containsKey("properties"));
29+
Assert.assertTrue(schema.containsKey("patternProperties"));
30+
Assert.assertTrue(schema.containsKey("definitions"));
31+
32+
// Verify patternProperties contains the expected pattern
33+
Map<String, Object> patternProperties = (Map<String, Object>) schema.get("patternProperties");
34+
Assert.assertTrue(patternProperties.containsKey("^x-[\\w\\d\\.\\x2d_]+$"));
35+
36+
// Verify the pattern property has been dereferenced (should contain the extension definition)
37+
Map<String, Object> extensionPattern = (Map<String, Object>) patternProperties.get("^x-[\\w\\d\\.\\x2d_]+$");
38+
Assert.assertNotNull(extensionPattern);
39+
Assert.assertEquals("Any property starting with x- is valid.", extensionPattern.get("description"));
40+
41+
// Verify nested patternProperties in info definition
42+
Map<String, Object> definitions = (Map<String, Object>) schema.get("definitions");
43+
Map<String, Object> infoDefinition = (Map<String, Object>) definitions.get("info");
44+
Map<String, Object> infoPatternProperties = (Map<String, Object>) infoDefinition.get("patternProperties");
45+
Assert.assertTrue(infoPatternProperties.containsKey("^x-[\\w\\d\\.\\x2d_]+$"));
46+
47+
// Print the schema for debugging
48+
// System.out.println("Dereferenced schema:");
49+
// System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
50+
}
51+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"$id": "http://example.com/schemas/test-schema.json",
3+
"$schema": "http://json-schema.org/draft-07/schema",
4+
"title": "Test Schema with Pattern Properties",
5+
"type": "object",
6+
"required": [
7+
"name",
8+
"info"
9+
],
10+
"additionalProperties": false,
11+
"patternProperties": {
12+
"^x-[\\w\\d\\.\\x2d_]+$": {
13+
"$ref": "#/definitions/extension"
14+
}
15+
},
16+
"properties": {
17+
"name": {
18+
"type": "string",
19+
"description": "The name of the object."
20+
},
21+
"info": {
22+
"$ref": "#/definitions/info"
23+
},
24+
"metadata": {
25+
"$ref": "#/definitions/metadata"
26+
}
27+
},
28+
"definitions": {
29+
"extension": {
30+
"description": "Any property starting with x- is valid.",
31+
"additionalProperties": true,
32+
"additionalItems": true
33+
},
34+
"info": {
35+
"type": "object",
36+
"required": [
37+
"version",
38+
"title"
39+
],
40+
"additionalProperties": false,
41+
"patternProperties": {
42+
"^x-[\\w\\d\\.\\x2d_]+$": {
43+
"$ref": "#/definitions/extension"
44+
}
45+
},
46+
"properties": {
47+
"title": {
48+
"type": "string",
49+
"description": "A unique and precise title."
50+
},
51+
"version": {
52+
"type": "string",
53+
"description": "A semantic version number."
54+
},
55+
"description": {
56+
"type": "string",
57+
"description": "A longer description."
58+
}
59+
}
60+
},
61+
"metadata": {
62+
"type": "object",
63+
"additionalProperties": false,
64+
"patternProperties": {
65+
"^x-[\\w\\d\\.\\x2d_]+$": {
66+
"$ref": "#/definitions/extension"
67+
}
68+
},
69+
"properties": {
70+
"created": {
71+
"type": "string",
72+
"format": "date-time"
73+
},
74+
"updated": {
75+
"type": "string",
76+
"format": "date-time"
77+
}
78+
}
79+
}
80+
}
81+
}
82+

0 commit comments

Comments
 (0)