Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix schema location for escaped json pointer #1038

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/main/java/com/networknt/schema/SchemaLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.networknt.schema;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
Expand Down Expand Up @@ -230,7 +233,21 @@ public static JsonNodePath of(String fragmentString) {
if (index != -1) {
fragment = fragment.append(index);
} else {
fragment = fragment.append(fragmentPart.toString());
String fragmentPartString = fragmentPart.toString();
if (PathType.JSON_POINTER.equals(fragment.getPathType())) {
if (fragmentPartString.contains("~")) {
fragmentPartString = fragmentPartString.replace("~1", "/");
fragmentPartString = fragmentPartString.replace("~0", "~");
}
if (fragmentPartString.contains("%")) {
try {
fragmentPartString = URLDecoder.decode(fragmentPartString, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
// Do nothing
}
}
}
fragment = fragment.append(fragmentPartString);
}
}
if (index == -1 && fragmentString.endsWith("/")) {
Expand Down
20 changes: 1 addition & 19 deletions src/main/java/com/networknt/schema/utils/JsonNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
package com.networknt.schema.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.JsonNodePath;

Expand Down Expand Up @@ -61,21 +57,7 @@ public static <T extends JsonNode> T get(JsonNode node, Object propertyOrIndex)
if (propertyOrIndex instanceof Number) {
value = node.get(((Number) propertyOrIndex).intValue());
} else {
// In the case of string this represents an escaped json pointer and thus does not reflect the property directly
String unescaped = propertyOrIndex.toString();
if (unescaped.contains("~")) {
unescaped = unescaped.replace("~1", "/");
unescaped = unescaped.replace("~0", "~");
}
if (unescaped.contains("%")) {
try {
unescaped = URLDecoder.decode(unescaped, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
// Do nothing
}
}

value = node.get(unescaped);
value = node.get(propertyOrIndex.toString());
}
return (T) value;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/networknt/schema/SchemaLocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import static org.junit.jupiter.api.Assertions.*;

import java.util.Set;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

class SchemaLocationTest {

@Test
Expand Down Expand Up @@ -173,6 +177,30 @@ void documentFragment() {
assertTrue(SchemaLocation.Fragment.isDocumentFragment("#"));
}

@Test
void shouldLoadEscapedFragment() {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
JsonSchema schema = factory.getSchema(SchemaLocation
.of("classpath:schema/example-escaped.yaml#/paths/~1users/post/requestBody/application~1json/schema"));
Set<ValidationMessage> result = schema.validate("1", InputFormat.JSON);
assertFalse(result.isEmpty());
result = schema.validate("{}", InputFormat.JSON);
assertTrue(result.isEmpty());
}

@Test
void escapedJsonPointerFragment() {
JsonNodePath fragment = SchemaLocation.Fragment.of("/paths/~1users/post/requestBody/application~1json/schema");
assertEquals("/paths/~1users/post/requestBody/application~1json/schema", fragment.toString());
assertEquals(6, fragment.getNameCount());
assertEquals("paths", fragment.getName(0));
assertEquals("/users", fragment.getName(1));
assertEquals("post", fragment.getName(2));
assertEquals("requestBody", fragment.getName(3));
assertEquals("application/json", fragment.getName(4));
assertEquals("schema", fragment.getName(5));
}

@Test
void ofNull() {
assertNull(SchemaLocation.of(null));
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/schema/example-escaped.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
paths:
/users:
post:
requestBody:
application/json:
schema:
anyOf:
- type: object
- type: string