Skip to content

Commit 37b289f

Browse files
committed
Allow registries to omit empty/default fields in JSON
Closes #14506
1 parent 866eafe commit 37b289f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/cargo/sources/registry/index/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ pub struct IndexPackage<'a> {
206206
#[serde(borrow)]
207207
pub deps: Vec<RegistryDependency<'a>>,
208208
/// Set of features defined for the package, i.e., `[features]` table.
209+
#[serde(default)]
209210
pub features: BTreeMap<InternedString, Vec<InternedString>>,
210211
/// This field contains features with new, extended syntax. Specifically,
211212
/// namespaced features (`dep:`) and weak dependencies (`pkg?/feat`).
@@ -268,10 +269,13 @@ pub struct RegistryDependency<'a> {
268269
#[serde(borrow)]
269270
pub req: Cow<'a, str>,
270271
/// Set of features enabled for this dependency.
272+
#[serde(default)]
271273
pub features: Vec<InternedString>,
272274
/// Whether or not this is an optional dependency.
275+
#[serde(default)]
273276
pub optional: bool,
274277
/// Whether or not default features are enabled.
278+
#[serde(default = "default_true")]
275279
pub default_features: bool,
276280
/// The target platform for this dependency.
277281
pub target: Option<Cow<'a, str>>,
@@ -292,6 +296,10 @@ pub struct RegistryDependency<'a> {
292296
pub lib: bool,
293297
}
294298

299+
fn default_true() -> bool {
300+
true
301+
}
302+
295303
impl<'gctx> RegistryIndex<'gctx> {
296304
/// Creates an empty registry index at `path`.
297305
pub fn new(

src/doc/src/reference/registry-index.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,24 @@ explaining the format of the entry.
140140
"req": "^0.6",
141141
// Array of features (as strings) enabled for this dependency.
142142
"features": ["i128_support"],
143-
// Boolean of whether or not this is an optional dependency.
143+
// Boolean of whether or not this is an optional dependency. Defaults to `false` if not specified.
144144
"optional": false,
145-
// Boolean of whether or not default features are enabled.
145+
// Boolean of whether or not default features are enabled. Defaults to `true` if not specified.
146146
"default_features": true,
147147
// The target platform for the dependency.
148-
// null if not a target dependency.
148+
// If not specified or `null`, it is not a target dependency.
149149
// Otherwise, a string such as "cfg(windows)".
150150
"target": null,
151151
// The dependency kind.
152152
// "dev", "build", or "normal".
153-
// Note: this is a required field, but a small number of entries
154-
// exist in the crates.io index with either a missing or null
155-
// `kind` field due to implementation bugs.
153+
// If not specified or `null`, it defaults to "normal".
156154
"kind": "normal",
157155
// The URL of the index of the registry where this dependency is
158-
// from as a string. If not specified or null, it is assumed the
156+
// from as a string. If not specified or `null`, it is assumed the
159157
// dependency is in the current registry.
160158
"registry": null,
161159
// If the dependency is renamed, this is a string of the actual
162-
// package name. If not specified or null, this dependency is not
160+
// package name. If not specified or `null`, this dependency is not
163161
// renamed.
164162
"package": null,
165163
}

0 commit comments

Comments
 (0)