fix: config empty values unset, drop adapter from_vars

This commit is contained in:
2026-10-25 21:18:16 +01:00
parent 9bf9342fc0
commit a00a5dc655
6 changed files with 83 additions and 88 deletions

View File

@@ -59,7 +59,8 @@ const LEGACY_ENV: &[(&str, &str)] = &[
];
/// Reads the legacy variables as plain strings; typing is left to the config
/// structs so that e.g. `JWT_SECRET=12345` stays a string.
/// structs so that e.g. `JWT_SECRET=12345` stays a string. Empty or
/// whitespace-only values are skipped as unset.
struct LegacyEnv;
impl Provider for LegacyEnv {
@@ -76,6 +77,11 @@ impl Provider for LegacyEnv {
let value = raw
.into_string()
.map_err(|_| figment::Error::from(format!("{variable} is not valid unicode")))?;
// An empty value (`PORT=` in a .env template) means "not set": the
// default applies, or the optional field stays `None`.
if value.trim().is_empty() {
continue;
}
let (section, field) = key.split_once('.').expect("legacy keys are section.field");
let entry = sections
.entry(section.to_owned())

View File

@@ -345,6 +345,27 @@ fn malformed_numeric_env_value_fails_naming_the_variable() {
});
}
#[test]
fn empty_env_value_falls_back_to_the_default() {
Jail::expect_with(|jail| {
jail_with(
jail,
&[
("RATE_LIMIT", ""),
("PORT", " "),
("TMDB_API_KEY", ""),
("NATS_URL", ""),
],
);
let c = load().expect("empty values are unset, not errors");
assert_eq!(c.server.rate_limit, 60);
assert_eq!(c.server.port, 3000);
assert!(c.metadata.tmdb_api_key.is_none());
assert!(c.nats.url.is_none());
Ok(())
});
}
#[test]
fn allow_registration_accepts_true_and_1_only() {
for (value, expected) in [