Skip to content
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

enums: serde rename_all_fields not respected #273

Closed
mguentner opened this issue Mar 11, 2024 · 0 comments · Fixed by #304
Closed

enums: serde rename_all_fields not respected #273

mguentner opened this issue Mar 11, 2024 · 0 comments · Fixed by #304

Comments

@mguentner
Copy link
Contributor

Description

When using serde's rename_all_fields on an enum together with schemars, the fields are not properly renamed in the schema.
Only explicitly renaming each and every field renames them.

How to reproduce

use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, JsonSchema)]
#[derive(Debug, Clone, PartialEq)]
#[serde(tag = "Variant")]
#[serde(rename_all = "lowercase")]
#[serde(rename_all_fields = "PascalCase")]
pub enum Example {
    First {
        nested_attribute: String,
    },
    Second {
        #[serde(rename = "AnotherAttribute")]
        another_attribute: String,
    },
}

fn main() {
    let schema = schema_for!(Example);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
    let example1 = Example::First { nested_attribute: "foo".to_owned()};
    println!("{}", serde_json::to_string_pretty(&example1).unwrap());
    let example2 = Example::Second { another_attribute: "foo".to_owned()};
    println!("{}", serde_json::to_string_pretty(&example2).unwrap());
}

Output:

❗ comments are mine

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Example",
  "oneOf": [
    {
      "type": "object",
      "required": [
        "Variant",
❗       "nested_attribute"  👈 snake_case, in spite of `rename_all_fields = "PascalCase"`
      ],
      "properties": {
        "Variant": {
          "type": "string",
          "enum": [
            "first"
          ]
        },
        "nested_attribute": {
          "type": "string"
        }
      }
    },
    {
      "type": "object",
      "required": [
❗       "AnotherAttribute", 👈 PascalCase!
        "Variant"
      ],
      "properties": {
        "AnotherAttribute": {
          "type": "string"
        },
        "Variant": {
          "type": "string",
          "enum": [
            "second"
          ]
        }
      }
    }
  ]
}
❗👇 json serialization output is the same in terms of capitalization 
{
  "Variant": "first",
  "NestedAttribute": "foo"
}
{
  "Variant": "second",
  "AnotherAttribute": "foo"
}
mguentner added a commit to mguentner/schemars that referenced this issue Jul 10, 2024
mguentner added a commit to mguentner/schemars that referenced this issue Jul 10, 2024
mguentner added a commit to mguentner/schemars that referenced this issue Jul 10, 2024
GREsau pushed a commit to mguentner/schemars that referenced this issue Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant