fastcrud icon indicating copy to clipboard operation
fastcrud copied to clipboard

Implement `select_schema` on `EndpointCreator` and `crud_router`

Open ljmc-github opened this issue 1 year ago • 1 comments
trafficstars

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_schema optional kwarg to EndpointCreator and crud_router
  • add a SelectSchemaType
  • replaces BaseModel in the schema_to_select arguments and appropriate returns
  • add if/else logic for FastCRUD._read_item and FastCRUD._read_items
  • refactor of the logic around is_paginated and has_offset_limit in FastCRUD._read_items
  • addition of client fixture with select_schema set to read_schema fixture 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.

ljmc-github avatar Oct 09 '24 22:10 ljmc-github