Skip to content

Generate schema for v2 manifest #2997

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

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
extraArgs: "--features openssl/vendored",
target: "",
targetDir: "target/release",
generateManifestSchema: true,
}
- {
os: "ubuntu-22.04",
Expand Down Expand Up @@ -132,6 +133,10 @@ jobs:
--output-signature spin.sig \
${{ matrix.config.targetDir }}/spin${{ matrix.config.extension }}

- name: Generate manifest schema
if: matrix.config.generateManifestSchema
run: ${{ matrix.config.targetDir }}/spin${{ matrix.config.extension }} maintenance generate-manifest-schema -o manifest.schema.json

- name: package release assets
if: runner.os != 'Windows'
shell: bash
Expand Down Expand Up @@ -167,6 +172,13 @@ jobs:
with:
name: spin-${{ env.RUNNER_OS }}-${{ matrix.config.arch }}
path: _dist/spin-${{ env.RELEASE_VERSION }}-${{ env.RUNNER_OS }}-${{ matrix.config.arch }}.zip

- name: Upload schema as GitHub artifact
if: matrix.config.generateManifestSchema
uses: actions/upload-artifact@v4
with:
name: manifest.schema.json
path: manifest.schema.json

checksums:
name: generate release checksums
Expand Down Expand Up @@ -206,13 +218,20 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: download release assets
- name: download binaries for release assets
uses: actions/download-artifact@v4
with:
pattern: spin-*
path: _dist
merge-multiple: true

- name: download schema for release assets
uses: actions/download-artifact@v4
with:
pattern: manifest.schema.json
path: _dist
merge-multiple: true

- name: check if pre-release
shell: bash
run: |
Expand Down
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pretty_assertions = "1.3"
regex = { workspace = true }
reqwest = { workspace = true }
rpassword = "7"
schemars = { version = "0.8.21", features = ["indexmap2", "semver"] }
semver = { workspace = true }
serde = { version = "1", features = ["derive"] }
serde_json = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/manifest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = { workspace = true }
[dependencies]
anyhow = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
schemars = { version = "0.8.21", features = ["indexmap2", "semver"] }
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you call a bunch of pirates plotting to overthrow their captain?

semver = { workspace = true, features = ["serde"] }
serde = { workspace = true }
spin-serde = { path = "../serde" }
Expand Down
1 change: 1 addition & 0 deletions crates/manifest/src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn v1_to_v2_app(manifest: v1::AppManifestV1) -> Result<v2::AppManifest, Erro
};
components.insert(
component_id.clone(),
#[allow(deprecated)]
v2::Component {
source: component.source,
description: component.description,
Expand Down
13 changes: 8 additions & 5 deletions crates/manifest/src/schema/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fmt::Display;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use wasm_pkg_common::{package::PackageRef, registry::Registry};

/// Variable definition
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Variable {
/// `required = true`
Expand All @@ -20,7 +21,7 @@ pub struct Variable {
}

/// Component source
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, untagged)]
pub enum ComponentSource {
/// `"local.wasm"`
Expand All @@ -35,8 +36,10 @@ pub enum ComponentSource {
/// `{ ... }`
Registry {
/// `registry = "example.com"`
#[schemars(with = "Option<String>")]
registry: Option<Registry>,
/// `package = "example:component"`
#[schemars(with = "String")]
package: PackageRef,
/// `version = "1.2.3"`
version: String,
Expand Down Expand Up @@ -64,7 +67,7 @@ impl Display for ComponentSource {
}

/// WASI files mount
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, untagged)]
pub enum WasiFilesMount {
/// `"images/*.png"`
Expand All @@ -79,7 +82,7 @@ pub enum WasiFilesMount {
}

/// Component build configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ComponentBuildConfig {
/// `command = "cargo build"`
Expand All @@ -104,7 +107,7 @@ impl ComponentBuildConfig {
}

/// Component build command or commands
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum Commands {
/// `command = "cargo build"`
Expand Down
107 changes: 107 additions & 0 deletions crates/manifest/src/schema/json_schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::schema::v2::{ComponentSpec, Map, OneOrManyComponentSpecs};
use schemars::JsonSchema;

// The structs here allow dead code because they exist only
// to represent JSON schemas, and are never instantiated.

#[allow(dead_code)]
#[derive(JsonSchema)]
pub struct TriggerSchema {
/// HTTP triggers
#[schemars(default)]
http: Vec<HttpTriggerSchema>,
/// Redis triggers
#[schemars(default)]
redis: Vec<RedisTriggerSchema>,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(deny_unknown_fields)]
pub struct HttpTriggerSchema {
/// `id = "trigger-id"`
#[serde(default, skip_serializing_if = "String::is_empty")]
pub id: String,
/// `component = ...`
#[serde(default, skip_serializing_if = "Option::is_none")]
pub component: Option<ComponentSpec>,
/// `components = { ... }`
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub components: Map<String, OneOrManyComponentSpecs>,
/// `route = "/user/:name/..."`
route: HttpRouteSchema,
/// `executor = { type = "wagi" }
#[schemars(default, schema_with = "toml_table")]
executor: Option<toml::Table>,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(untagged)]
pub enum HttpRouteSchema {
/// `route = "/user/:name/..."`
Route(String),
/// `route = { private = true }`
Private(HttpPrivateEndpoint),
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(deny_unknown_fields)]
pub struct HttpPrivateEndpoint {
/// Whether the private endpoint is private. This must be true.
pub private: bool,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(deny_unknown_fields)]
pub struct RedisTriggerSchema {
/// `id = "trigger-id"`
#[serde(default, skip_serializing_if = "String::is_empty")]
pub id: String,
/// `component = ...`
#[serde(default, skip_serializing_if = "Option::is_none")]
pub component: Option<ComponentSpec>,
/// `components = { ... }`
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub components: Map<String, OneOrManyComponentSpecs>,
/// `channel = "my-messages"`
channel: String,
/// `address = "redis://redis.example.com:6379"`
#[serde(default, skip_serializing_if = "Option::is_none")]
address: Option<String>,
}

pub fn toml_table(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::SingleOrVec::Single(Box::new(
schemars::schema::InstanceType::Object,
))),
..Default::default()
})
}

pub fn map_of_toml_tables(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::SingleOrVec::Single(Box::new(
schemars::schema::InstanceType::Object,
))),
..Default::default()
})
}

pub fn one_or_many<T: schemars::JsonSchema>(
gen: &mut schemars::gen::SchemaGenerator,
) -> schemars::schema::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
one_of: Some(vec![
gen.subschema_for::<T>(),
gen.subschema_for::<Vec<T>>(),
]),
..Default::default()
})),
..Default::default()
})
}
1 change: 1 addition & 0 deletions crates/manifest/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod v2;
// Types common between manifest versions. Re-exported from versioned modules
// to make them easier to split if necessary.
pub(crate) mod common;
mod json_schema;

#[derive(Deserialize)]
pub(crate) struct VersionProbe {
Expand Down
Loading
Loading