Skip to content

Commit

Permalink
ensure ref are normalized before lookup's
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Lopes <[email protected]>
  • Loading branch information
pmlopes committed Nov 25, 2022
1 parent 1f16fa1 commit d5ca2b8
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public SchemaRepository dereference(String uri, JsonSchema schema) throws Schema
for (String id : metaSchemaIds) {
// read files from classpath
JsonSchema schema = JsonSchema.of(fs.readFileBlocking(id.substring(id.indexOf("://") + 3)).toJsonObject());
dereference(id, schema);
// try to extract the '$id' from the schema itself, fallback to old field 'id' and if not present to the given url
dereference(schema.get("$id", schema.get("id", id)), schema);
}
return this;
}
Expand All @@ -154,6 +155,9 @@ public Validator validator(JsonSchema schema) {
public Validator validator(String ref) {
// resolve the pointer to an absolute path
final URL url = new URL(ref, baseUri);
if ("".equals(url.fragment())) {
url.anchor(""); // normalize hash https://url.spec.whatwg.org/#dom-url-hash
}
final String uri = url.href();
if (lookup.containsKey(uri)) {
return new SchemaValidatorImpl(uri, options, Collections.unmodifiableMap(lookup));
Expand Down Expand Up @@ -188,6 +192,9 @@ public Validator validator(String ref, JsonSchemaOptions options) {
// resolve the pointer to an absolute path
final URL url = new URL(ref, baseUri);
final String uri = url.href();
if ("".equals(url.fragment())) {
url.anchor(""); // normalize hash https://url.spec.whatwg.org/#dom-url-hash
}
if (lookup.containsKey(uri)) {
return new SchemaValidatorImpl(uri, config, Collections.unmodifiableMap(lookup));
}
Expand All @@ -208,6 +215,9 @@ public JsonObject resolve(JsonSchema schema) {
public JsonObject resolve(String ref) {
// resolve the pointer to an absolute path
final URL url = new URL(ref, baseUri);
if ("".equals(url.fragment())) {
url.anchor(""); // normalize hash https://url.spec.whatwg.org/#dom-url-hash
}
final String uri = url.href();
if (lookup.containsKey(uri)) {
return Ref.resolve(Collections.unmodifiableMap(lookup), baseUri, lookup.get(uri));
Expand Down

0 comments on commit d5ca2b8

Please sign in to comment.