Skip to content

Support external references #621

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
845 changes: 505 additions & 340 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions cargo-typify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub struct CliArgs {
value_parser = ["generate", "allow", "deny"]
)]
unknown_crates: Option<String>,

#[arg(short = 'D', long, default_value = "false")]
distinct_definitions: bool,
}

impl CliArgs {
Expand Down Expand Up @@ -162,6 +165,8 @@ pub fn convert(args: &CliArgs) -> Result<String> {
}

let mut type_space = TypeSpace::new(&settings);
type_space.with_path(&args.input);
type_space.distinct_defs(args.distinct_definitions);
type_space
.add_root_schema(schema)
.wrap_err("Schema conversion failed")?;
Expand Down Expand Up @@ -193,6 +198,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert_eq!(args.output_path(), None);
Expand All @@ -208,6 +214,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert_eq!(args.output_path(), Some(PathBuf::from("some_file.rs")));
Expand All @@ -223,6 +230,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert_eq!(args.output_path(), Some(PathBuf::from("input.rs")));
Expand All @@ -238,6 +246,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert!(args.use_builder());
Expand All @@ -253,6 +262,7 @@ mod tests {
no_builder: true,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert!(!args.use_builder());
Expand All @@ -268,6 +278,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert!(args.use_builder());
Expand Down
3 changes: 3 additions & 0 deletions cargo-typify/tests/outputs/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Options:

[possible values: generate, allow, deny]

-D, --distinct-definitions


-h, --help
Print help (see a summary with '-h')

Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore = ["/"]
3 changes: 2 additions & 1 deletion typify-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ readme = "../README.md"

[dependencies]
heck = "0.5.0"
iref = "3.1.4"
log = "0.4.22"
pathdiff = "0.2.1"
proc-macro2 = "1.0.86"
quote = "1.0.36"
regress = "0.10.0"
Expand All @@ -21,7 +23,6 @@ syn = { version = "2.0.72", features = ["full"] }
thiserror = "1.0.63"
unicode-ident = "1.0.12"


[dev-dependencies]
env_logger = "0.10.2"
expectorate = "1.1.0"
Expand Down
15 changes: 10 additions & 5 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl TypeSpace {
object: None,
reference: Some(reference),
extensions: _,
} => self.convert_reference(metadata, reference),
} => self.convert_reference(metadata, &reference),

// Accept references that... for some reason... include the type.
// TODO this could be generalized to validate any redundant
Expand Down Expand Up @@ -905,6 +905,7 @@ impl TypeSpace {
// this case and strip out the null in both enum values and instance
// type. Nevertheless, we do our best to interpret even incorrect
// JSON schema.

let mut has_null = false;

let validator = StringValidator::new(&type_name, validation)?;
Expand Down Expand Up @@ -1051,7 +1052,14 @@ impl TypeSpace {
// f64 here, but we're already constrained by the schemars
// representation so ... it's probably the best we can do at
// the moment.
match (default.as_f64(), min, max) {
//
// I added this because numbers are sometimes specified in double quotes
let d = match default {
serde_json::Value::Number(a) => a.as_f64(),
serde_json::Value::String(a) => a.parse().ok(),
_ => None,
};
match (d, min, max) {
(Some(_), None, None) => Some(()),
(Some(value), None, Some(fmax)) if value <= fmax => Some(()),
(Some(value), Some(fmin), None) if value >= fmin => Some(()),
Expand Down Expand Up @@ -1275,9 +1283,6 @@ impl TypeSpace {
metadata: &'a Option<Box<Metadata>>,
ref_name: &str,
) -> Result<(TypeEntry, &'a Option<Box<Metadata>>)> {
if !ref_name.starts_with('#') {
panic!("external references are not supported: {}", ref_name);
}
let key = ref_key(ref_name);
let type_id = self
.ref_to_id
Expand Down
Loading
Loading