Skip to content

Add propertyNames field in maps #361

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions schemars/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl SchemaSettings {
Box::new(SetSingleExample),
Box::new(ReplaceConstValue),
Box::new(ReplacePrefixItems),
Box::new(RemovePropertyNames),
],
inline_subschemas: false,
contract: Contract::Deserialize,
Expand Down
2 changes: 1 addition & 1 deletion schemars/src/json_schema_impls/indexmap2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use crate::JsonSchema;
use alloc::collections::{BTreeMap, BTreeSet};
use indexmap2::{IndexMap, IndexSet};

forward_impl!((<K, V: JsonSchema, H> JsonSchema for IndexMap<K, V, H>) => BTreeMap<K, V>);
forward_impl!((<K: JsonSchema, V: JsonSchema, H> JsonSchema for IndexMap<K, V, H>) => BTreeMap<K, V>);
forward_impl!((<T: JsonSchema, H> JsonSchema for IndexSet<T, H>) => BTreeSet<T>);
2 changes: 2 additions & 0 deletions schemars/src/json_schema_impls/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ macro_rules! map_impl {
($($desc:tt)+) => {
impl $($desc)+
where
K: JsonSchema,
V: JsonSchema,
{
always_inline!();
Expand All @@ -22,6 +23,7 @@ macro_rules! map_impl {
json_schema!({
"type": "object",
"additionalProperties": generator.subschema_for::<V>(),
"propertyNames": generator.subschema_for::<K>(),
})
}
}
Expand Down
16 changes: 16 additions & 0 deletions schemars/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"type": "object",
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"type": "object",
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
},
"required": [
"tag"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"type": "object",
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"type": "object",
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"type": "object",
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
},
"required": [
"tag"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"type": "object",
"additionalProperties": {
"type": "string"
},
"propertyNames": {
"type": "string"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
},
"values": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"propertyNames": {
"type": "string"
}
},
"value": true,
"inner": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
},
"values": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"propertyNames": {
"type": "string"
}
},
"value": true,
"inner": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
},
"values": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"propertyNames": {
"type": "string"
}
},
"value": true,
"inner": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
},
"values": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"propertyNames": {
"type": "string"
}
},
"value": true,
"inner": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
},
"values": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"propertyNames": {
"type": "string"
}
},
"value": true,
"inner": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
},
"values": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"propertyNames": {
"type": "string"
}
},
"value": true,
"inner": {
Expand Down
Loading