Skip to content

Commit

Permalink
Add JsonSchemaAs impl for IfIsHumanReadable
Browse files Browse the repository at this point in the history
  • Loading branch information
swlynch99 committed Mar 8, 2024
1 parent 50febe0 commit 395d896
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions serde_with/src/schemars_0_8.rs
Expand Up @@ -492,6 +492,15 @@ impl<T, U: JsonSchema> JsonSchemaAs<T> for TryFromIntoRef<U> {
forward_schema!(U);
}

impl<T, TA, FA> JsonSchemaAs<T> for IfIsHumanReadable<TA, FA>
where
TA: JsonSchemaAs<T>,
{
// serde_json always has `is_human_readable` set to true so we just use the
// schema for the human readable variant.
forward_schema!(WrapSchema<T, TA>);
}

macro_rules! schema_for_map {
($type:ty) => {
impl<K, V, KA, VA> JsonSchemaAs<$type> for Map<KA, VA>
Expand Down
13 changes: 13 additions & 0 deletions serde_with/tests/schemars_0_8.rs
Expand Up @@ -705,6 +705,19 @@ fn test_map() {
});
}

#[test]
fn test_if_human_readable() {
#[serde_as]
#[derive(Serialize, JsonSchema)]
struct Test {
#[serde_as(as = "IfIsHumanReadable<DisplayFromStr>")]
data: i32,
}

check_valid_json_schema(&Test { data: 5 });
check_matches_schema::<Test>(&json!({ "data": "5" }));
}

#[test]
fn test_set_last_value_wins_with_duplicates() {
#[serde_as]
Expand Down

0 comments on commit 395d896

Please sign in to comment.