From bc7c7d6a0c030177f3e5c0bcabf99d341e4f0e54 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Tue, 31 Dec 2024 00:55:22 +0100 Subject: [PATCH] Add propertyNames in maps --- schemars/src/generate.rs | 1 + schemars/src/json_schema_impls/indexmap2.rs | 2 +- schemars/src/json_schema_impls/maps.rs | 2 ++ schemars/src/transform.rs | 16 ++++++++++++++++ .../enums.rs~adjacently_tagged_enum.json | 3 +++ .../enums.rs~externally_tagged_enum.json | 3 +++ .../enums.rs~internally_tagged_enum.json | 3 +++ .../integration/enums.rs~untagged_enum.json | 3 +++ ...unknown_fields.rs~adjacently_tagged_enum.json | 3 +++ ...unknown_fields.rs~externally_tagged_enum.json | 3 +++ ...unknown_fields.rs~internally_tagged_enum.json | 3 +++ ...ums_deny_unknown_fields.rs~untagged_enum.json | 3 +++ .../integration/settings.rs~draft07.de.json | 5 ++++- .../integration/settings.rs~draft07.ser.json | 5 ++++- .../integration/settings.rs~draft2019_09.de.json | 5 ++++- .../settings.rs~draft2019_09.ser.json | 5 ++++- .../integration/settings.rs~draft2020_12.de.json | 5 ++++- .../settings.rs~draft2020_12.ser.json | 5 ++++- 18 files changed, 68 insertions(+), 7 deletions(-) diff --git a/schemars/src/generate.rs b/schemars/src/generate.rs index 375fa08d..ebb75397 100644 --- a/schemars/src/generate.rs +++ b/schemars/src/generate.rs @@ -133,6 +133,7 @@ impl SchemaSettings { Box::new(SetSingleExample), Box::new(ReplaceConstValue), Box::new(ReplacePrefixItems), + Box::new(RemovePropertyNames), ], inline_subschemas: false, contract: Contract::Deserialize, diff --git a/schemars/src/json_schema_impls/indexmap2.rs b/schemars/src/json_schema_impls/indexmap2.rs index 355864eb..a0ff6064 100644 --- a/schemars/src/json_schema_impls/indexmap2.rs +++ b/schemars/src/json_schema_impls/indexmap2.rs @@ -2,5 +2,5 @@ use crate::JsonSchema; use alloc::collections::{BTreeMap, BTreeSet}; use indexmap2::{IndexMap, IndexSet}; -forward_impl!(( JsonSchema for IndexMap) => BTreeMap); +forward_impl!(( JsonSchema for IndexMap) => BTreeMap); forward_impl!(( JsonSchema for IndexSet) => BTreeSet); diff --git a/schemars/src/json_schema_impls/maps.rs b/schemars/src/json_schema_impls/maps.rs index 86d43c8b..e1e51b65 100644 --- a/schemars/src/json_schema_impls/maps.rs +++ b/schemars/src/json_schema_impls/maps.rs @@ -6,6 +6,7 @@ macro_rules! map_impl { ($($desc:tt)+) => { impl $($desc)+ where + K: JsonSchema, V: JsonSchema, { always_inline!(); @@ -22,6 +23,7 @@ macro_rules! map_impl { json_schema!({ "type": "object", "additionalProperties": generator.subschema_for::(), + "propertyNames": generator.subschema_for::(), }) } } diff --git a/schemars/src/transform.rs b/schemars/src/transform.rs index 7d24b5d6..2288d7e0 100644 --- a/schemars/src/transform.rs +++ b/schemars/src/transform.rs @@ -448,3 +448,19 @@ impl Transform for GatherPropertyNames { transform_immediate_subschemas(self, schema); } } + +/// Removes the `propertyNames` field from JSON Schema objects. This also applies to subschemas. +/// +/// This is useful for OpenAPI 3.0, which does not support the `propertyNames` field. +#[derive(Debug, Clone)] +pub struct RemovePropertyNames; + +impl Transform for RemovePropertyNames { + fn transform(&mut self, schema: &mut Schema) { + transform_subschemas(self, schema); + + if let Some(obj) = schema.as_object_mut() { + obj.remove("propertyNames"); + } + } +} \ No newline at end of file diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~adjacently_tagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~adjacently_tagged_enum.json index e75faf26..879b4506 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~adjacently_tagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~adjacently_tagged_enum.json @@ -25,6 +25,9 @@ "type": "object", "additionalProperties": { "type": "string" + }, + "propertyNames": { + "type": "string" } } }, diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~externally_tagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~externally_tagged_enum.json index c5e19aa4..15f6bbb4 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~externally_tagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~externally_tagged_enum.json @@ -16,6 +16,9 @@ "type": "object", "additionalProperties": { "type": "string" + }, + "propertyNames": { + "type": "string" } } }, diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~internally_tagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~internally_tagged_enum.json index 219be7ae..ee6696e4 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~internally_tagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~internally_tagged_enum.json @@ -25,6 +25,9 @@ "additionalProperties": { "type": "string" }, + "propertyNames": { + "type": "string" + }, "required": [ "tag" ] diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~untagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~untagged_enum.json index e744d555..d416ed72 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~untagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums.rs~untagged_enum.json @@ -9,6 +9,9 @@ "type": "object", "additionalProperties": { "type": "string" + }, + "propertyNames": { + "type": "string" } }, { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.json index 9c4b6d9b..6713ab91 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.json @@ -26,6 +26,9 @@ "type": "object", "additionalProperties": { "type": "string" + }, + "propertyNames": { + "type": "string" } } }, diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.json index 911428ba..ddd3aceb 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.json @@ -15,6 +15,9 @@ "type": "object", "additionalProperties": { "type": "string" + }, + "propertyNames": { + "type": "string" } } }, diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.json index ab5e53a8..0dc025a0 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.json @@ -26,6 +26,9 @@ "additionalProperties": { "type": "string" }, + "propertyNames": { + "type": "string" + }, "required": [ "tag" ] diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.json index 1e6a3671..7e5fa42a 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.json @@ -9,6 +9,9 @@ "type": "object", "additionalProperties": { "type": "string" + }, + "propertyNames": { + "type": "string" } }, { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json index a6b613eb..8988ae6c 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.de.json @@ -16,7 +16,10 @@ }, "values": { "type": "object", - "additionalProperties": true + "additionalProperties": true, + "propertyNames": { + "type": "string" + } }, "value": true, "inner": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json index d76d9902..0500e52e 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.ser.json @@ -16,7 +16,10 @@ }, "values": { "type": "object", - "additionalProperties": true + "additionalProperties": true, + "propertyNames": { + "type": "string" + } }, "value": true, "inner": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json index f7047553..4469e48d 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.de.json @@ -16,7 +16,10 @@ }, "values": { "type": "object", - "additionalProperties": true + "additionalProperties": true, + "propertyNames": { + "type": "string" + } }, "value": true, "inner": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json index 41c32376..3b461f9e 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.ser.json @@ -16,7 +16,10 @@ }, "values": { "type": "object", - "additionalProperties": true + "additionalProperties": true, + "propertyNames": { + "type": "string" + } }, "value": true, "inner": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json index 1f511612..3f416a7c 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.de.json @@ -16,7 +16,10 @@ }, "values": { "type": "object", - "additionalProperties": true + "additionalProperties": true, + "propertyNames": { + "type": "string" + } }, "value": true, "inner": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json index a200584a..33ca18e4 100644 --- a/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2020_12.ser.json @@ -16,7 +16,10 @@ }, "values": { "type": "object", - "additionalProperties": true + "additionalProperties": true, + "propertyNames": { + "type": "string" + } }, "value": true, "inner": {