networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
800 stars 320 forks source link

Impossible to construct a SchemaLocation containing a json pointer with an escaped fragment #1037

Closed Mahoney closed 2 months ago

Mahoney commented 2 months ago

It's common to have json pointers including a / in a segment in OpenAPI, because OpenAPI uses both paths and content types as keys.

/ is escaped as ~1 in Json Pointer - so for instance in the json pointer /paths/~1users/post/requestBody/application~1json/schema the unescaped segments are paths, /users, post, requestBody, application/json and schema.

It is not possible to construct a SchemaLocation from this valid json pointer.

The following test fails:

@Test
void handlesEscapedJsonPointer() {
    assertEquals("#/application~1json", SchemaLocation.of("#/application~1json").toString());
}
Expected :#/application~1json
Actual   :#/application~01json

Because the following test fails:

@Test
void handlesEscapedJsonPointer() {
    assertEquals("/application~1json", SchemaLocation.Fragment.of("/application~1json").toString());
}
Expected :/application~1json
Actual   :/application~01json
Mahoney commented 2 months ago

It's because JsonNodePath.toString() calls PathType.JSON_POINTER.append(String currentPath, String child) which assumes child is unescaped and escapes it.