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

Preserving casing while building configuration #564

Open
ohadvano opened this issue May 22, 2024 · 1 comment
Open

Preserving casing while building configuration #564

ohadvano opened this issue May 22, 2024 · 1 comment

Comments

@ohadvano
Copy link

ohadvano commented May 22, 2024

When reading a JSON file, for example:

{
    "myStructField": "value"
}

Using config:

let settings = Config::builder()
    .add_source(config::File::with_name("src/settings.json").required(false))
    .build()
    .unwrap();

And trying to deserialize it to a struct defined as:

#[derive(Debug, Deserialize)]
#[serde(rename_all = "cascalCase")]
pub struct MyStruct {
    my_struct_field: String,
}

The program will panic with an error that myStructField does not exist. This is correct because config will by default lowercase all the keys. This creates a scenario where it's not possible to use serde deserializer to handle this, since there's no rename_all option that will fit this case.

I've seen this issue repeating multiple times, but didn't see an issue that suggests a way to solve this. I'm not too familiar with the internal implementation, but would it be possible to add an option to preserve the keys while building the configuration, for example by adding .add_source(config::File::with_name("src/settings.json").preserve_casing(true).required(false))?

If it should be possible, and it's accepted, I can try implementing this (unless there's some alternative to get through this).
We'd like to use the config library, but we have many JSON file configurations that this creates a lot of work to modify them, and the need to preserve the two versions of the JSON files for backward compatibilities.

@kagu2023
Copy link

+1. I see the same issue after upgrading from 0.13.4(which preserve case) to 0.14.

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