maribakulj commited on
Commit
310419f
·
1 Parent(s): 115749e

Fix settings boolean parsing robustness

Browse files
app/backend/app/config/settings.py CHANGED
@@ -1,6 +1,7 @@
1
  """Application settings loaded from environment variables."""
2
 
3
  import re
 
4
 
5
  from pydantic import AliasChoices, Field
6
  from pydantic import field_validator
@@ -70,6 +71,7 @@ class Settings(BaseSettings):
70
 
71
  @field_validator(
72
  "debug",
 
73
  "gallica_use_fixtures",
74
  "bodleian_use_fixtures",
75
  "europeana_use_fixtures",
@@ -81,32 +83,11 @@ class Settings(BaseSettings):
81
  def normalize_bool_env_values(cls, value: object, info):
82
  """Normalize permissive boolean-like environment values for robust deployments."""
83
 
84
- if isinstance(value, bool):
85
- return value
86
- if isinstance(value, (int, float)):
87
- return bool(value)
88
- if not isinstance(value, str):
89
- return value
90
-
91
- normalized = value.strip().lower()
92
- truthy = {"1", "true", "yes", "on"}
93
- falsy = {"0", "false", "no", "off", ""}
94
-
95
- if normalized in truthy:
96
- return True
97
- if normalized in falsy:
98
- return False
99
-
100
- first_token = re.split(r"[^a-z0-9]+", normalized, maxsplit=1)[0]
101
- if first_token in truthy:
102
  return True
103
- if first_token in falsy:
104
- return False
105
-
106
- if info.field_name.endswith("_use_fixtures") and "fixture" in normalized:
107
- return True
108
-
109
- return cls.model_fields[info.field_name].default
110
 
111
  model_config = SettingsConfigDict(env_prefix="CLAFOUTIS_", extra="ignore")
112
 
 
1
  """Application settings loaded from environment variables."""
2
 
3
  import re
4
+ from typing import Any
5
 
6
  from pydantic import AliasChoices, Field
7
  from pydantic import field_validator
 
71
 
72
  @field_validator(
73
  "debug",
74
+ "serve_frontend",
75
  "gallica_use_fixtures",
76
  "bodleian_use_fixtures",
77
  "europeana_use_fixtures",
 
83
  def normalize_bool_env_values(cls, value: object, info):
84
  """Normalize permissive boolean-like environment values for robust deployments."""
85
 
86
+ default = cls.model_fields[info.field_name].default
87
+ parsed = _parse_bool_with_fallback(value, default)
88
+ if isinstance(value, str) and info.field_name.endswith("_use_fixtures") and "fixture" in value.lower():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  return True
90
+ return parsed
 
 
 
 
 
 
91
 
92
  model_config = SettingsConfigDict(env_prefix="CLAFOUTIS_", extra="ignore")
93
 
app/tests/unit/backend/test_settings.py CHANGED
@@ -38,3 +38,13 @@ def test_bodleian_use_fixtures_accepts_descriptive_value(monkeypatch) -> None:
38
  loaded = Settings()
39
 
40
  assert loaded.bodleian_use_fixtures is True
 
 
 
 
 
 
 
 
 
 
 
38
  loaded = Settings()
39
 
40
  assert loaded.bodleian_use_fixtures is True
41
+
42
+
43
+ def test_serve_frontend_accepts_descriptive_value(monkeypatch) -> None:
44
+ """Frontend serving flag should tolerate descriptive values and keep default-safe behavior."""
45
+
46
+ monkeypatch.setenv("CLAFOUTIS_SERVE_FRONTEND", "Serve the built frontend from the backend")
47
+
48
+ loaded = Settings()
49
+
50
+ assert loaded.serve_frontend is True