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

Bugfix/dereference schemas #100

Merged
merged 5 commits into from
Dec 14, 2023
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
1,426 changes: 814 additions & 612 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"crates/schematools",
"crates/cli"
Expand All @@ -10,8 +11,9 @@ repository = "https://github.com/kstasik/schema-tools"
version = "0.15.2"

[workspace.dependencies]
reqwest = { version = "0.11.12", default-features = false, features = ["default-tls", "blocking"] }
reqwest = { version = "0.11.22", default-features = false, features = ["default-tls", "blocking"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
serde_yaml = "0.9.14"
serde_yaml = "0.9"
log = "0.4"
thiserror = "1.0"
10 changes: 5 additions & 5 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "schematools-cli"
description = "Tools for codegen, preprocessing and validation of json-schema and openapi spec"
edition = "2018"
edition = "2021"
license.workspace = true
repository.workspace = true
version.workspace = true
Expand All @@ -15,14 +15,14 @@ json-patch = ["schematools/json-patch"]
semver = ["schematools/semver"]

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }

clap = { version = "4.1", features = ["derive"] }
clap = { version = "4.4", features = ["derive"] }
env_logger = "0.10"
sha2 = "0.10.1"
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.10"

schematools = { path = "../schematools" }
schematools = { path = "../schematools" }
13 changes: 7 additions & 6 deletions crates/schematools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "schematools"
description = "Tools for codegen, preprocessing and validation of json-schema and openapi spec"
edition = "2018"
edition = "2021"
license.workspace = true
readme = "../../README.md"
repository.workspace = true
Expand All @@ -13,6 +13,7 @@ codegen = ["tera", "semver"]
http = ["jsonschema/resolve-http", "reqwest"]

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
reqwest = { workspace = true, optional = true }
Expand All @@ -24,15 +25,15 @@ lazy_static = "1.4.0"
regex = "1"
jsonschema = { version = "0.17", default-features = false, features = ["resolve-file"] }
tera = { version = "1", default-features = false, optional = true }
serde = { version = "1.0", features = ["derive"] }
walkdir = "2"
json-patch = { version = "0.2.7", optional = true }
json-patch = { version = "1.2", optional = true }
semver = { version = "1.0", optional = true }
git2 = { version = "0.17", optional = true }
git2 = { version = "0.18", optional = true }
md5 = "0.7.0"
Inflector = "0.11"
Inflector = { version = "0.11" }
digest = "0.10.1"

[dev-dependencies]
test-case = "3.0"
serial_test = "2.0"
test-case = "3.3"
testing_logger = "0.1.1"
5 changes: 5 additions & 0 deletions crates/schematools/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ pub fn discover_git(
#[cfg(feature = "git2")]
mod tests {
use super::*;
use serial_test::serial;

#[test]
#[serial]
fn test_discovery_git_inherit_templates() {
let mut discovery = Discovery::default();

Expand Down Expand Up @@ -203,6 +205,7 @@ mod tests {
}

#[test]
#[serial]
fn test_discovery_git_inherit() {
let mut discovery = Discovery::default();

Expand All @@ -229,6 +232,7 @@ mod tests {
}

#[test]
#[serial]
fn test_discovery_git() {
let mut discovery = Discovery::default();

Expand Down Expand Up @@ -269,6 +273,7 @@ mod tests {
}

#[test]
#[serial]
fn test_discover_git_hash() {
let registry = discover_git(
"https://github.com/kstasik/schema-tools.git",
Expand Down
19 changes: 7 additions & 12 deletions crates/schematools/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@ pub mod patch;
use serde_json::Value;
use url::Url;

// this function should probably be removed in favor of schema registry
pub fn rel_to_absolute_refs(_url: &Url, mut _data: Value) -> Value {
unreachable!();
pub fn rel_to_absolute_refs(url: &Url, mut data: Value) -> Value {
if url.scheme() == "file" {
let mut prefix = url.clone();
prefix.path_segments_mut().unwrap().pop();

/*
if url.scheme() == "file" {
let mut prefix = url.clone();
prefix.path_segments_mut().unwrap().pop();

process_node(&prefix, &mut data);
}
process_node(&prefix, &mut data);
}

data
*/
data
}

fn process_node(url: &Url, data: &mut Value) {
Expand Down
9 changes: 5 additions & 4 deletions crates/schematools/src/process/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::schema::Schema;
use crate::{error::Error, schema::path_to_url};

#[cfg(feature = "json-patch")]
use json_patch::{diff, from_value, patch};
use json_patch::{diff, patch, Patch};
use serde::Serialize;
use serde_json::Value;
use serde_json::{from_value, Value};

#[derive(Copy, Clone, Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Operation {
Expand Down Expand Up @@ -65,12 +65,13 @@ pub fn execute(schema: &mut Schema, action: &Action) -> Result<(), Error> {
}
Action::Apply(c) => {
let patch_file = Schema::load_url(path_to_url(c.patch.clone())?)?;
let p = from_value(patch_file.get_body().clone()).map_err(Error::SerdeJsonError)?;
let p: Patch =
from_value(patch_file.get_body().clone()).map_err(Error::SerdeJsonError)?;

patch(schema.get_body_mut(), &p).map_err(Error::JsonPatchError)
}
Action::Inline(i) => {
let p = from_value(serde_json::json!([i])).map_err(Error::SerdeJsonError)?;
let p: Patch = from_value(serde_json::json!([i])).map_err(Error::SerdeJsonError)?;

patch(schema.get_body_mut(), &p).map_err(Error::JsonPatchError)
}
Expand Down
116 changes: 116 additions & 0 deletions crates/schematools/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,122 @@ mod tests {
assert_eq!(spec.is_ok(), true);
}

#[test]
fn test_when_file_and_spec_are_valid_with_reference() {
let url = Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/02-simple-with-reference.json"
))
.unwrap();

let spec = Schema::load_url(url);
assert_eq!(spec.is_ok(), true);
}

#[test]
fn test_when_file_and_spec_are_valid_with_external_ref() {
let url = Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/03-simple-with-external-ref.json"
))
.unwrap();

let spec = Schema::load_url(url);
assert_eq!(spec.is_ok(), true);
}

#[test]
fn test_loading_few_simple_spec() {
let urls = vec![
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/01-simple.json"
))
.unwrap(),
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/01-simple.json"
))
.unwrap(),
];

let spec = Schema::load_urls(urls);
assert_eq!(spec.is_ok(), true);
}

#[test]
fn test_loading_few_specs_with_reference() {
let urls = vec![
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/02-simple-with-reference.json"
))
.unwrap(),
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/02-simple-with-reference.json"
))
.unwrap(),
];

let spec = Schema::load_urls(urls);
assert_eq!(spec.is_ok(), true);
}

#[test]
fn test_loading_few_specs_with_external_reference() {
let urls = vec![
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/03-simple-with-external-ref.json"
))
.unwrap(),
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/03-simple-with-external-ref.json"
))
.unwrap(),
];

let spec = Schema::load_urls(urls);
assert_eq!(spec.is_ok(), true);
}

#[test]
fn test_loading_few_different_specs_url() {
let urls = vec![
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/01-simple.json"
))
.unwrap(),
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/02-simple-with-reference.json"
))
.unwrap(),
Url::parse(&format!(
"file://{}/{}",
env!("CARGO_MANIFEST_DIR"),
"resources/test/json-schemas/03-simple-with-external-ref.json"
))
.unwrap(),
];

let spec = Schema::load_urls(urls);
assert_eq!(spec.is_ok(), true);
}

#[test_case( "./not-existing.json".to_string() ; "relative" )]
#[test_case( "../not-existing.json".to_string() ; "relative2" )]
#[test_case( "not-existing.json".to_string(); "relative3" )]
Expand Down
4 changes: 2 additions & 2 deletions crates/schematools/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn resolve_externals(
}
}
Value::Array(a) => {
for (_, x) in a.iter().enumerate() {
for x in a.iter() {
resolve_externals(resolved, base, x, client);
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ fn absolutize_refs(current: &Url, root: &mut Value) {
}
}
Value::Array(ref mut a) => {
for (_, x) in a.iter_mut().enumerate() {
for x in a.iter_mut() {
absolutize_refs(current, x);
}
}
Expand Down
Loading