Skip to content

make_dataclass erase the __post_init__ method  #728

@vincentsarago

Description

@vincentsarago

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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions