Skip to content

Commit 23886a4

Browse files
committed
fix: use NoDecode for cors origins
Otherwise, the env var syntax needs to be an encoded json string, which is awkward.
1 parent 0daf0bf commit 23886a4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

stac_fastapi/pgstac/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from urllib.parse import quote_plus as quote
66

77
from pydantic import BaseModel, BeforeValidator, Field, model_validator
8-
from pydantic_settings import BaseSettings, SettingsConfigDict
8+
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
99
from stac_fastapi.types.config import ApiSettings
1010
from typing_extensions import Self
1111

@@ -201,7 +201,9 @@ class Settings(ApiSettings):
201201
Implies that the `Transactions` extension is enabled.
202202
"""
203203

204-
cors_origins: Annotated[Sequence[str], BeforeValidator(str_to_list)] = ("*",)
204+
cors_origins: Annotated[Sequence[str], BeforeValidator(str_to_list), NoDecode] = (
205+
"*",
206+
)
205207
cors_origin_regex: Optional[str] = None
206208
cors_methods: Annotated[Sequence[str], BeforeValidator(str_to_list)] = (
207209
"GET",

tests/test_config.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import pytest
66
from pydantic import ValidationError
7+
from pytest import MonkeyPatch
78

8-
from stac_fastapi.pgstac.config import PostgresSettings
9+
from stac_fastapi.pgstac.config import PostgresSettings, Settings
910

1011

1112
async def test_pg_settings_with_env(monkeypatch):
@@ -74,3 +75,14 @@ async def test_pg_settings_attributes(monkeypatch):
7475
postgres_dbname="pgstac",
7576
_env_file=None,
7677
)
78+
79+
80+
def test_cors_origins(monkeypatch: MonkeyPatch) -> None:
81+
monkeypatch.setenv(
82+
"CORS_ORIGINS", "http://stac-fastapi-pgstac.test,http://stac-fastapi.test"
83+
)
84+
settings = Settings()
85+
assert settings.cors_origins == [
86+
"http://stac-fastapi-pgstac.test",
87+
"http://stac-fastapi.test",
88+
]

0 commit comments

Comments
 (0)