Issue with ALLOW_ORIGINS Environment Variable Causing Pydantic Error #486
-
Hi @tarsil, I am encountering an issue with the allow_origins
Input should be a valid list [type=list_type, input_value='https://mywebsite.com/ https://github.com/', input_type=str]
For further information visit https://errors.pydantic.dev/2.10/v/list_type I have tried setting the ALLOW_ORIGINS="https://mywebsite.com https://github.com" this is how I am specifying in development settings: class DevelopmentSettings(AppSettings):
...
allow_origins: list = os.environ["ALLOW_ORIGINS"].split()
@property
def cors_config(self) -> CORSConfig:
"""
Initial Default configuration for the CORS.
This can be overwritten in another setting or simply override
`allow_origins` or then override the `def cors_config()`
property to change the behavior of the whole cors_config.
"""
if not self.allow_origins:
return None
return CORSConfig(
allow_origins=self.allow_origins,
allow_methods=self.allow_methods,
allow_credentials=self.allow_credentials,
) This is how I am specifying the cors_config in main.py # main.py
...
from esmerald.conf import settings
...
def get_application():
app = Esmerald(
...
cors_config=settings.cors_config
)
return app
Could you provide guidance on this. Thank you for your time and assistance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 18 replies
-
Yes, so an env var when it comes as a list, it won't come as a list at all, its a string split by whatever as you said but pydantic as an issue with those. My advice is to use what Lilya under Esmerald has. Try to use like this from lilya.environments import EnvironLoader
loader = EnvironLoader()
def validate_list(value: str) -> list[str]:
return value.split(",").split()
class DevelopmentSettings(AppSettings):
...
allow_origins: list = loader("ALLOW_ORIGINS", cast=validate_list, default=['*'])
@property
def cors_config(self) -> CORSConfig:
"""
Initial Default configuration for the CORS.
This can be overwritten in another setting or simply override
`allow_origins` or then override the `def cors_config()`
property to change the behavior of the whole cors_config.
"""
if not self.allow_origins:
return None
return CORSConfig(
allow_origins=self.allow_origins,
allow_methods=self.allow_methods,
allow_credentials=self.allow_credentials,
) Also, if you have: class DevelopmentSettings(AppSettings):
...
allow_origins: list = os.environ["ALLOW_ORIGINS"].split()
@property
def cors_config(self) -> CORSConfig:
"""
Initial Default configuration for the CORS.
This can be overwritten in another setting or simply override
`allow_origins` or then override the `def cors_config()`
property to change the behavior of the whole cors_config.
"""
if not self.allow_origins:
return None
return CORSConfig(
allow_origins=self.allow_origins,
allow_methods=self.allow_methods,
allow_credentials=self.allow_credentials,
) You don't need to pass it into Esmerald(....). Why? Because if you are loading the development settings via ESMERALD_SETTINGS_MODULE, then Esmerald knows that the property Pydantic has an issue with lists and casts from those coming from env vars. Let me know if it worked for you. Docs about Lilya Environment loader -> https://www.lilya.dev/environments/?h=en Esmerald is on top of it, so it will work normally in your import. |
Beta Was this translation helpful? Give feedback.
-
@tarsil |
Beta Was this translation helpful? Give feedback.
@prashikdewtale10 Ok there might be something wrong on your side that I'm not aware but this is how I made it work cleanly in my codebase that also uses the
cors_config
.