Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make_dataclass erase the __post_init__ method #728

Closed
vincentsarago opened this issue Jul 8, 2024 · 0 comments · Fixed by #729
Closed

make_dataclass erase the __post_init__ method #728

vincentsarago opened this issue Jul 8, 2024 · 0 comments · Fixed by #729
Labels
bug Something isn't working

Comments

@vincentsarago
Copy link
Member

vincentsarago commented Jul 8, 2024

in #714 we changed how we create GET request model from using attrs to dataclass. This was to support alias with -. The downside was that we need to do validation/conversion in a __post_init__ method.

It seems that this wasn't well tested 😢, and now when we create a Model with extensions the __post_init__ is not ran 😭 😭 😭 😭 😭

from stac_fastapi.api.models import create_get_request_model
from stac_fastapi.extensions.core import FieldsExtension

model = create_get_request_model(extensions=[FieldsExtension()])
model(datetime="yo")


model = create_get_request_model(extensions=[])
model(datetime="yo")

>> HTTPException: 400: Invalid RFC3339 datetime.

where

def create_request_model(
model_name="SearchGetRequest",
base_model: Union[Type[BaseModel], APIRequest] = BaseSearchGetRequest,
extensions: Optional[List[ApiExtension]] = None,
mixins: Optional[Union[List[BaseModel], List[APIRequest]]] = None,
request_type: Optional[str] = "GET",
) -> Union[Type[BaseModel], APIRequest]:
"""Create a pydantic model for validating request bodies."""
fields = {}
extension_models = []
# Check extensions for additional parameters to search
for extension in extensions or []:
if extension_model := extension.get_request_model(request_type):
extension_models.append(extension_model)
mixins = mixins or []
models = extension_models + mixins + [base_model]
# Handle GET requests
if all([issubclass(m, APIRequest) for m in models]):
return make_dataclass(model_name, [], bases=tuple(models))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant