Skip to content

Commit 4dd13f2

Browse files
authored
Add support for schemars v0.9 (#849)
2 parents 038d8a6 + c9b08af commit 4dd13f2

37 files changed

+3390
-21
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
# It is good to test more than the MSRV and stable since sometimes
6161
# breakage occurs in intermediate versions.
6262
# IMPORTANT: Synchronize the MSRV with the Cargo.toml values.
63-
rust: ["1.71", "1.75", "1.80", "1.85", "stable", "beta", "nightly"]
63+
rust: ["1.74", "1.75", "1.80", "1.85", "stable", "beta", "nightly"]
6464
runs-on: ${{ matrix.os }}
6565
steps:
6666
- uses: actions/checkout@v4

Cargo.lock

Lines changed: 31 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
@@ -11,7 +11,7 @@ edition = "2021"
1111
license = "MIT OR Apache-2.0"
1212
readme = "README.md"
1313
repository = "https://github.com/jonasbb/serde_with/"
14-
rust-version = "1.71"
14+
rust-version = "1.74"
1515
version = "3.12.0"
1616

1717
[workspace.metadata.release]

serde_with/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
* Generalize `serde_with::rust::unwrap_or_skip` to support deserializing references by @beroal (#832)
1313
* Bump MSRV to 1.71, since that is required for the `jsonschema` dev-dependency.
1414
* Make `serde_conv` available without the `std` feature by @arilou (#839)
15+
* Bump MSRV to 1.74, since that is required for `schemars` v0.9.0 by @swlynch99 (#849)
1516

1617
### Fixed
1718

1819
* Make the `DurationSeconds` types and other variants more accessible even without `std` (#845)
1920

21+
### Added
22+
23+
* Added support for `schemars` v0.9.0 under the `schemars_0_9` feature flag by @swlynch99 (#849)
24+
2025
## [3.12.0] - 2024-12-25
2126

2227
### Added

serde_with/Cargo.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ alloc = ["serde/alloc", "base64?/alloc", "chrono_0_4?/alloc", "hex?/alloc", "ser
4141
## Enables support for various types from the std library.
4242
## This will enable `std` support in all dependencies too.
4343
## The feature enabled by default and also enables `alloc`.
44-
std = ["alloc", "serde/std", "chrono_0_4?/clock", "chrono_0_4?/std", "indexmap_1?/std", "indexmap_2?/std", "time_0_3?/serde-well-known", "time_0_3?/std"]
44+
std = ["alloc", "serde/std", "chrono_0_4?/clock", "chrono_0_4?/std", "indexmap_1?/std", "indexmap_2?/std", "time_0_3?/serde-well-known", "time_0_3?/std", "schemars_0_9?/std"]
4545

4646
#! # Documentation
4747
#!
@@ -118,6 +118,13 @@ macros = ["dep:serde_with_macros"]
118118
## This pulls in `schemars` v0.8 as a dependency. It will also implicitly enable
119119
## the `std` feature as `schemars` is not `#[no_std]`.
120120
schemars_0_8 = ["dep:schemars_0_8", "std", "serde_with_macros?/schemars_0_8"]
121+
## This feature enables integration with `schemars` 0.9
122+
## This makes `#[derive(JsonSchema)]` pick up the correct schema for the type
123+
## used within `#[serde_as(as = ...)]`.
124+
##
125+
## This pulls in `schemars` v0.9 as a dependency. It will also implicitly enable
126+
## the `alloc` feature.
127+
schemars_0_9 = ["dep:schemars_0_9", "alloc", "serde_with_macros?/schemars_0_9", "dep:serde_json"]
121128
## The feature enables integration of `time` v0.3 specific conversions.
122129
## This includes support for the timestamp and duration types.
123130
##
@@ -136,6 +143,7 @@ hex = { version = "0.4.3", optional = true, default-features = false }
136143
indexmap_1 = { package = "indexmap", version = "1.8", optional = true, default-features = false, features = ["serde-1"] }
137144
indexmap_2 = { package = "indexmap", version = "2.0", optional = true, default-features = false, features = ["serde"] }
138145
schemars_0_8 = { package = "schemars", version = "0.8.16", optional = true, default-features = false }
146+
schemars_0_9 = { package = "schemars", version = "0.9.0", optional = true, default-features = false }
139147
serde = { version = "1.0.152", default-features = false }
140148
serde_derive = "1.0.152"
141149
serde_json = { version = "1.0.45", optional = true, default-features = false }
@@ -154,6 +162,7 @@ rmp-serde = "1.3.0"
154162
ron = "0.10"
155163
rustversion = "1.0.0"
156164
schemars_0_8 = { package = "schemars", version = "0.8.16" }
165+
schemars_0_9 = { package = "schemars", version = "0.9.0" }
157166
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
158167
serde_json = { version = "1.0.25", features = ["preserve_order"] }
159168
serde_test = "1.0.124"
@@ -231,6 +240,11 @@ name = "schemars_0_8"
231240
path = "tests/schemars_0_8.rs"
232241
required-features = ["schemars_0_8"]
233242

243+
[[test]]
244+
name = "schemars_0_9"
245+
path = "tests/schemars_0_9/main.rs"
246+
required-features = ["schemars_0_9", "std"]
247+
234248
[package.metadata.docs.rs]
235249
all-features = true
236250
rustdoc-args = [

serde_with/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ pub mod rust;
312312
#[cfg(feature = "schemars_0_8")]
313313
#[cfg_attr(docsrs, doc(cfg(feature = "schemars_0_8")))]
314314
pub mod schemars_0_8;
315+
#[cfg(feature = "schemars_0_9")]
316+
#[cfg_attr(docsrs, doc(cfg(feature = "schemars_0_9")))]
317+
pub mod schemars_0_9;
315318
pub mod ser;
316319
mod serde_conv;
317320
#[cfg(feature = "time_0_3")]
@@ -2595,5 +2598,5 @@ pub struct SetLastValueWins<T>(PhantomData<T>);
25952598
/// feature is enabled.
25962599
///
25972600
/// [`JsonSchema`]: ::schemars_0_8::JsonSchema
2598-
#[cfg(feature = "schemars_0_8")]
2601+
#[cfg(any(feature = "schemars_0_8", feature = "schemars_0_9"))]
25992602
pub struct Schema<T: ?Sized, TA>(PhantomData<T>, PhantomData<TA>);

serde_with/src/schemars_0_8.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ impl JsonSchemaAs<Vec<u8>> for BytesOrString {
442442
"serde_with::BytesOrString".into()
443443
}
444444

445-
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
445+
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
446446
SchemaObject {
447447
subschemas: Some(Box::new(SubschemaValidation {
448448
any_of: Some(std::vec![
449-
gen.subschema_for::<Vec<u8>>(),
449+
generator.subschema_for::<Vec<u8>>(),
450450
SchemaObject {
451451
instance_type: Some(InstanceType::String.into()),
452452
metadata: Some(Box::new(Metadata {
@@ -607,13 +607,13 @@ where
607607
///
608608
/// Unfortunately for us, we need to handle all of these options by recursing
609609
/// into the subschemas and applying the same transformations as above.
610-
fn kvmap_transform_schema(gen: &mut SchemaGenerator, schema: &mut Schema) {
610+
fn kvmap_transform_schema_0_8(gen: &mut SchemaGenerator, schema: &mut Schema) {
611611
let mut parents = Vec::new();
612612

613-
Self::kvmap_transform_schema_impl(gen, schema, &mut parents, 0);
613+
Self::kvmap_transform_schema_impl_0_8(gen, schema, &mut parents, 0);
614614
}
615615

616-
fn kvmap_transform_schema_impl(
616+
fn kvmap_transform_schema_impl_0_8(
617617
gen: &mut SchemaGenerator,
618618
schema: &mut Schema,
619619
parents: &mut Vec<String>,
@@ -707,19 +707,19 @@ where
707707

708708
if let Some(one_of) = &mut subschemas.one_of {
709709
for subschema in one_of {
710-
Self::kvmap_transform_schema_impl(gen, subschema, &mut parents, depth + 1);
710+
Self::kvmap_transform_schema_impl_0_8(gen, subschema, &mut parents, depth + 1);
711711
}
712712
}
713713

714714
if let Some(any_of) = &mut subschemas.any_of {
715715
for subschema in any_of {
716-
Self::kvmap_transform_schema_impl(gen, subschema, &mut parents, depth + 1);
716+
Self::kvmap_transform_schema_impl_0_8(gen, subschema, &mut parents, depth + 1);
717717
}
718718
}
719719

720720
if let Some(all_of) = &mut subschemas.all_of {
721721
for subschema in all_of {
722-
Self::kvmap_transform_schema_impl(gen, subschema, &mut parents, depth + 1);
722+
Self::kvmap_transform_schema_impl_0_8(gen, subschema, &mut parents, depth + 1);
723723
}
724724
}
725725
}
@@ -743,7 +743,7 @@ where
743743

744744
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
745745
let mut value = <WrapSchema<T, TA>>::json_schema(gen);
746-
<WrapSchema<Vec<T>, KeyValueMap<TA>>>::kvmap_transform_schema(gen, &mut value);
746+
<WrapSchema<Vec<T>, KeyValueMap<TA>>>::kvmap_transform_schema_0_8(gen, &mut value);
747747

748748
SchemaObject {
749749
instance_type: Some(InstanceType::Object.into()),
@@ -919,12 +919,12 @@ macro_rules! schema_for_pickfirst {
919919
.into()
920920
}
921921

922-
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
922+
fn json_schema(g: &mut SchemaGenerator) -> Schema {
923923
let mut first = true;
924924
let subschemas = std::vec![$(
925925
{
926926
let is_first = std::mem::replace(&mut first, false);
927-
let schema = gen.subschema_for::<WrapSchema<T, $param>>();
927+
let schema = g.subschema_for::<WrapSchema<T, $param>>();
928928

929929
if !is_first {
930930
SchemaObject {

0 commit comments

Comments
 (0)