Skip to content

Commit

Permalink
fix(schemas): Fix 'metadata' JSON Schema (#15033)
Browse files Browse the repository at this point in the history
Instead of allowing any type in metadata, we were specifying fields like
`"string": "<any string>"`.

Found this when looking into #15030

<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

### What does this PR try to resolve?

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

### How should we test and review this PR?

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
  • Loading branch information
weihanglo authored Jan 8, 2025
2 parents 33b49c9 + e48257d commit 4a1e2ea
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cargo-platform = { path = "crates/cargo-platform", version = "0.2.0" }
cargo-test-macro = { version = "0.4.0", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.7.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.14", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.7.0", path = "crates/cargo-util-schemas" }
cargo-util-schemas = { version = "0.7.3", path = "crates/cargo-util-schemas" }
cargo_metadata = "0.19.0"
clap = "4.5.20"
clap_complete = { version = "4.5.35", features = ["unstable-dynamic"] }
Expand Down
35 changes: 1 addition & 34 deletions crates/cargo-util-schemas/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -621,40 +621,7 @@
}
]
},
"TomlValue": {
"type": "object",
"properties": {
"string": {
"type": "string"
},
"integer": {
"type": "integer",
"format": "int64"
},
"float": {
"type": "number",
"format": "double"
},
"boolean": {
"type": "boolean"
},
"datetime": {
"type": "string"
},
"array": {
"type": "array",
"items": {
"$ref": "#/definitions/TomlValue"
}
},
"table": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/TomlValue"
}
}
}
},
"TomlValue": true,
"TomlTarget": {
"type": "object",
"properties": {
Expand Down
37 changes: 2 additions & 35 deletions crates/cargo-util-schemas/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use schemars::JsonSchema;

use serde::{Deserialize, Serialize};

use std::collections::HashMap;
use std::string::String;

use toml::Value as TomlValue;
Expand All @@ -16,39 +15,7 @@ impl JsonSchema for TomlValueWrapper {
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
use schemars::schema::*;

SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(ObjectValidation {
properties: [
(
"string".to_string(),
gen.subschema_for::<std::string::String>(),
),
("integer".to_string(), gen.subschema_for::<i64>()),
("float".to_string(), gen.subschema_for::<f64>()),
("boolean".to_string(), gen.subschema_for::<bool>()),
(
"datetime".to_string(),
gen.subschema_for::<std::string::String>(),
), // Assuming datetime is represented as a string
(
"array".to_string(),
gen.subschema_for::<Vec<TomlValueWrapper>>(),
),
(
"table".to_string(),
gen.subschema_for::<HashMap<std::string::String, TomlValueWrapper>>(),
),
]
.iter()
.cloned()
.collect(),
..Default::default()
})),
..Default::default()
}
.into()
// HACK: this is both more and less permissive than `TomlValue` but its close
gen.subschema_for::<serde_json::Value>().into()
}
}

0 comments on commit 4a1e2ea

Please sign in to comment.