Skip to content

Commit

Permalink
Fix getSchema() anchor fragment lookup. (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
crutcher authored Jan 24, 2024
1 parent 44b1ec5 commit f3825f3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/JsonSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ protected JsonSchema getMappedSchema(final URI schemaUri, SchemaValidatorsConfig
URI documentUri = "".equals(schemaUri.getSchemeSpecificPart()) ? new URI(schemaUri.getScheme(), schemaUri.getUserInfo(), schemaUri.getHost(), schemaUri.getPort(), schemaUri.getPath(), schemaUri.getQuery(), null) : new URI(schemaUri.getScheme(), schemaUri.getSchemeSpecificPart(), null);
SchemaLocation documentLocation = new SchemaLocation(schemaLocation.getAbsoluteIri());
JsonSchema document = doCreate(validationContext, documentLocation, evaluationPath, documentUri, schemaNode, null, false);
JsonNode subSchemaNode = document.getRefSchemaNode(schemaLocation.getFragment().toString());
JsonNode subSchemaNode = document.getRefSchemaNode("#" + schemaLocation.getFragment().toString());
if (subSchemaNode != null) {
jsonSchema = doCreate(validationContext, schemaLocation, evaluationPath, mappedUri, subSchemaNode, document, false);
} else {
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/networknt/schema/Issue928Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.uri.URITranslator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.net.URI;

public class Issue928Test {
private final ObjectMapper mapper = new ObjectMapper();

private JsonSchemaFactory factoryFor(SpecVersion.VersionFlag version) {
return JsonSchemaFactory
.builder(JsonSchemaFactory.getInstance(version))
.objectMapper(mapper)
.addUriTranslator(
URITranslator.prefix("https://example.org", "resource:")
)
.build();
}

@Test
public void test_07() {
test_spec(SpecVersion.VersionFlag.V7);
}

@Test
public void test_201909() {
test_spec(SpecVersion.VersionFlag.V201909);
}

@Test
public void test_202012() {
test_spec(SpecVersion.VersionFlag.V202012);
}

public void test_spec(SpecVersion.VersionFlag specVersion) {
JsonSchemaFactory schemaFactory = factoryFor(specVersion);

String versionId = specVersion.getId();
String versionStr = versionId.substring(versionId.indexOf("draft") + 6, versionId.indexOf("/schema"));

String baseUrl = String.format("https://example.org/schema/issue928-v%s.json", versionStr);
System.out.println("baseUrl: " + baseUrl);

JsonSchema byPointer = schemaFactory.getSchema(
URI.create(baseUrl + "#/definitions/example"));

Assertions.assertEquals(byPointer.validate(mapper.valueToTree("A")).size(), 0);
Assertions.assertEquals(byPointer.validate(mapper.valueToTree("Z")).size(), 1);

JsonSchema byAnchor = schemaFactory.getSchema(
URI.create(baseUrl + "#example"));

Assertions.assertEquals(
byPointer.getSchemaNode(),
byAnchor.getSchemaNode());

Assertions.assertEquals(byAnchor.validate(mapper.valueToTree("A")).size(), 0);
Assertions.assertEquals(byAnchor.validate(mapper.valueToTree("Z")).size(), 1);
}
}
14 changes: 14 additions & 0 deletions src/test/resources/schema/issue928-v07.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"example": {
"$id": "#example",
"type": "string",
"enum": [
"A", "B"
]
}
}
}
14 changes: 14 additions & 0 deletions src/test/resources/schema/issue928-v2019-09.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"type": "object",
"definitions": {
"example": {
"$anchor": "example",
"type": "string",
"enum": [
"A", "B"
]
}
}
}
14 changes: 14 additions & 0 deletions src/test/resources/schema/issue928-v2020-12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"type": "object",
"definitions": {
"example": {
"$anchor": "example",
"type": "string",
"enum": [
"A", "B"
]
}
}
}

0 comments on commit f3825f3

Please sign in to comment.