fastcrud
fastcrud copied to clipboard
Implement `select_schema` on `EndpointCreator` and `crud_router`
Pull Request Template for FastCRUD
Description
Adds an optional kwarg for the EndpointCreator and crud_router to specify a Pydantic schema for returning objects rather than dicts.
I've added documentation on the classes docstrings wrt to the new kwarg, but will need some guidance/help to add to the pages and on what to build examples.
Closes https://github.com/igorbenav/fastcrud/issues/113
Changes
- add a
select_schemaoptional kwarg toEndpointCreatorandcrud_router - add a
SelectSchemaType - replaces
BaseModelin theschema_to_selectarguments and appropriate returns - add if/else logic for
FastCRUD._read_itemandFastCRUD._read_items - refactor of the logic around
is_paginatedandhas_offset_limitinFastCRUD._read_items - addition of client fixture with
select_schemaset toread_schemafixture and tests with model validation
Tests
I copy pasted the "simple" client tests for get and get_multi items, replacing the client for the new client with select_schema and added read_schema.model_validate(...) in the assertions.
I've created them on the SQLModel side, happy to duplicate that on the SQLAlchemy side, but it's not part of my current cases so I'll have to learn how.
I'm not certain tests with read_schema.model_validate(...) are quite enough, I would prefer a isinstance somewhere, but couldn't find the right set of tests to build upon.
Checklist
- [x] I have read the CONTRIBUTING document.
- [x] My code follows the code style of this project.
- [x] I have added necessary documentation (if appropriate). NB. I'm happy to do more on this front
- [x] I have added tests that cover my changes (if applicable).
- [x] All new and existing tests passed.
Additional Notes
Fairly new to SQLModel and FastAPI, there might be things I missed.