[ruff] Update schemars to v1 (#20942)

This commit is contained in:
Takayuki Maeda 2025-10-20 15:59:52 +09:00 committed by GitHub
parent 991e8ed178
commit 48b50128eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 686 additions and 704 deletions

View file

@ -34,6 +34,7 @@ rustc-hash = { workspace = true }
salsa = { workspace = true }
serde = { workspace = true, optional = true }
schemars = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
smallvec = { workspace = true }
static_assertions = { workspace = true }
thiserror = { workspace = true }
@ -66,7 +67,7 @@ serde = [
"ruff_source_file/serde",
"ruff_python_ast/serde",
]
schemars = ["dep:schemars", "ruff_formatter/schemars"]
schemars = ["dep:schemars", "dep:serde_json", "ruff_formatter/schemars"]
[lints]
workspace = true

View file

@ -403,15 +403,12 @@ pub enum DocstringCodeLineWidth {
#[cfg(feature = "schemars")]
mod schema {
use ruff_formatter::LineWidth;
use schemars::r#gen::SchemaGenerator;
use schemars::schema::{Metadata, Schema, SubschemaValidation};
use schemars::{Schema, SchemaGenerator};
use serde_json::Value;
/// A dummy type that is used to generate a schema for `DocstringCodeLineWidth::Dynamic`.
pub(super) fn dynamic(_: &mut SchemaGenerator) -> Schema {
Schema::Object(schemars::schema::SchemaObject {
const_value: Some("dynamic".to_string().into()),
..Default::default()
})
schemars::json_schema!({ "const": "dynamic" })
}
// We use a manual schema for `fixed` even thought it isn't strictly necessary according to the
@ -422,19 +419,14 @@ mod schema {
// `allOf`. There's no semantic difference between `allOf` and `oneOf` for single element lists.
pub(super) fn fixed(generator: &mut SchemaGenerator) -> Schema {
let schema = generator.subschema_for::<LineWidth>();
Schema::Object(schemars::schema::SchemaObject {
metadata: Some(Box::new(Metadata {
description: Some(
"Wrap docstring code examples at a fixed line width.".to_string(),
),
..Metadata::default()
})),
subschemas: Some(Box::new(SubschemaValidation {
one_of: Some(vec![schema]),
..SubschemaValidation::default()
})),
..Default::default()
})
let mut schema_object = Schema::default();
let map = schema_object.ensure_object();
map.insert(
"description".to_string(),
Value::String("Wrap docstring code examples at a fixed line width.".to_string()),
);
map.insert("oneOf".to_string(), Value::Array(vec![schema.into()]));
schema_object
}
}