From dee706a528cb37f853be5bd2b6b4c2b3605eab80 Mon Sep 17 00:00:00 2001 From: Sean Lynch <42618346+swlynch99@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:51:00 -0700 Subject: [PATCH] Implement JsonSchemaAs for OneOrMany instead of JsonSchema Currently, there is an `impl JsonSchema for WrapSchema>`. This works fine if you only directly need `OneOrMany` but causes errors if you want to nest it (e.g. `Option>`). This PR changes the impl to a `JsonSchemaAs` impl, which is the correct approach. I think I just missed these ones during a rebase. In any case, the change should be fully backwards compatible due to the blanket schema impl. I have added a snapshot test. The actual snapshot doesn't matter - the real test is that the struct below compiles #[serde_as] #[derive(Serialize, Deserialize, JsonSchema)] struct Test { #[serde_as(as = "Option>")] optional_vec: Option>, } --- serde_with/src/schemars_0_8.rs | 8 ++--- serde_with/tests/schemars_0_8.rs | 7 ++++ .../snapshots/one_or_many_nested.json | 33 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 serde_with/tests/schemars_0_8/snapshots/one_or_many_nested.json diff --git a/serde_with/src/schemars_0_8.rs b/serde_with/src/schemars_0_8.rs index 1696bd80..e7e1fcb5 100644 --- a/serde_with/src/schemars_0_8.rs +++ b/serde_with/src/schemars_0_8.rs @@ -794,9 +794,9 @@ map_first_last_wins_schema!(=> S indexmap_1::IndexMap); #[cfg(feature = "indexmap_2")] map_first_last_wins_schema!(=> S indexmap_2::IndexMap); -impl JsonSchema for WrapSchema, OneOrMany> +impl JsonSchemaAs> for OneOrMany where - WrapSchema: JsonSchema, + TA: JsonSchemaAs, { fn schema_name() -> String { std::format!( @@ -835,9 +835,9 @@ where } } -impl JsonSchema for WrapSchema, OneOrMany> +impl JsonSchemaAs> for OneOrMany where - WrapSchema: JsonSchema, + TA: JsonSchemaAs, { fn schema_name() -> String { std::format!( diff --git a/serde_with/tests/schemars_0_8.rs b/serde_with/tests/schemars_0_8.rs index 2c909c65..4237aa4b 100644 --- a/serde_with/tests/schemars_0_8.rs +++ b/serde_with/tests/schemars_0_8.rs @@ -406,6 +406,13 @@ mod snapshots { value: u32 } } + + one_or_many_nested { + struct Test { + #[serde_as(as = "Option>")] + optional_many: Option>, + } + } } } diff --git a/serde_with/tests/schemars_0_8/snapshots/one_or_many_nested.json b/serde_with/tests/schemars_0_8/snapshots/one_or_many_nested.json new file mode 100644 index 00000000..67c4ce91 --- /dev/null +++ b/serde_with/tests/schemars_0_8/snapshots/one_or_many_nested.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Test", + "type": "object", + "properties": { + "optional_many": { + "default": null, + "anyOf": [ + { + "$ref": "#/definitions/OneOrMany" + }, + { + "type": "null" + } + ] + } + }, + "definitions": { + "OneOrMany": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "string" + } + } + ] + } + } +}