Skip to content

Commit 192f0de

Browse files
committed
feat: add json parsing, more values
1 parent 900f0a2 commit 192f0de

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

stac_fastapi/pgstac/config.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Postgres API configuration."""
22

3+
import json
34
import warnings
45
from typing import Annotated, Any, List, Optional, Sequence, Type
56
from urllib.parse import quote_plus as quote
@@ -158,8 +159,12 @@ def connection_string(self):
158159

159160
def str_to_list(value: Any) -> Any:
160161
if isinstance(value, str):
161-
return [v.strip() for v in value.split(",")]
162-
return value
162+
if value.startswith("["):
163+
return json.loads(value)
164+
else:
165+
return [v.strip() for v in value.split(",")]
166+
else:
167+
return value
163168

164169

165170
class Settings(ApiSettings):
@@ -205,13 +210,13 @@ class Settings(ApiSettings):
205210
"*",
206211
)
207212
cors_origin_regex: Optional[str] = None
208-
cors_methods: Annotated[Sequence[str], BeforeValidator(str_to_list)] = (
213+
cors_methods: Annotated[Sequence[str], BeforeValidator(str_to_list), NoDecode] = (
209214
"GET",
210215
"POST",
211216
"OPTIONS",
212217
)
213218
cors_credentials: bool = False
214-
cors_headers: Annotated[Sequence[str], BeforeValidator(str_to_list)] = (
219+
cors_headers: Annotated[Sequence[str], BeforeValidator(str_to_list), NoDecode] = (
215220
"Content-Type",
216221
)
217222

tests/test_config.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,58 @@ async def test_pg_settings_attributes(monkeypatch):
7777
)
7878

7979

80-
def test_cors_origins(monkeypatch: MonkeyPatch) -> None:
80+
@pytest.mark.parametrize(
81+
"cors_origins",
82+
[
83+
"http://stac-fastapi-pgstac.test,http://stac-fastapi.test",
84+
'["http://stac-fastapi-pgstac.test","http://stac-fastapi.test"]',
85+
],
86+
)
87+
def test_cors_origins(monkeypatch: MonkeyPatch, cors_origins: str) -> None:
8188
monkeypatch.setenv(
82-
"CORS_ORIGINS", "http://stac-fastapi-pgstac.test,http://stac-fastapi.test"
89+
"CORS_ORIGINS",
90+
cors_origins,
8391
)
8492
settings = Settings()
8593
assert settings.cors_origins == [
8694
"http://stac-fastapi-pgstac.test",
8795
"http://stac-fastapi.test",
8896
]
97+
98+
99+
@pytest.mark.parametrize(
100+
"cors_methods",
101+
[
102+
"GET,POST",
103+
'["GET","POST"]',
104+
],
105+
)
106+
def test_cors_methods(monkeypatch: MonkeyPatch, cors_methods: str) -> None:
107+
monkeypatch.setenv(
108+
"CORS_METHODS",
109+
cors_methods,
110+
)
111+
settings = Settings()
112+
assert settings.cors_methods == [
113+
"GET",
114+
"POST",
115+
]
116+
117+
118+
@pytest.mark.parametrize(
119+
"cors_headers",
120+
[
121+
"Content-Type,X-Foo",
122+
'["Content-Type","X-Foo"]',
123+
],
124+
)
125+
def test_cors_headers(monkeypatch: MonkeyPatch, cors_headers: str) -> None:
126+
monkeypatch.setenv(
127+
"CORS_HEADERS",
128+
cors_headers,
129+
)
130+
settings = Settings()
131+
assert settings.cors_headers == [
132+
"Content-Type",
133+
"X-Foo",
134+
]

0 commit comments

Comments
 (0)