-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix getSchema() anchor fragment lookup. (#930)
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |