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

Do not always dump pydantic models to dict #56

Closed
psychedelicious opened this issue Feb 19, 2024 · 0 comments · Fixed by #57
Closed

Do not always dump pydantic models to dict #56

psychedelicious opened this issue Feb 19, 2024 · 0 comments · Fixed by #57

Comments

@psychedelicious
Copy link
Contributor

Request:

When using pydantic models for events, they are dumped to dict by fastapi-events.

Motivation

This is convenient but restrictive. A pydantic model is often more useful for a handler than a dict:

  • You can call isinstance on it. This is particularly useful for events where one handler callback handles multiple event types.
  • The model may have methods or other logic/data that isn't captured by dumping to dict.
  • You can get type hints in event handlers.

Proposed API

Add arg payload_schema_dump: bool = True to dispatch.

The default behaviour is unchanged. When this is set to False, handlers are called with the pydantic model as the payload.

If validate_payload is True, we still do the round-trip to re-validate the payload, but skip the final dump.

psychedelicious added a commit to psychedelicious/fastapi-events that referenced this issue Feb 19, 2024
- Adds `payload_schema_dump: bool = True` arg to `dispatch`, `_derive_event_name_and_payload_from_pydantic_model` and `_validate_payload`. When `False`, the event handlers are called "live" pydantic model for payload. The default behaviour is unchanged.
- Updates tests for this new functionality.

Closes melvinkcx#56
psychedelicious added a commit to psychedelicious/fastapi-events that referenced this issue Feb 20, 2024
- Adds `payload_schema_dump: bool = True` arg to `dispatch`, `_derive_event_name_and_payload_from_pydantic_model` and `_validate_payload`. When `False`, the event handlers are called "live" pydantic model for payload. The default behaviour is unchanged.
- Updates tests for this new functionality.

Closes melvinkcx#56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant