Skip to content

Commit 8869b20

Browse files
authored
Do not emit schemars(deserialize_with = "...") (#736)
2 parents 7e66d6b + eb77c40 commit 8869b20

File tree

4 files changed

+108
-34
lines changed

4 files changed

+108
-34
lines changed

serde_with/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
* Do not emit `schemars(deserialize_with = "...")` annotations, as `schemars` does not support them (#735)
13+
Thanks to @sivizius for reporting the issue.
14+
1015
## [3.8.0] - 2024-04-24
1116

1217
### Added

serde_with/tests/schemars_0_8.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,44 @@ fn schemars_custom_with() {
117117
}));
118118
}
119119

120+
#[test]
121+
fn schemars_deserialize_only_bug_735() {
122+
#[serde_as]
123+
#[derive(JsonSchema, Serialize)]
124+
#[schemars(crate = "::schemars_0_8")]
125+
struct Basic {
126+
/// Basic field, no attribute
127+
bare_field: u32,
128+
129+
/// Will emit matching schemars attribute
130+
#[serde_as(as = "PickFirst<(_, DisplayFromStr)>")]
131+
both: u32,
132+
133+
/// Can emit schemars with serialize_as, but it will be ignored
134+
#[serde_as(serialize_as = "PickFirst<(_, DisplayFromStr)>")]
135+
serialize_only: u32,
136+
137+
/// schemars doesn't support deserialize_as
138+
#[serde_as(deserialize_as = "PickFirst<(_, DisplayFromStr)>")]
139+
deserialize_only: u32,
140+
141+
/// Can emit schemars with serialize_as, but it will be ignored
142+
/// schemars doesn't support deserialize_as
143+
#[serde_as(
144+
serialize_as = "PickFirst<(_, DisplayFromStr)>",
145+
deserialize_as = "PickFirst<(_, DisplayFromStr)>"
146+
)]
147+
serialize_and_deserialize: u32,
148+
}
149+
150+
let schema = schemars::schema_for!(Basic);
151+
let mut schema = serde_json::to_string_pretty(&schema).expect("schema could not be serialized");
152+
schema.push('\n');
153+
154+
let expected = expect_file!["./schemars_0_8/schemars_deserialize_only_bug_735.json"];
155+
expected.assert_eq(&schema);
156+
}
157+
120158
#[test]
121159
fn schemars_custom_schema_with() {
122160
fn custom_int(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Basic",
4+
"type": "object",
5+
"required": [
6+
"bare_field",
7+
"both",
8+
"deserialize_only",
9+
"serialize_and_deserialize",
10+
"serialize_only"
11+
],
12+
"properties": {
13+
"bare_field": {
14+
"description": "Basic field, no attribute",
15+
"type": "integer",
16+
"format": "uint32",
17+
"minimum": 0.0
18+
},
19+
"both": {
20+
"description": "Will emit matching schemars attribute",
21+
"allOf": [
22+
{
23+
"$ref": "#/definitions/PickFirst<(uint32String)>"
24+
}
25+
]
26+
},
27+
"deserialize_only": {
28+
"description": "schemars doesn't support deserialize_as",
29+
"type": "integer",
30+
"format": "uint32",
31+
"minimum": 0.0
32+
},
33+
"serialize_and_deserialize": {
34+
"description": "Can emit schemars with serialize_as, but it will be ignored schemars doesn't support deserialize_as",
35+
"type": "integer",
36+
"format": "uint32",
37+
"minimum": 0.0
38+
},
39+
"serialize_only": {
40+
"description": "Can emit schemars with serialize_as, but it will be ignored",
41+
"type": "integer",
42+
"format": "uint32",
43+
"minimum": 0.0
44+
}
45+
},
46+
"definitions": {
47+
"PickFirst<(uint32String)>": {
48+
"anyOf": [
49+
{
50+
"type": "integer",
51+
"format": "uint32",
52+
"minimum": 0.0
53+
},
54+
{
55+
"writeOnly": true,
56+
"allOf": [
57+
{
58+
"type": "string"
59+
}
60+
]
61+
}
62+
]
63+
}
64+
}
65+
}

serde_with_macros/src/lib.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -799,47 +799,13 @@ fn serde_as_add_attr_to_field(
799799
quote!(#serde_with_crate_path::As::<#replacement_type>::deserialize).to_string();
800800
let attr = parse_quote!(#[serde(deserialize_with = #attr_inner_tokens)]);
801801
field.attrs.push(attr);
802-
803-
if let Some(cfg) = schemars_config.cfg_expr() {
804-
let with_cfg = utils::schemars_with_attr_if(
805-
&field.attrs,
806-
&["with", "deserialize_with", "schema_with"],
807-
)?;
808-
let attr_inner_tokens =
809-
quote!(#serde_with_crate_path::Schema::<#type_original, #replacement_type>::deserialize)
810-
.to_string();
811-
let attr = parse_quote! {
812-
#[cfg_attr(
813-
all(#cfg, not(#with_cfg)),
814-
schemars(deserialize_with = #attr_inner_tokens))
815-
]
816-
};
817-
field.attrs.push(attr);
818-
}
819802
}
820803
if let Some(type_) = serde_as_options.serialize_as {
821804
let replacement_type = replace_infer_type_with_type(type_.clone(), type_same);
822805
let attr_inner_tokens =
823806
quote!(#serde_with_crate_path::As::<#replacement_type>::serialize).to_string();
824807
let attr = parse_quote!(#[serde(serialize_with = #attr_inner_tokens)]);
825808
field.attrs.push(attr);
826-
827-
if let Some(cfg) = schemars_config.cfg_expr() {
828-
let with_cfg = utils::schemars_with_attr_if(
829-
&field.attrs,
830-
&["with", "serialize_with", "schema_with"],
831-
)?;
832-
let attr_inner_tokens =
833-
quote!(#serde_with_crate_path::Schema::<#type_original, #replacement_type>::serialize)
834-
.to_string();
835-
let attr = parse_quote! {
836-
#[cfg_attr(
837-
all(#cfg, not(#with_cfg)),
838-
schemars(serialize_with = #attr_inner_tokens))
839-
]
840-
};
841-
field.attrs.push(attr);
842-
}
843809
}
844810

845811
Ok(())

0 commit comments

Comments
 (0)