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

Support external references #621

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
331 changes: 315 additions & 16 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 @@ -195,6 +200,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert_eq!(args.output_path(), None);
Expand All @@ -210,6 +216,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 @@ -225,6 +232,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 @@ -240,6 +248,7 @@ mod tests {
no_builder: false,
crates: vec![],
unknown_crates: Default::default(),
distinct_definitions: false,
};

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

assert!(!args.use_builder());
Expand All @@ -270,6 +280,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 @@ -20,7 +20,8 @@ serde_json = "1.0.119"
syn = { version = "2.0.68", features = ["full"] }
thiserror = "1.0.61"
unicode-ident = "1.0.12"

pathdiff = "0.2.1"
iref = "3.1.4"
Copy link
Contributor

Choose a reason for hiding this comment

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

the dependencies above are alpha sorted. would be good for these new ones to also be alpha sorted


[dev-dependencies]
env_logger = "0.10.2"
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 @@ -884,6 +884,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 @@ -1030,7 +1031,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 @@ -1173,9 +1181,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