Integer Strings Parsed as Float in Environment Variables
#1 opened on Jun 21, 2024
Repository metrics
- Stars
- (7 stars)
- PR merge metrics
- (PR metrics pending)
Description
Description:
The toml-env library appears to be parsing integer strings into floats, which is not the intended usage. This issue occurs when environment variables are used to override configuration values.
Steps to Reproduce:
- Define a configuration with environment variable mapping using the
toml-envlibrary. - Set an environment variable with an integer string value.
- Run the application.
Example Code:
let config: Option<ConfigFileResponse> = initialize(Args {
auto_map_env: Some(AutoMapEnvArgs {
divider: "__",
prefix: Some("TAGOIO"), // Prefix for environment variables
transform: Box::new(|name| name.to_lowercase()),
}),
// logging: Logging::StdOut,
config_path: Some(&config_path),
..Args::default()
})
.unwrap_or_else(|err| {
log::error!(target: "error", "Failed to initialize configuration: {}", err);
std::process::exit(1);
});
Error Message: When running the following command:
TAGOIO__RELAY__DOWNLINK_PORT="1883" cargo run start
The following error is triggered:
[2024-06-21T19:06:21Z ERROR error] Failed to initialize configuration: Error merging configuration environment variables into config TOML file "./config.toml": Incompatible types at path "$.relay.downlink_port", expected "string" received "float".
Expected Behavior:
The environment variable TAGOIO__RELAY__DOWNLINK_PORT should be parsed as an integer string and not converted to a float.
Actual Behavior:
The environment variable TAGOIO__RELAY__DOWNLINK_PORT is being parsed as a float, causing a type mismatch error.
Additional Context:
toml-envlibrary version: 1.2.0- Operating System: macos-sonoma
Proposed Solution: Ensure that integer strings in environment variables are parsed correctly as integers and not converted to floats.