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

[taplo-common] fix infinite recursion with composed allOfs #644

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/taplo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ tokio = { version = "1.24.2", features = [
"io-util",
], default-features = false }

[dev-dependencies]
tokio = { version = "1.24.2", features = [
"macros",
"test-util",
] }

[features]
# default-tls enables native-tls but without enabling native-tls specific features.
native-tls = ["reqwest/default-tls"]
Expand Down
46 changes: 43 additions & 3 deletions crates/taplo-common/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,12 @@ impl<E: Environment> Schemas<E> {
if let Some(all_ofs) = schema["allOf"].as_array() {
if !all_ofs.is_empty() && composed {
let mut schema = schema.clone();
if let Some(obj) = schema["allOf"].as_object_mut() {
obj.remove("allOf");
}
// Remove the allOf because we're resolving in this spot -- not doing so will cause
// infinite recursion.
schema
.as_object_mut()
.expect("schema is an object")
.remove("allOf");

let mut merged_all_of = Value::Object(serde_json::Map::default());

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed below in line 569, and above in line 512/518/525, that depth wasn't getting subtracted by 1. Is this expected? Naively I'd expect depth to be subtracted here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it looks like #353 is related.

Expand Down Expand Up @@ -720,3 +723,40 @@ mod formats {
semver::VersionReq::parse(value).is_ok()
}
}

#[cfg(test)]
mod tests {
use std::path::Path;

use crate::environment::native::NativeEnvironment;

use super::*;

#[tokio::test]
async fn test_all_of_composed() {
let schemas = new_schemas();
let schema_url = url_for_test_schema("all-of-composed.json");

// Ensure this doesn't panic: this ensures that allOfs don't cause infinite recursion.
schemas
.possible_schemas_from(&schema_url, &serde_json::Value::Null, &Keys::empty(), 8)
.await
.unwrap();
}

fn new_schemas() -> Schemas<NativeEnvironment> {
Schemas::new(NativeEnvironment::new(), reqwest::Client::new())
}

fn url_for_test_schema(name: &str) -> Url {
let dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let path = dir
.parent()
.unwrap()
.parent()
.unwrap()
.join("test-data/schemas")
.join(name);
Url::parse(&format!("file://{}", path.display())).unwrap()
}
}
23 changes: 23 additions & 0 deletions test-data/schemas/all-of-composed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"duration": {
"allOf": [
{
"$ref": "#/definitions/Duration"
}
]
}
},
"definitions": {
"Duration": {
"title": "Duration",
"description": "A duration, specified in a human-readable format.",
"examples": [
"60s",
"1w 3d"
],
"type": "string"
}
}
}
Loading