Skip to content
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
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"
]
}
}
}