Skip to content

Commit

Permalink
Bug fix for JSON Pointer parsing (networknt#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
costas80 committed May 15, 2023
1 parent 2143b54 commit c0ca7d2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/PathType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public enum PathType {
t = t.replace("'", "\\'");
}

return "['" + token + "']";
return "['" + t + "']";
}, (index) -> "[" + index + "]"),

/**
Expand Down
35 changes: 34 additions & 1 deletion src/test/java/com/networknt/schema/Issue687Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static Stream<Arguments> appendTokens() {
Arguments.of(PathType.JSON_PATH, "$.foo", "b.ar", "$.foo['b.ar']"),
Arguments.of(PathType.JSON_PATH, "$.foo", "b~ar", "$.foo['b~ar']"),
Arguments.of(PathType.JSON_PATH, "$.foo", "b/ar", "$.foo['b/ar']"),
Arguments.of(PathType.JSON_PATH, "$", "'", "$['\'']"),
Arguments.of(PathType.JSON_PATH, "$", "'", "$['\\'']"),
Arguments.of(PathType.JSON_PATH, "$", "b'ar", "$['b\\'ar']"),
Arguments.of(PathType.JSON_POINTER, "/foo", "bar", "/foo/bar"),
Arguments.of(PathType.JSON_POINTER, "/foo", "b.ar", "/foo/b.ar"),
Arguments.of(PathType.JSON_POINTER, "/foo", "b~ar", "/foo/b~0ar"),
Expand Down Expand Up @@ -122,4 +123,36 @@ void testDoubleQuotes() throws JsonProcessingException {
assertEquals("$['\"']", validationMessages.iterator().next().getPath());
}

@Test
void testSingleQuotes() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
schemaValidatorsConfig.setPathType(PathType.JSON_PATH);
/*
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"'": {
"type": "boolean"
}
}
}
*/
JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)
.getSchema(mapper.readTree("{\n" +
" \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\n" +
" \"type\": \"object\",\n" +
" \"properties\": {\n" +
" \"'\": {\n" +
" \"type\": \"boolean\"\n" +
" }\n" +
" }\n" +
"}"), schemaValidatorsConfig);
// {"\"": 1}
Set<ValidationMessage> validationMessages = schema.validate(mapper.readTree("{\"'\": 1}"));
assertEquals(1, validationMessages.size());
assertEquals("$['\\'']", validationMessages.iterator().next().getPath());
}

}

0 comments on commit c0ca7d2

Please sign in to comment.