From ec2a485fba870c80dfac4c51c9243a801572cd5b Mon Sep 17 00:00:00 2001 From: Andreas Walter Date: Mon, 4 Nov 2024 11:22:54 +0000 Subject: [PATCH] Avoid characters in schema name that are invalid as ref --- serde_with/src/schemars_0_8.rs | 20 +++++------ serde_with/tests/schemars_0_8.rs | 8 +++++ .../schemars_deserialize_only_bug_735.json | 4 +-- .../schemars_0_8/snapshots/enum_map.json | 4 +-- .../schemars_0_8/snapshots/key_value_map.json | 4 +-- .../snapshots/key_value_map_enum.json | 4 +-- .../snapshots/key_value_map_flatten.json | 4 +-- .../snapshots/one_or_many_nested.json | 4 +-- .../snapshots/one_or_many_prefer_one.json | 2 +- .../schemars_0_8/snapshots/pickfirst.json | 2 +- .../snapshots/pickfirst_nested.json | 34 +++++++++++++++++++ 11 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 serde_with/tests/schemars_0_8/snapshots/pickfirst_nested.json diff --git a/serde_with/src/schemars_0_8.rs b/serde_with/src/schemars_0_8.rs index 0864c4eb..30547a71 100644 --- a/serde_with/src/schemars_0_8.rs +++ b/serde_with/src/schemars_0_8.rs @@ -536,11 +536,11 @@ where T: JsonSchema, { fn schema_name() -> String { - std::format!("EnumMap<{}>", T::schema_name()) + std::format!("EnumMap({})", T::schema_name()) } fn schema_id() -> Cow<'static, str> { - std::format!("serde_with::EnumMap<{}>", T::schema_id()).into() + std::format!("serde_with::EnumMap({})", T::schema_id()).into() } // We generate the schema here by going through all the variants of the @@ -727,12 +727,12 @@ where TA: JsonSchemaAs, { fn schema_name() -> String { - std::format!("KeyValueMap<{}>", >::schema_name()) + std::format!("KeyValueMap({})", >::schema_name()) } fn schema_id() -> Cow<'static, str> { std::format!( - "serde_with::KeyValueMap<{}>", + "serde_with::KeyValueMap({})", >::schema_id() ) .into() @@ -800,14 +800,14 @@ where { fn schema_name() -> String { std::format!( - "OneOrMany<{}, PreferOne>", + "OneOrMany({},PreferOne)", >::schema_name() ) } fn schema_id() -> Cow<'static, str> { std::format!( - "serde_with::OneOrMany<{}, PreferOne>", + "serde_with::OneOrMany({},PreferOne)", >::schema_id() ) .into() @@ -896,9 +896,9 @@ macro_rules! schema_for_pickfirst { fn schema_name() -> String { std::format!( concat!( - "PickFirst<(", + "PickFirst(", $( "{", stringify!($param), "}", )+ - ")>" + ")" ), $( $param = >::schema_name(), )+ ) @@ -907,9 +907,9 @@ macro_rules! schema_for_pickfirst { fn schema_id() -> Cow<'static, str> { std::format!( concat!( - "serde_with::PickFirst<(", + "serde_with::PickFirst(", $( "{", stringify!($param), "}", )+ - ")>" + ")" ), $( $param = >::schema_id(), )+ ) diff --git a/serde_with/tests/schemars_0_8.rs b/serde_with/tests/schemars_0_8.rs index a9cba6ed..d110abd8 100644 --- a/serde_with/tests/schemars_0_8.rs +++ b/serde_with/tests/schemars_0_8.rs @@ -430,6 +430,14 @@ mod snapshots { } } + pickfirst_nested { + #[serde(transparent)] + struct Test { + #[serde_as(as = "OneOrMany>")] + optional_value: Vec + } + } + one_or_many_nested { struct Test { #[serde_as(as = "Option>")] diff --git a/serde_with/tests/schemars_0_8/schemars_deserialize_only_bug_735.json b/serde_with/tests/schemars_0_8/schemars_deserialize_only_bug_735.json index 8ad0d16a..f318b342 100644 --- a/serde_with/tests/schemars_0_8/schemars_deserialize_only_bug_735.json +++ b/serde_with/tests/schemars_0_8/schemars_deserialize_only_bug_735.json @@ -20,7 +20,7 @@ "description": "Will emit matching schemars attribute", "allOf": [ { - "$ref": "#/definitions/PickFirst<(uint32String)>" + "$ref": "#/definitions/PickFirst(uint32String)" } ] }, @@ -44,7 +44,7 @@ } }, "definitions": { - "PickFirst<(uint32String)>": { + "PickFirst(uint32String)": { "anyOf": [ { "type": "integer", diff --git a/serde_with/tests/schemars_0_8/snapshots/enum_map.json b/serde_with/tests/schemars_0_8/snapshots/enum_map.json index 9a8d428c..f8eeb3b5 100644 --- a/serde_with/tests/schemars_0_8/snapshots/enum_map.json +++ b/serde_with/tests/schemars_0_8/snapshots/enum_map.json @@ -7,11 +7,11 @@ ], "properties": { "data": { - "$ref": "#/definitions/EnumMap" + "$ref": "#/definitions/EnumMap(Mappable)" } }, "definitions": { - "EnumMap": { + "EnumMap(Mappable)": { "type": "object", "properties": { "A": { diff --git a/serde_with/tests/schemars_0_8/snapshots/key_value_map.json b/serde_with/tests/schemars_0_8/snapshots/key_value_map.json index e232adc5..63eee02a 100644 --- a/serde_with/tests/schemars_0_8/snapshots/key_value_map.json +++ b/serde_with/tests/schemars_0_8/snapshots/key_value_map.json @@ -7,11 +7,11 @@ ], "properties": { "data": { - "$ref": "#/definitions/KeyValueMap" + "$ref": "#/definitions/KeyValueMap(KvMapData)" } }, "definitions": { - "KeyValueMap": { + "KeyValueMap(KvMapData)": { "type": "object", "additionalProperties": { "type": "object", diff --git a/serde_with/tests/schemars_0_8/snapshots/key_value_map_enum.json b/serde_with/tests/schemars_0_8/snapshots/key_value_map_enum.json index d4a60070..c6474db9 100644 --- a/serde_with/tests/schemars_0_8/snapshots/key_value_map_enum.json +++ b/serde_with/tests/schemars_0_8/snapshots/key_value_map_enum.json @@ -7,11 +7,11 @@ ], "properties": { "data": { - "$ref": "#/definitions/KeyValueMap" + "$ref": "#/definitions/KeyValueMap(KvMapEnum)" } }, "definitions": { - "KeyValueMap": { + "KeyValueMap(KvMapEnum)": { "type": "object", "additionalProperties": { "oneOf": [ diff --git a/serde_with/tests/schemars_0_8/snapshots/key_value_map_flatten.json b/serde_with/tests/schemars_0_8/snapshots/key_value_map_flatten.json index a94120d9..74ee495f 100644 --- a/serde_with/tests/schemars_0_8/snapshots/key_value_map_flatten.json +++ b/serde_with/tests/schemars_0_8/snapshots/key_value_map_flatten.json @@ -7,11 +7,11 @@ ], "properties": { "data": { - "$ref": "#/definitions/KeyValueMap" + "$ref": "#/definitions/KeyValueMap(KvMapFlatten)" } }, "definitions": { - "KeyValueMap": { + "KeyValueMap(KvMapFlatten)": { "type": "object", "additionalProperties": { "type": "object", 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 index 67c4ce91..31f845f4 100644 --- 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 @@ -7,7 +7,7 @@ "default": null, "anyOf": [ { - "$ref": "#/definitions/OneOrMany" + "$ref": "#/definitions/OneOrMany(String,PreferOne)" }, { "type": "null" @@ -16,7 +16,7 @@ } }, "definitions": { - "OneOrMany": { + "OneOrMany(String,PreferOne)": { "anyOf": [ { "type": "string" diff --git a/serde_with/tests/schemars_0_8/snapshots/one_or_many_prefer_one.json b/serde_with/tests/schemars_0_8/snapshots/one_or_many_prefer_one.json index b9aa247a..84466425 100644 --- a/serde_with/tests/schemars_0_8/snapshots/one_or_many_prefer_one.json +++ b/serde_with/tests/schemars_0_8/snapshots/one_or_many_prefer_one.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "OneOrMany", + "title": "OneOrMany(int32,PreferOne)", "anyOf": [ { "type": "integer", diff --git a/serde_with/tests/schemars_0_8/snapshots/pickfirst.json b/serde_with/tests/schemars_0_8/snapshots/pickfirst.json index 766bcaef..af5006c1 100644 --- a/serde_with/tests/schemars_0_8/snapshots/pickfirst.json +++ b/serde_with/tests/schemars_0_8/snapshots/pickfirst.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "PickFirst<(uint32String)>", + "title": "PickFirst(uint32String)", "anyOf": [ { "type": "integer", diff --git a/serde_with/tests/schemars_0_8/snapshots/pickfirst_nested.json b/serde_with/tests/schemars_0_8/snapshots/pickfirst_nested.json new file mode 100644 index 00000000..a429e29b --- /dev/null +++ b/serde_with/tests/schemars_0_8/snapshots/pickfirst_nested.json @@ -0,0 +1,34 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "OneOrMany(PickFirst(uint32String),PreferOne)", + "anyOf": [ + { + "$ref": "#/definitions/PickFirst(uint32String)" + }, + { + "type": "array", + "items": { + "$ref": "#/definitions/PickFirst(uint32String)" + } + } + ], + "definitions": { + "PickFirst(uint32String)": { + "anyOf": [ + { + "type": "integer", + "format": "uint32", + "minimum": 0.0 + }, + { + "writeOnly": true, + "allOf": [ + { + "type": "string" + } + ] + } + ] + } + } +}