Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonSchemaAs impl for IfIsHumanReadable #717

Merged
merged 1 commit into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_is_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