Skip to content

Commit 2ea93da

Browse files
committed
fix: Generate a correct schema for the manifests
1 parent 4c45d94 commit 2ea93da

File tree

10 files changed

+220
-224
lines changed

10 files changed

+220
-224
lines changed

crates/icp-cli/src/commands/network/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ pub(crate) async fn exec(ctx: &Context, args: &RunArgs) -> Result<(), CommandErr
7575

7676
let cfg = match &network.configuration {
7777
// Locally-managed network
78-
Configuration::Managed(cfg) => cfg,
78+
Configuration::Managed { managed: cfg } => cfg,
7979

8080
// Non-managed networks cannot be started
81-
Configuration::Connected(_) => {
81+
Configuration::Connected { connected: _ } => {
8282
return Err(CommandError::Unmanaged {
8383
name: args.name.to_owned(),
8484
});

crates/icp/src/manifest/canister.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
};
88

99
#[derive(Clone, Debug, PartialEq, JsonSchema, Deserialize)]
10+
#[serde(untagged)]
1011
pub enum Instructions {
1112
Recipe {
1213
recipe: Recipe,
@@ -31,9 +32,9 @@ pub struct CanisterManifest {
3132
/// The unique name of the canister as defined in this manifest.
3233
pub name: String,
3334

34-
/// The configuration specifying the various settings when
35-
/// creating the canister.
35+
/// The configuration specifying the various settings when creating the canister.
3636
#[serde(default)]
37+
#[schemars(with = "Option<Settings>")]
3738
pub settings: Settings,
3839

3940
#[serde(flatten)]

crates/icp/src/manifest/environment.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Deserializer};
55

66
use crate::canister::Settings;
77

8-
#[derive(Clone, Debug, PartialEq, Deserialize)]
8+
#[derive(Clone, Debug, PartialEq, Deserialize, JsonSchema)]
99
pub struct EnvironmentInner {
1010
pub name: String,
1111
pub network: Option<String>,
@@ -30,16 +30,20 @@ pub enum CanisterSelection {
3030

3131
#[derive(Clone, Debug, PartialEq, JsonSchema)]
3232
pub struct EnvironmentManifest {
33-
// environment name
33+
// The environment name
3434
pub name: String,
3535

36-
// target network for canister deployment
36+
/// The target network for canister deployment.
37+
/// Defaults to the `local` network if not specified
38+
#[schemars(with = "Option<String>")]
3739
pub network: String,
3840

39-
// canisters the environment should contain
41+
/// An optional list of the canisters to be included in this environments.
42+
/// Defaults to all the canisters if not specified.
43+
#[schemars(with = "Option<Vec<String>>")]
4044
pub canisters: CanisterSelection,
4145

42-
// canister settings overrides
46+
/// Override the canister settings for this environment
4347
pub settings: Option<HashMap<String, Settings>>,
4448
}
4549

crates/icp/src/manifest/mod.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub mod project;
1818
pub(crate) mod recipe;
1919
pub(crate) mod serde_helpers;
2020

21-
pub use {canister::CanisterManifest, environment::EnvironmentManifest, network::NetworkManifest};
21+
pub use {
22+
canister::CanisterManifest,
23+
environment::EnvironmentManifest,
24+
network::NetworkManifest,
25+
};
2226

2327
pub const PROJECT_MANIFEST: &str = "icp.yaml";
2428
pub const CANISTER_MANIFEST: &str = "canister.yaml";
@@ -44,14 +48,18 @@ impl Default for Networks {
4448
Networks::Networks(vec![
4549
NetworkManifest {
4650
name: "local".to_string(),
47-
configuration: Configuration::Managed(Managed::default()),
51+
configuration: Configuration::Managed {
52+
managed: Managed::default(),
53+
},
4854
},
4955
NetworkManifest {
5056
name: "mainnet".to_string(),
51-
configuration: Configuration::Connected(Connected {
52-
url: IC_MAINNET_NETWORK_URL.to_string(),
53-
root_key: None,
54-
}),
57+
configuration: Configuration::Connected {
58+
connected: Connected {
59+
url: IC_MAINNET_NETWORK_URL.to_string(),
60+
root_key: None,
61+
},
62+
},
5563
},
5664
])
5765
}
@@ -149,19 +157,23 @@ mod tests {
149157
Networks::Networks(vec![
150158
NetworkManifest {
151159
name: "local".to_string(),
152-
configuration: Configuration::Managed(Managed {
153-
gateway: Gateway {
154-
host: "localhost".to_string(),
155-
port: Port::Fixed(8000),
156-
},
157-
}),
160+
configuration: Configuration::Managed {
161+
managed: Managed {
162+
gateway: Gateway {
163+
host: "localhost".to_string(),
164+
port: Port::Fixed(8000),
165+
},
166+
}
167+
},
158168
},
159169
NetworkManifest {
160170
name: "mainnet".to_string(),
161-
configuration: Configuration::Connected(Connected {
162-
url: "https://icp-api.io".to_string(),
163-
root_key: None,
164-
}),
171+
configuration: Configuration::Connected {
172+
connected: Connected {
173+
url: "https://icp-api.io".to_string(),
174+
root_key: None,
175+
}
176+
},
165177
},
166178
])
167179
);

crates/icp/src/manifest/network.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ pub struct NetworkInner {
1111
pub configuration: Option<Configuration>,
1212
}
1313

14+
/// A network definition for the project
1415
#[derive(Clone, Debug, PartialEq, JsonSchema)]
1516
pub struct NetworkManifest {
1617
pub name: String,
18+
19+
#[schemars(with = "Option<Configuration>")]
1720
pub configuration: Configuration,
1821
}
1922

@@ -82,12 +85,14 @@ mod tests {
8285
)?,
8386
NetworkManifest {
8487
name: "my-network".to_string(),
85-
configuration: Configuration::Managed(Managed {
86-
gateway: Gateway {
87-
host: "localhost".to_string(),
88-
port: Port::Fixed(8000),
88+
configuration: Configuration::Managed {
89+
managed: Managed {
90+
gateway: Gateway {
91+
host: "localhost".to_string(),
92+
port: Port::Fixed(8000),
93+
}
8994
}
90-
})
95+
}
9196
},
9297
);
9398

@@ -148,10 +153,12 @@ mod tests {
148153
)?,
149154
NetworkManifest {
150155
name: "my-network".to_string(),
151-
configuration: Configuration::Connected(Connected {
152-
url: "https://ic0.app".to_string(),
153-
root_key: None,
154-
})
156+
configuration: Configuration::Connected {
157+
connected: Connected {
158+
url: "https://ic0.app".to_string(),
159+
root_key: None,
160+
}
161+
}
155162
},
156163
);
157164

@@ -171,10 +178,12 @@ mod tests {
171178
)?,
172179
NetworkManifest {
173180
name: "my-network".to_string(),
174-
configuration: Configuration::Connected(Connected {
175-
url: "https://ic0.app".to_string(),
176-
root_key: Some("root-key".to_string()),
177-
})
181+
configuration: Configuration::Connected {
182+
connected: Connected {
183+
url: "https://ic0.app".to_string(),
184+
root_key: Some("root-key".to_string()),
185+
}
186+
}
178187
},
179188
);
180189

@@ -192,12 +201,14 @@ mod tests {
192201
)?,
193202
NetworkManifest {
194203
name: "my-network".to_string(),
195-
configuration: Configuration::Managed(Managed {
196-
gateway: Gateway {
197-
host: "localhost".to_string(),
198-
port: Port::Fixed(8000),
204+
configuration: Configuration::Managed {
205+
managed: Managed {
206+
gateway: Gateway {
207+
host: "localhost".to_string(),
208+
port: Port::Fixed(8000),
209+
}
199210
}
200-
})
211+
}
201212
},
202213
);
203214

@@ -218,12 +229,14 @@ mod tests {
218229
)?,
219230
NetworkManifest {
220231
name: "my-network".to_string(),
221-
configuration: Configuration::Managed(Managed {
222-
gateway: Gateway {
223-
host: "my-host".to_string(),
224-
port: Port::Fixed(1234),
232+
configuration: Configuration::Managed {
233+
managed: Managed {
234+
gateway: Gateway {
235+
host: "my-host".to_string(),
236+
port: Port::Fixed(1234),
237+
}
225238
}
226-
})
239+
}
227240
},
228241
);
229242

@@ -243,12 +256,14 @@ mod tests {
243256
)?,
244257
NetworkManifest {
245258
name: "my-network".to_string(),
246-
configuration: Configuration::Managed(Managed {
247-
gateway: Gateway {
248-
host: "localhost".to_string(),
249-
port: Port::Random,
259+
configuration: Configuration::Managed {
260+
managed: Managed {
261+
gateway: Gateway {
262+
host: "localhost".to_string(),
263+
port: Port::Random,
264+
}
250265
}
251-
})
266+
}
252267
},
253268
);
254269

crates/icp/src/manifest/project.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ impl From<Environments> for Vec<EnvironmentManifest> {
8080

8181
#[derive(Debug, PartialEq, JsonSchema)]
8282
pub struct ProjectManifest {
83+
#[serde(flatten)]
84+
#[schemars(with = "Option<Canisters>")]
8385
pub canisters: Vec<Item<CanisterManifest>>,
86+
87+
#[schemars(with = "Option<NetworkManifest>")]
8488
pub networks: Vec<NetworkManifest>,
89+
90+
#[schemars(with = "Option<EnvironmentManifest>")]
8591
pub environments: Vec<EnvironmentManifest>,
8692
}
8793

crates/icp/src/manifest/recipe.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,24 @@ impl From<RecipeType> for String {
9595

9696
#[derive(Clone, Debug, Deserialize, PartialEq, JsonSchema)]
9797
pub struct Recipe {
98+
/// An identifier for a recipe, it can have one of the following formats:
99+
///
100+
/// `file://<path_to_recipe>` - point to a local recipe template
101+
///
102+
/// `http://<url_to_recipe>` - point to a remote recipe template
103+
///
104+
/// `@<registry>/<recipe_name>[@<version>]` - Point to a recipe in a known registry.
105+
///
106+
/// For now the only registry is the `dfinity` regitry at https://github.com/dfinity/icp-cli-recipes
107+
/// `version` is optional and defaults to `latest`
108+
///
109+
/// It is always recommended to pin the version and provide a hash for it in the `sha256` field
98110
#[serde(rename = "type")]
111+
#[schemars(with = "String")]
99112
pub recipe_type: RecipeType,
100113

101114
#[serde(default)]
102-
#[schemars(with = "HashMap<String, serde_json::Value>")]
115+
#[schemars(with = "Option<HashMap<String, serde_json::Value>>")]
103116
pub configuration: HashMap<String, serde_yaml::Value>,
104117

105118
/// Optional sha256 checksum for the recipe template.

crates/icp/src/network/access.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn get_network_access(
6969
let access = match &network.configuration {
7070
//
7171
// Managed
72-
Configuration::Managed(_) => {
72+
Configuration::Managed { managed: _ } => {
7373
// Load network descriptor
7474
let desc =
7575
nd.load_network_descriptor()?
@@ -111,7 +111,7 @@ pub fn get_network_access(
111111

112112
//
113113
// Connected
114-
Configuration::Connected(cfg) => {
114+
Configuration::Connected { connected: cfg } => {
115115
let root_key = cfg
116116
.root_key
117117
.as_ref()

crates/icp/src/network/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,27 @@ pub struct Connected {
8383
#[derive(Clone, Debug, Deserialize, PartialEq, JsonSchema, Serialize)]
8484
#[serde(tag = "mode", rename_all = "lowercase")]
8585
pub enum Configuration {
86+
// Note: we must use struct variants to be able to flatten
87+
// and make schemars generate the proper schema
8688
/// A managed network is one which can be controlled and manipulated.
87-
Managed(Managed),
89+
Managed {
90+
#[serde(flatten)]
91+
managed: Managed,
92+
},
8893

8994
/// A connected network is one which can be interacted with
9095
/// but cannot be controlled or manipulated.
91-
Connected(Connected),
96+
Connected {
97+
#[serde(flatten)]
98+
connected: Connected,
99+
},
92100
}
93101

94102
impl Default for Configuration {
95103
fn default() -> Self {
96-
Configuration::Managed(Managed::default())
104+
Configuration::Managed {
105+
managed: Managed::default(),
106+
}
97107
}
98108
}
99109

0 commit comments

Comments
 (0)