Skip to content

Commit 48b5012

Browse files
authored
[ruff] Update schemars to v1 (#20942)
1 parent 991e8ed commit 48b5012

File tree

22 files changed

+689
-707
lines changed

22 files changed

+689
-707
lines changed

Cargo.lock

Lines changed: 29 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "ef9f9329be6923ac
152152
"salsa_unstable",
153153
"inventory",
154154
] }
155-
schemars = { version = "0.8.16" }
155+
schemars = { version = "1.0.4" }
156156
seahash = { version = "4.1.0" }
157157
serde = { version = "1.0.197", features = ["derive"] }
158158
serde-wasm-bindgen = { version = "0.6.4" }

crates/ruff_dev/src/generate_json_schema.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33

44
use anyhow::{Result, bail};
55
use pretty_assertions::StrComparison;
6-
use schemars::schema_for;
6+
use schemars::generate::SchemaSettings;
77

88
use crate::ROOT_DIR;
99
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
@@ -17,7 +17,9 @@ pub(crate) struct Args {
1717
}
1818

1919
pub(crate) fn main(args: &Args) -> Result<()> {
20-
let schema = schema_for!(Options);
20+
let settings = SchemaSettings::draft07();
21+
let generator = settings.into_generator();
22+
let schema = generator.into_root_schema_for::<Options>();
2123
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
2224
let filename = "ruff.schema.json";
2325
let schema_path = PathBuf::from(ROOT_DIR).join(filename);

crates/ruff_dev/src/generate_ty_schema.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33

44
use anyhow::{Result, bail};
55
use pretty_assertions::StrComparison;
6-
use schemars::schema_for;
6+
use schemars::generate::SchemaSettings;
77

88
use crate::ROOT_DIR;
99
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
@@ -17,7 +17,9 @@ pub(crate) struct Args {
1717
}
1818

1919
pub(crate) fn main(args: &Args) -> Result<()> {
20-
let schema = schema_for!(Options);
20+
let settings = SchemaSettings::draft07();
21+
let generator = settings.into_generator();
22+
let schema = generator.into_root_schema_for::<Options>();
2123
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
2224
let filename = "ty.schema.json";
2325
let schema_path = PathBuf::from(ROOT_DIR).join(filename);

crates/ruff_linter/src/rule_selector.rs

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -257,74 +257,74 @@ pub struct PreviewOptions {
257257
#[cfg(feature = "schemars")]
258258
mod schema {
259259
use itertools::Itertools;
260-
use schemars::_serde_json::Value;
261-
use schemars::JsonSchema;
262-
use schemars::schema::{InstanceType, Schema, SchemaObject};
260+
use schemars::{JsonSchema, Schema, SchemaGenerator};
261+
use serde_json::Value;
263262
use strum::IntoEnumIterator;
264263

265264
use crate::RuleSelector;
266265
use crate::registry::RuleNamespace;
267266
use crate::rule_selector::{Linter, RuleCodePrefix};
268267

269268
impl JsonSchema for RuleSelector {
270-
fn schema_name() -> String {
271-
"RuleSelector".to_string()
269+
fn schema_name() -> std::borrow::Cow<'static, str> {
270+
std::borrow::Cow::Borrowed("RuleSelector")
272271
}
273272

274-
fn json_schema(_gen: &mut schemars::r#gen::SchemaGenerator) -> Schema {
275-
Schema::Object(SchemaObject {
276-
instance_type: Some(InstanceType::String.into()),
277-
enum_values: Some(
278-
[
279-
// Include the non-standard "ALL" selectors.
280-
"ALL".to_string(),
281-
// Include the legacy "C" and "T" selectors.
282-
"C".to_string(),
283-
"T".to_string(),
284-
// Include some common redirect targets for those legacy selectors.
285-
"C9".to_string(),
286-
"T1".to_string(),
287-
"T2".to_string(),
288-
]
289-
.into_iter()
290-
.chain(
291-
RuleCodePrefix::iter()
292-
.map(|p| {
293-
let prefix = p.linter().common_prefix();
294-
let code = p.short_code();
295-
format!("{prefix}{code}")
296-
})
297-
.chain(Linter::iter().filter_map(|l| {
298-
let prefix = l.common_prefix();
299-
(!prefix.is_empty()).then(|| prefix.to_string())
300-
})),
301-
)
302-
.filter(|p| {
303-
// Exclude any prefixes where all of the rules are removed
304-
if let Ok(Self::Rule { prefix, .. } | Self::Prefix { prefix, .. }) =
305-
RuleSelector::parse_no_redirect(p)
306-
{
307-
!prefix.rules().all(|rule| rule.is_removed())
308-
} else {
309-
true
310-
}
273+
fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
274+
let enum_values: Vec<String> = [
275+
// Include the non-standard "ALL" selectors.
276+
"ALL".to_string(),
277+
// Include the legacy "C" and "T" selectors.
278+
"C".to_string(),
279+
"T".to_string(),
280+
// Include some common redirect targets for those legacy selectors.
281+
"C9".to_string(),
282+
"T1".to_string(),
283+
"T2".to_string(),
284+
]
285+
.into_iter()
286+
.chain(
287+
RuleCodePrefix::iter()
288+
.map(|p| {
289+
let prefix = p.linter().common_prefix();
290+
let code = p.short_code();
291+
format!("{prefix}{code}")
311292
})
312-
.filter(|_rule| {
313-
// Filter out all test-only rules
314-
#[cfg(any(feature = "test-rules", test))]
315-
#[expect(clippy::used_underscore_binding)]
316-
if _rule.starts_with("RUF9") || _rule == "PLW0101" {
317-
return false;
318-
}
319-
320-
true
321-
})
322-
.sorted()
323-
.map(Value::String)
324-
.collect(),
325-
),
326-
..SchemaObject::default()
293+
.chain(Linter::iter().filter_map(|l| {
294+
let prefix = l.common_prefix();
295+
(!prefix.is_empty()).then(|| prefix.to_string())
296+
})),
297+
)
298+
.filter(|p| {
299+
// Exclude any prefixes where all of the rules are removed
300+
if let Ok(Self::Rule { prefix, .. } | Self::Prefix { prefix, .. }) =
301+
RuleSelector::parse_no_redirect(p)
302+
{
303+
!prefix.rules().all(|rule| rule.is_removed())
304+
} else {
305+
true
306+
}
327307
})
308+
.filter(|_rule| {
309+
// Filter out all test-only rules
310+
#[cfg(any(feature = "test-rules", test))]
311+
#[expect(clippy::used_underscore_binding)]
312+
if _rule.starts_with("RUF9") || _rule == "PLW0101" {
313+
return false;
314+
}
315+
316+
true
317+
})
318+
.sorted()
319+
.collect();
320+
321+
let mut schema = schemars::json_schema!({ "type": "string" });
322+
schema.ensure_object().insert(
323+
"enum".to_string(),
324+
Value::Array(enum_values.into_iter().map(Value::String).collect()),
325+
);
326+
327+
schema
328328
}
329329
}
330330
}

crates/ruff_linter/src/settings/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,12 +617,12 @@ impl TryFrom<String> for RequiredVersion {
617617

618618
#[cfg(feature = "schemars")]
619619
impl schemars::JsonSchema for RequiredVersion {
620-
fn schema_name() -> String {
621-
"RequiredVersion".to_string()
620+
fn schema_name() -> std::borrow::Cow<'static, str> {
621+
std::borrow::Cow::Borrowed("RequiredVersion")
622622
}
623623

624-
fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
625-
generator.subschema_for::<String>()
624+
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
625+
<String as schemars::JsonSchema>::json_schema(generator)
626626
}
627627
}
628628

crates/ruff_python_ast/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ rustc-hash = { workspace = true }
3030
salsa = { workspace = true, optional = true }
3131
schemars = { workspace = true, optional = true }
3232
serde = { workspace = true, optional = true }
33+
serde_json = { workspace = true, optional = true }
3334
thiserror = { workspace = true }
3435

3536
[features]
36-
schemars = ["dep:schemars"]
37+
schemars = ["dep:schemars", "dep:serde_json"]
3738
cache = ["dep:ruff_cache", "dep:ruff_macros"]
3839
serde = [
3940
"dep:serde",

crates/ruff_python_ast/src/name.rs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ use crate::generated::ExprName;
1111
#[cfg_attr(feature = "cache", derive(ruff_macros::CacheKey))]
1212
#[cfg_attr(feature = "salsa", derive(salsa::Update))]
1313
#[cfg_attr(feature = "get-size", derive(get_size2::GetSize))]
14+
#[cfg_attr(
15+
feature = "schemars",
16+
derive(schemars::JsonSchema),
17+
schemars(with = "String")
18+
)]
1419
pub struct Name(compact_str::CompactString);
1520

1621
impl Name {
@@ -201,35 +206,6 @@ impl PartialEq<Name> for &String {
201206
}
202207
}
203208

204-
#[cfg(feature = "schemars")]
205-
impl schemars::JsonSchema for Name {
206-
fn is_referenceable() -> bool {
207-
String::is_referenceable()
208-
}
209-
210-
fn schema_name() -> String {
211-
String::schema_name()
212-
}
213-
214-
fn schema_id() -> std::borrow::Cow<'static, str> {
215-
String::schema_id()
216-
}
217-
218-
fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
219-
String::json_schema(generator)
220-
}
221-
222-
fn _schemars_private_non_optional_json_schema(
223-
generator: &mut schemars::r#gen::SchemaGenerator,
224-
) -> schemars::schema::Schema {
225-
String::_schemars_private_non_optional_json_schema(generator)
226-
}
227-
228-
fn _schemars_private_is_option() -> bool {
229-
String::_schemars_private_is_option()
230-
}
231-
}
232-
233209
/// A representation of a qualified name, like `typing.List`.
234210
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
235211
pub struct QualifiedName<'a>(SegmentsVec<'a>);

0 commit comments

Comments
 (0)