fix: honor rename_all_fields, add tests (#304)

This commit is contained in:
Maximilian Güntner 2024-08-27 18:55:21 +02:00 committed by GitHub
parent 0e8a053c74
commit 04fa0713da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 116 additions and 0 deletions

View file

@ -146,3 +146,40 @@ enum NoVariants {}
fn enum_no_variants() -> TestResult { fn enum_no_variants() -> TestResult {
test_default_generated_schema::<NoVariants>("no-variants") test_default_generated_schema::<NoVariants>("no-variants")
} }
#[derive(JsonSchema)]
#[serde(rename_all_fields = "PascalCase")]
pub enum RenameAllFields {
First {
nested_attribute: std::string::String,
},
}
#[derive(JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum RenameAll {
First { nested_attribute: bool },
}
#[derive(JsonSchema)]
pub enum RenameAttribute {
First {
#[serde(rename = "RenamedAttribute")]
nested_attribute: std::string::String,
},
}
#[test]
fn enum_unit_rename_attribute() -> TestResult {
test_default_generated_schema::<RenameAttribute>("enum-rename-attr")
}
#[test]
fn enum_unit_rename_all_fields() -> TestResult {
test_default_generated_schema::<RenameAllFields>("enum-rename-all-fields")
}
#[test]
fn enum_unit_rename_all() -> TestResult {
test_default_generated_schema::<RenameAll>("enum-rename-all")
}

View file

@ -0,0 +1,26 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "RenameAllFields",
"oneOf": [
{
"type": "object",
"properties": {
"First": {
"type": "object",
"properties": {
"NestedAttribute": {
"type": "string"
}
},
"required": [
"NestedAttribute"
]
}
},
"required": [
"First"
],
"additionalProperties": false
}
]
}

View file

@ -0,0 +1,26 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "RenameAll",
"oneOf": [
{
"type": "object",
"properties": {
"first": {
"type": "object",
"properties": {
"nested_attribute": {
"type": "boolean"
}
},
"required": [
"nested_attribute"
]
}
},
"required": [
"first"
],
"additionalProperties": false
}
]
}

View file

@ -0,0 +1,26 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "RenameAttribute",
"oneOf": [
{
"type": "object",
"properties": {
"First": {
"type": "object",
"properties": {
"RenamedAttribute": {
"type": "string"
}
},
"required": [
"RenamedAttribute"
]
}
},
"required": [
"First"
],
"additionalProperties": false
}
]
}

View file

@ -10,6 +10,7 @@ use super::get_meta_items;
pub(crate) static SERDE_KEYWORDS: &[&str] = &[ pub(crate) static SERDE_KEYWORDS: &[&str] = &[
"rename", "rename",
"rename_all", "rename_all",
"rename_all_fields",
"deny_unknown_fields", "deny_unknown_fields",
"tag", "tag",
"content", "content",