Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incomplete eager reference resolving #604

Merged
merged 1 commit into from
Oct 22, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Incomplete external reference resolution.

## [0.24.0] - 2024-10-20

### Added
Expand Down
4 changes: 4 additions & 0 deletions crates/jsonschema-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Incomplete external reference resolution.

## [0.24.0] - 2024-10-20

### Added
Expand Down
33 changes: 26 additions & 7 deletions crates/jsonschema-referencing/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,32 @@ fn process_resources(
}
// Retrieve external resources
for uri in external.drain() {
if !resources.contains_key(&uri) {
let mut fragmentless = uri.clone();
fragmentless.set_fragment(None);
if !resources.contains_key(&fragmentless) {
let retrieved = retriever
.retrieve(&uri.borrow())
.map_err(|err| Error::unretrievable(uri.as_str(), Some(err)))?;
.retrieve(&fragmentless.borrow())
.map_err(|err| Error::unretrievable(fragmentless.as_str(), Some(err)))?;
let resource = Arc::new(Resource::from_contents_and_specification(
retrieved,
default_draft,
)?);
resources.insert(uri.clone(), Arc::clone(&resource));
queue.push_back((uri, resource));
resources.insert(fragmentless.clone(), Arc::clone(&resource));
if let Some(fragment) = uri.fragment() {
// The original `$ref` could have a fragment that points to a place that won't
// be discovered via the regular sub-resources discovery. Therefore we need to
// explicitly check it
if let Some(resolved) = resource.contents().pointer(fragment.as_str()) {
queue.push_back((
uri,
Arc::new(Resource::from_contents_and_specification(
resolved.clone(),
default_draft,
)?),
));
}
}
queue.push_back((fragmentless, resource));
}
}
}
Expand Down Expand Up @@ -452,8 +468,11 @@ fn collect_external_resources(
// Reference has already been seen
return Ok(());
}
let mut resolved = uri::resolve_against(&base.borrow(), reference)?;
resolved.set_fragment(None);
let resolved = if reference.contains('#') && base.has_fragment() {
uri::resolve_against(&uri::DEFAULT_ROOT_URI.borrow(), reference)?
} else {
uri::resolve_against(&base.borrow(), reference)?
};
collected.insert(resolved);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/jsonschema-referencing/src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn from_str(uri: &str) -> Result<Uri<String>, Error> {
}
}

static DEFAULT_ROOT_URI: Lazy<Uri<String>> =
pub(crate) static DEFAULT_ROOT_URI: Lazy<Uri<String>> =
Lazy::new(|| Uri::parse("json-schema:///".to_string()).expect("Invalid URI"));

pub type EncodedString = EStr<Fragment>;
Expand Down
46 changes: 46 additions & 0 deletions crates/jsonschema/src/keywords/ref_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pub(crate) fn compile_recursive_ref<'a>(
#[cfg(test)]
mod tests {
use crate::tests_util;
use referencing::{Draft, Retrieve, Uri};
use serde_json::{json, Value};
use test_case::test_case;

Expand Down Expand Up @@ -403,4 +404,49 @@ mod tests {
);
}
}

#[test]
fn test_resolving_finds_references_in_referenced_resources() {
let schema = json!({"$ref": "/indirection#/baz"});

struct MyRetrieve;

impl Retrieve for MyRetrieve {
fn retrieve(
&self,
uri: &Uri<&str>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
match uri.path().as_str() {
"/indirection" => Ok(json!({
"$id": "/indirection",
"baz": {
"$ref": "/types#/foo"
}
})),
"/types" => Ok(json!({
"$id": "/types",
"foo": {
"$ref": "#/bar"
},
"bar": {
"type": "integer"
}
})),
_ => panic!("Not found"),
}
}
}

let validator = match crate::options()
.with_draft(Draft::Draft201909)
.with_retriever(MyRetrieve)
.build(&schema)
{
Ok(validator) => validator,
Err(error) => panic!("{error}"),
};

assert!(validator.is_valid(&json!(2)));
assert!(!validator.is_valid(&json!("")));
}
}