Skip to content

Commit 96ac271

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix: ensure max_connection accepts default/None
1 parent 1315ced commit 96ac271

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import json
44
import logging
5-
from typing import List, Optional, Tuple
5+
from typing import Any, List, Optional, Tuple
66
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
77

88
from pydantic import field_validator
@@ -21,7 +21,7 @@ class RedisSentinelSettings(BaseSettings):
2121
REDIS_SENTINEL_MASTER_NAME: str = "master"
2222
REDIS_DB: int = 15
2323

24-
REDIS_MAX_CONNECTIONS: int = 10
24+
REDIS_MAX_CONNECTIONS: int | None = 10
2525
REDIS_RETRY_TIMEOUT: bool = True
2626
REDIS_DECODE_RESPONSES: bool = True
2727
REDIS_CLIENT_NAME: str = "stac-fastapi-app"
@@ -36,13 +36,18 @@ def validate_db_sentinel(cls, v: int) -> int:
3636
raise ValueError("REDIS_DB must be a positive integer")
3737
return v
3838

39-
@field_validator("REDIS_MAX_CONNECTIONS")
39+
@field_validator("REDIS_MAX_CONNECTIONS", mode="before")
4040
@classmethod
41-
def validate_max_connections_sentinel(cls, v: int) -> int:
42-
"""Validate REDIS_MAX_CONNECTIONS is at least 1."""
43-
if v < 1:
44-
raise ValueError("REDIS_MAX_CONNECTIONS must be at least 1")
45-
return v
41+
def validate_max_connections_sentinel(cls, v: Any) -> Optional[int]:
42+
"""Validate REDIS_MAX_CONNECTIONS."""
43+
if v is None or v == "":
44+
return None
45+
if isinstance(v, str) and v.lower() in ("none", "null"):
46+
return None
47+
try:
48+
return int(v)
49+
except (ValueError, TypeError):
50+
raise ValueError("Invalid value for REDIS_MAX_CONNECTIONS")
4651

4752
@field_validator("REDIS_HEALTH_CHECK_INTERVAL")
4853
@classmethod
@@ -111,7 +116,7 @@ class RedisSettings(BaseSettings):
111116
REDIS_PORT: int = 6379
112117
REDIS_DB: int = 15
113118

114-
REDIS_MAX_CONNECTIONS: int = 10
119+
REDIS_MAX_CONNECTIONS: int | None = 10
115120
REDIS_RETRY_TIMEOUT: bool = True
116121
REDIS_DECODE_RESPONSES: bool = True
117122
REDIS_CLIENT_NAME: str = "stac-fastapi-app"
@@ -134,13 +139,18 @@ def validate_db_standalone(cls, v: int) -> int:
134139
raise ValueError("REDIS_DB must be a positive integer")
135140
return v
136141

137-
@field_validator("REDIS_MAX_CONNECTIONS")
142+
@field_validator("REDIS_MAX_CONNECTIONS", mode="before")
138143
@classmethod
139-
def validate_max_connections_standalone(cls, v: int) -> int:
140-
"""Validate REDIS_MAX_CONNECTIONS is at least 1."""
141-
if v < 1:
142-
raise ValueError("REDIS_MAX_CONNECTIONS must be at least 1")
143-
return v
144+
def validate_max_connections_standalone(cls, v: Any) -> Optional[int]:
145+
"""Validate REDIS_MAX_CONNECTIONS."""
146+
if v is None or v == "":
147+
return None
148+
if isinstance(v, str) and v.lower() in ("none", "null"):
149+
return None
150+
try:
151+
return int(v)
152+
except (ValueError, TypeError):
153+
raise ValueError("Invalid value for REDIS_MAX_CONNECTIONS")
144154

145155
@field_validator("REDIS_HEALTH_CHECK_INTERVAL")
146156
@classmethod

0 commit comments

Comments
 (0)