-
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.
665: Can't load JSON schemas with URN value in id field (#906)
* 665: Can't load JSON schemas with URN value in id field * Better handling of URNs
- Loading branch information
1 parent
97b4cba
commit b138dc6
Showing
11 changed files
with
153 additions
and
4 deletions.
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
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
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
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,30 @@ | ||
package com.networknt.schema.uri; | ||
|
||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
/** | ||
* A URIFactory that handles "urn" scheme of {@link URI}s. | ||
*/ | ||
public final class URNURIFactory implements URIFactory { | ||
|
||
public static final String SCHEME = "urn"; | ||
|
||
@Override | ||
public URI create(final String uri) { | ||
try { | ||
return URI.create(uri); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException("Unable to create URI.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public URI create(final URI baseURI, final String segment) { | ||
String urnPart = baseURI.getRawSchemeSpecificPart(); | ||
int pos = urnPart.indexOf(':'); | ||
String namespace = pos < 0 ? urnPart : urnPart.substring(0, pos); | ||
return URI.create(SCHEME + ":" + namespace + ":" + segment); | ||
} | ||
} |
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,49 @@ | ||
package com.networknt.schema; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
public class Issue665Test extends BaseJsonSchemaValidatorTest { | ||
|
||
@Test | ||
void testUrnUriAsLocalRef() throws IOException { | ||
JsonSchema schema = getJsonSchemaFromClasspath("draft7/urn/issue665.json", SpecVersion.VersionFlag.V7); | ||
Assertions.assertNotNull(schema); | ||
Assertions.assertDoesNotThrow(schema::initializeValidators); | ||
Set<ValidationMessage> messages = schema.validate(getJsonNodeFromStringContent( | ||
"{\"myData\": {\"value\": \"hello\"}}")); | ||
Assertions.assertEquals(messages, Collections.emptySet()); | ||
} | ||
|
||
@Test | ||
void testUrnUriAsLocalRef_ExternalURN() { | ||
JsonSchemaFactory factory = JsonSchemaFactory | ||
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)) | ||
.uriFetcher(uri -> uri.equals(URI.create("urn:data")) | ||
? Thread.currentThread().getContextClassLoader() | ||
.getResourceAsStream("draft7/urn/issue665_external_urn_subschema.json") | ||
: null, | ||
"urn") | ||
.build(); | ||
|
||
try (InputStream is = Thread.currentThread().getContextClassLoader() | ||
.getResourceAsStream("draft7/urn/issue665_external_urn_ref.json")) { | ||
JsonSchema schema = factory.getSchema(is); | ||
Assertions.assertNotNull(schema); | ||
Assertions.assertDoesNotThrow(schema::initializeValidators); | ||
Set<ValidationMessage> messages = schema.validate(getJsonNodeFromStringContent( | ||
"{\"myData\": {\"value\": \"hello\"}}")); | ||
Assertions.assertEquals(messages, Collections.emptySet()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
} |
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,26 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"required": [ | ||
"myData" | ||
], | ||
"properties": { | ||
"myData": { | ||
"$ref": "urn:data" | ||
} | ||
}, | ||
"definitions": { | ||
"data": { | ||
"$id": "urn:data", | ||
"type": "object", | ||
"required": [ | ||
"value" | ||
], | ||
"properties": { | ||
"value": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/resources/draft7/urn/issue665_external_urn_ref.json
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,12 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"required": [ | ||
"myData" | ||
], | ||
"properties": { | ||
"myData": { | ||
"$ref": "urn:data" | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/resources/draft7/urn/issue665_external_urn_subschema.json
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,13 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "urn:data", | ||
"type": "object", | ||
"required": [ | ||
"value" | ||
], | ||
"properties": { | ||
"value": { | ||
"type": "string" | ||
} | ||
} | ||
} |