Skip to content

Commit

Permalink
fix: honor rename_all_fields, add tests
Browse files Browse the repository at this point in the history
closes GREsau#273
  • Loading branch information
mguentner committed Jul 10, 2024
1 parent e0c2c31 commit 7d485b8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
41 changes: 40 additions & 1 deletion schemars/tests/enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod util;
use schemars::{JsonSchema, Map};
use schemars::{schema_for, JsonSchema, Map};
use util::*;

// Ensure that schemars_derive uses the full path to std::string::String
Expand Down Expand Up @@ -144,3 +144,42 @@ enum NoVariants {}
fn enum_no_variants() -> TestResult {
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 {
let schema = schema_for!(RenameAll);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
test_default_generated_schema::<RenameAll>("enum-rename-all")
}
26 changes: 26 additions & 0 deletions schemars/tests/expected/enum-rename-all-fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "RenameAllFields",
"oneOf": [
{
"type": "object",
"required": [
"First"
],
"properties": {
"First": {
"type": "object",
"required": [
"NestedAttribute"
],
"properties": {
"NestedAttribute": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
]
}
26 changes: 26 additions & 0 deletions schemars/tests/expected/enum-rename-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "RenameAll",
"oneOf": [
{
"type": "object",
"required": [
"first"
],
"properties": {
"first": {
"type": "object",
"required": [
"nested_attribute"
],
"properties": {
"nested_attribute": {
"type": "boolean"
}
}
}
},
"additionalProperties": false
}
]
}
26 changes: 26 additions & 0 deletions schemars/tests/expected/enum-rename-attr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "RenameAttribute",
"oneOf": [
{
"type": "object",
"required": [
"First"
],
"properties": {
"First": {
"type": "object",
"required": [
"RenamedAttribute"
],
"properties": {
"RenamedAttribute": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
]
}
1 change: 1 addition & 0 deletions schemars_derive/src/attr/schemars_to_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::get_meta_items;
pub(crate) static SERDE_KEYWORDS: &[&str] = &[
"rename",
"rename_all",
"rename_all_fields",
"deny_unknown_fields",
"tag",
"content",
Expand Down

0 comments on commit 7d485b8

Please sign in to comment.