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

Override Serde for schema generation only #260

Open
NuSkooler opened this issue Dec 18, 2023 · 1 comment
Open

Override Serde for schema generation only #260

NuSkooler opened this issue Dec 18, 2023 · 1 comment

Comments

@NuSkooler
Copy link

NuSkooler commented Dec 18, 2023

We are using Schemars for schema generation in our project and it has been great!

I have a particular struct that is used to both deserialize from PascalCase XML and serialize to snake_case JSON. When creating a schema, the PascalCase is always used. Am I doing something wrong?

Example:

#[derive(JsonSchema, Serialize, Deserialize)]
#[serde(rename_all(deserialize = "PascalCase"))] // for XML input
#[serde(rename_all(serialize = "snake_case"))] // JSON output
pub struct MyStruct {
  pub foo Foo; // schema ends up with "Foo"
}

Edit: I also tried "overriding" Serde with a schemars declaration:

#[schemars(rename_all = "snake_case")]

But no luck

@GREsau
Copy link
Owner

GREsau commented Aug 27, 2024

Overriding it with the #[schemars(...)] attribute should work. I just tried this:

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

#[derive(JsonSchema, Serialize, Deserialize)]
#[serde(rename_all(deserialize = "PascalCase"))] // for XML input
#[serde(rename_all(serialize = "snake_case"))] // JSON output
#[schemars(rename_all = "snake_case")]
pub struct MyStruct {
    pub foo: i32,
}

fn main() {
    let schema = schema_for!(MyStruct);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

And the generated schema always has the property foo, not Foo. This is the case both for schemars v0.8 and v1.0 alpha.

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

No branches or pull requests

2 participants