diff --git a/serde_with/src/schemars_0_8.rs b/serde_with/src/schemars_0_8.rs index 55bb4a36..d861b57c 100644 --- a/serde_with/src/schemars_0_8.rs +++ b/serde_with/src/schemars_0_8.rs @@ -492,6 +492,15 @@ impl JsonSchemaAs for TryFromIntoRef { forward_schema!(U); } +impl JsonSchemaAs for IfIsHumanReadable +where + TA: JsonSchemaAs, +{ + // serde_json always has `is_human_readable` set to true so we just use the + // schema for the human readable variant. + forward_schema!(WrapSchema); +} + macro_rules! schema_for_map { ($type:ty) => { impl JsonSchemaAs<$type> for Map diff --git a/serde_with/tests/schemars_0_8.rs b/serde_with/tests/schemars_0_8.rs index 602c6324..f3b9fca2 100644 --- a/serde_with/tests/schemars_0_8.rs +++ b/serde_with/tests/schemars_0_8.rs @@ -705,6 +705,19 @@ fn test_map() { }); } +#[test] +fn test_if_is_human_readable() { + #[serde_as] + #[derive(Serialize, JsonSchema)] + struct Test { + #[serde_as(as = "IfIsHumanReadable")] + data: i32, + } + + check_valid_json_schema(&Test { data: 5 }); + check_matches_schema::(&json!({ "data": "5" })); +} + #[test] fn test_set_last_value_wins_with_duplicates() { #[serde_as]