From 8f5094ed19cf81327e0cd7ae4ae69a19385ef77a Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Tue, 10 Jun 2025 12:04:02 +0200 Subject: [PATCH] Remove optional features from the mas-policy crate --- crates/policy/Cargo.toml | 6 +---- crates/policy/src/bin/schema.rs | 24 ++++++++++-------- crates/policy/src/model.rs | 45 ++++++++++----------------------- misc/update.sh | 2 +- 4 files changed, 29 insertions(+), 48 deletions(-) diff --git a/crates/policy/Cargo.toml b/crates/policy/Cargo.toml index 073d86ef5..ca927bcc6 100644 --- a/crates/policy/Cargo.toml +++ b/crates/policy/Cargo.toml @@ -15,7 +15,7 @@ workspace = true anyhow.workspace = true arc-swap.workspace = true opa-wasm.workspace = true -schemars = { workspace = true, optional = true } +schemars.workspace = true serde_json.workspace = true serde.workspace = true thiserror.workspace = true @@ -25,9 +25,5 @@ tracing.workspace = true mas-data-model.workspace = true oauth2-types.workspace = true -[features] -jsonschema = ["dep:schemars"] - [[bin]] name = "schema" -required-features = ["jsonschema"] diff --git a/crates/policy/src/bin/schema.rs b/crates/policy/src/bin/schema.rs index d993cdbe9..3fbe09adb 100644 --- a/crates/policy/src/bin/schema.rs +++ b/crates/policy/src/bin/schema.rs @@ -4,6 +4,11 @@ // SPDX-License-Identifier: AGPL-3.0-only // Please see LICENSE in the repository root for full details. +#![expect( + clippy::disallowed_types, + reason = "We use Path/PathBuf instead of camino here for simplicity" +)] + use std::path::{Path, PathBuf}; use mas_policy::model::{ @@ -12,17 +17,14 @@ use mas_policy::model::{ use schemars::{JsonSchema, r#gen::SchemaSettings}; fn write_schema(out_dir: Option<&Path>, file: &str) { - let mut writer: Box = match out_dir { - Some(out_dir) => { - let path = out_dir.join(file); - eprintln!("Writing to {path:?}"); - let file = std::fs::File::create(path).expect("Failed to create file"); - Box::new(std::io::BufWriter::new(file)) - } - None => { - eprintln!("--- {file} ---"); - Box::new(std::io::stdout()) - } + let mut writer: Box = if let Some(out_dir) = out_dir { + let path = out_dir.join(file); + eprintln!("Writing to {path:?}"); + let file = std::fs::File::create(path).expect("Failed to create file"); + Box::new(std::io::BufWriter::new(file)) + } else { + eprintln!("--- {file} ---"); + Box::new(std::io::stdout()) }; let settings = SchemaSettings::draft07().with(|s| { diff --git a/crates/policy/src/model.rs b/crates/policy/src/model.rs index 4301b4165..d57a81655 100644 --- a/crates/policy/src/model.rs +++ b/crates/policy/src/model.rs @@ -13,12 +13,12 @@ use std::net::IpAddr; use mas_data_model::{Client, User}; use oauth2_types::{registration::VerifiedClientMetadata, scope::Scope}; +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; /// A well-known policy code. -#[derive(Deserialize, Debug, Clone, Copy)] +#[derive(Deserialize, Debug, Clone, Copy, JsonSchema)] #[serde(rename_all = "kebab-case")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub enum Code { /// The username is too short. UsernameTooShort, @@ -71,8 +71,7 @@ impl Code { } /// A single violation of a policy. -#[derive(Deserialize, Debug)] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] +#[derive(Deserialize, Debug, JsonSchema)] pub struct Violation { pub msg: String, pub redirect_uri: Option, @@ -111,9 +110,8 @@ impl EvaluationResult { } /// Identity of the requester -#[derive(Serialize, Debug, Default)] +#[derive(Serialize, Debug, Default, JsonSchema)] #[serde(rename_all = "snake_case")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub struct Requester { /// IP address of the entity making the request pub ip_address: Option, @@ -122,8 +120,7 @@ pub struct Requester { pub user_agent: Option, } -#[derive(Serialize, Debug)] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] +#[derive(Serialize, Debug, JsonSchema)] pub enum RegistrationMethod { #[serde(rename = "password")] Password, @@ -133,9 +130,8 @@ pub enum RegistrationMethod { } /// Input for the user registration policy. -#[derive(Serialize, Debug)] +#[derive(Serialize, Debug, JsonSchema)] #[serde(tag = "registration_method")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub struct RegisterInput<'a> { pub registration_method: RegistrationMethod, @@ -148,21 +144,16 @@ pub struct RegisterInput<'a> { } /// Input for the client registration policy. -#[derive(Serialize, Debug)] +#[derive(Serialize, Debug, JsonSchema)] #[serde(rename_all = "snake_case")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub struct ClientRegistrationInput<'a> { - #[cfg_attr( - feature = "jsonschema", - schemars(with = "std::collections::HashMap") - )] + #[schemars(with = "std::collections::HashMap")] pub client_metadata: &'a VerifiedClientMetadata, pub requester: Requester, } -#[derive(Serialize, Debug)] +#[derive(Serialize, Debug, JsonSchema)] #[serde(rename_all = "snake_case")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub enum GrantType { AuthorizationCode, ClientCredentials, @@ -171,23 +162,16 @@ pub enum GrantType { } /// Input for the authorization grant policy. -#[derive(Serialize, Debug)] +#[derive(Serialize, Debug, JsonSchema)] #[serde(rename_all = "snake_case")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub struct AuthorizationGrantInput<'a> { - #[cfg_attr( - feature = "jsonschema", - schemars(with = "Option>") - )] + #[schemars(with = "Option>")] pub user: Option<&'a User>, - #[cfg_attr( - feature = "jsonschema", - schemars(with = "std::collections::HashMap") - )] + #[schemars(with = "std::collections::HashMap")] pub client: &'a Client, - #[cfg_attr(feature = "jsonschema", schemars(with = "String"))] + #[schemars(with = "String")] pub scope: &'a Scope, pub grant_type: GrantType, @@ -196,9 +180,8 @@ pub struct AuthorizationGrantInput<'a> { } /// Input for the email add policy. -#[derive(Serialize, Debug)] +#[derive(Serialize, Debug, JsonSchema)] #[serde(rename_all = "snake_case")] -#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))] pub struct EmailInput<'a> { pub email: &'a str, diff --git a/misc/update.sh b/misc/update.sh index de9d4001c..4cbea8c31 100644 --- a/misc/update.sh +++ b/misc/update.sh @@ -14,7 +14,7 @@ cargo run -p mas-config > "${CONFIG_SCHEMA}" cargo run -p mas-handlers --bin graphql-schema > "${GRAPHQL_SCHEMA}" cargo run -p mas-handlers --bin api-schema > "${API_SCHEMA}" cargo run -p mas-i18n-scan -- --update "${BASE_DIR}/templates/" "${BASE_DIR}/translations/en.json" -OUT_DIR="${POLICIES_SCHEMA}" cargo run -p mas-policy --features jsonschema +OUT_DIR="${POLICIES_SCHEMA}" cargo run -p mas-policy cd "${BASE_DIR}/frontend" npm run format