Skip to content
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
6 changes: 6 additions & 0 deletions crates/taplo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

[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 @@ -542,9 +542,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());

Expand Down Expand Up @@ -747,3 +750,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