Skip to content

Upgrade schemars to 0.9 #4682

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
40 changes: 30 additions & 10 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ syn2mas = { path = "./crates/syn2mas", version = "=0.17.0-rc.0" }

# OpenAPI schema generation and validation
[workspace.dependencies.aide]
version = "0.14.2"
version = "0.15.0"
features = ["axum", "axum-extra", "axum-json", "axum-query", "macros"]

# An `Arc` that can be atomically updated
Expand Down Expand Up @@ -330,7 +330,7 @@ features = ["yaml", "json"]
# IP network address types
[workspace.dependencies.ipnetwork]
version = "0.20.0"
features = ["serde", "schemars"]
features = ["serde"]

# Iterator utilities
[workspace.dependencies.itertools]
Expand Down Expand Up @@ -531,8 +531,8 @@ version = "0.4.5"

# JSON Schema generation
[workspace.dependencies.schemars]
version = "0.8.22"
features = ["url", "chrono", "preserve_order"]
version = "0.9.0"
features = ["url2", "chrono04", "preserve_order"]

# SEC1 encoding format
[workspace.dependencies.sec1]
Expand Down
13 changes: 7 additions & 6 deletions crates/config/src/bin/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

use schemars::r#gen::SchemaSettings;
use schemars::{
generate::SchemaSettings,
transform::{AddNullable, RecursiveTransform},
};

fn main() {
let settings = SchemaSettings::draft07().with(|s| {
s.option_nullable = false;
s.option_add_null_type = false;
});
let generator = settings.into_generator();
let generator = SchemaSettings::draft07()
.with_transform(RecursiveTransform(AddNullable::default()))
.into_generator();
let schema = generator.into_root_schema_for::<mas_config::RootConfig>();

serde_json::to_writer_pretty(std::io::stdout(), &schema).expect("Failed to serialize schema");
Expand Down
27 changes: 10 additions & 17 deletions crates/config/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,22 @@

//! Useful JSON Schema definitions

use schemars::{
JsonSchema,
r#gen::SchemaGenerator,
schema::{InstanceType, Schema, SchemaObject},
};
use std::borrow::Cow;

use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};

/// A network hostname
pub struct Hostname;

impl JsonSchema for Hostname {
fn schema_name() -> String {
"Hostname".to_string()
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("Hostname")
}

fn json_schema(generator: &mut SchemaGenerator) -> Schema {
hostname(generator)
fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"format": "hostname",
})
}
}

fn hostname(_gen: &mut SchemaGenerator) -> Schema {
Schema::Object(SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some("hostname".to_owned()),
..SchemaObject::default()
})
}
22 changes: 5 additions & 17 deletions crates/config/src/sections/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ fn default_public_base() -> Url {
"http://[::]:8080".parse().unwrap()
}

fn http_address_example_1() -> &'static str {
"[::1]:8080"
}
fn http_address_example_2() -> &'static str {
"[::]:8080"
}
fn http_address_example_3() -> &'static str {
"127.0.0.1:8080"
}
fn http_address_example_4() -> &'static str {
"0.0.0.0:8080"
}

#[cfg(not(any(feature = "docker", feature = "dist")))]
fn http_listener_assets_path_default() -> Utf8PathBuf {
"./frontend/dist/".into()
Expand Down Expand Up @@ -111,10 +98,10 @@ pub enum BindConfig {
Address {
/// Host and port on which to listen
#[schemars(
example = "http_address_example_1",
example = "http_address_example_2",
example = "http_address_example_3",
example = "http_address_example_4"
example = &"[::1]:8080",
example = &"[::]:8080",
example = &"127.0.0.1:8080",
example = &"0.0.0.0:8080",
)]
address: String,
},
Expand Down Expand Up @@ -354,6 +341,7 @@ pub struct HttpConfig {
/// List of trusted reverse proxies that can set the `X-Forwarded-For`
/// header
#[serde(default = "default_trusted_proxies")]
#[schemars(with = "Vec<String>", inner(ip))]
pub trusted_proxies: Vec<IpNetwork>,

/// Public URL base from where the authentication service is reachable
Expand Down
6 changes: 1 addition & 5 deletions crates/config/src/sections/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ use tracing::info;

use super::ConfigurationSection;

fn example_secret() -> &'static str {
"0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff"
}

/// Password config option.
///
/// It either holds the password value directly or references a file where the
Expand Down Expand Up @@ -204,7 +200,7 @@ struct EncryptionRaw {
#[schemars(
with = "Option<String>",
regex(pattern = r"[0-9a-fA-F]{64}"),
example = "example_secret"
example = &"0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff"
)]
#[serde_as(as = "Option<serde_with::hex::Hex>")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
22 changes: 5 additions & 17 deletions crates/config/src/sections/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ use url::Url;

use super::ConfigurationSection;

fn sample_rate_example() -> f64 {
0.5
}

/// Propagation format for incoming and outgoing requests
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -70,7 +66,7 @@ pub struct TracingConfig {
///
/// Defaults to `1.0` if not set.
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(example = "sample_rate_example", range(min = 0.0, max = 1.0))]
#[schemars(example = 0.5, range(min = 0.0, max = 1.0))]
pub sample_rate: Option<f64>,
}

Expand Down Expand Up @@ -123,41 +119,33 @@ impl MetricsConfig {
}
}

fn sentry_dsn_example() -> &'static str {
"https://public@host:port/1"
}

fn sentry_environment_example() -> &'static str {
"production"
}

/// Configuration related to the Sentry integration
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct SentryConfig {
/// Sentry DSN
#[schemars(url, example = "sentry_dsn_example")]
#[schemars(url, example = &"https://public@host:port/1")]
#[serde(skip_serializing_if = "Option::is_none")]
pub dsn: Option<String>,

/// Environment to use when sending events to Sentry
///
/// Defaults to `production` if not set.
#[schemars(example = "sentry_environment_example")]
#[schemars(example = &"production")]
#[serde(skip_serializing_if = "Option::is_none")]
pub environment: Option<String>,

/// Sample rate for event submissions
///
/// Defaults to `1.0` if not set.
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(example = "sample_rate_example", range(min = 0.0, max = 1.0))]
#[schemars(example = 0.5, range(min = 0.0, max = 1.0))]
pub sample_rate: Option<f32>,

/// Sample rate for tracing transactions
///
/// Defaults to `0.0` if not set.
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(example = "sample_rate_example", range(min = 0.0, max = 1.0))]
#[schemars(example = 0.5, range(min = 0.0, max = 1.0))]
pub traces_sample_rate: Option<f32>,
}

Expand Down
7 changes: 5 additions & 2 deletions crates/handlers/src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use mas_router::{
};
use mas_storage::BoxRng;
use mas_templates::{ApiDocContext, Templates};
use schemars::transform::{AddNullable, RecursiveTransform};
use tower_http::cors::{Any, CorsLayer};

mod call_context;
Expand Down Expand Up @@ -159,8 +160,10 @@ where
aide::generate::infer_responses(false);

aide::generate::in_context(|ctx| {
ctx.schema =
schemars::r#gen::SchemaGenerator::new(schemars::r#gen::SchemaSettings::openapi3());
ctx.schema = schemars::generate::SchemaGenerator::new(
schemars::generate::SchemaSettings::openapi3()
.with_transform(RecursiveTransform(AddNullable::default())),
);
});

let mut api = OpenApi::default();
Expand Down
Loading
Loading