Add typing to all of our API endpoints
Background
With the work going on in https://gitlab.com/alleles/ella-frontend, we need continuous updates of types on endpoints as we move along.
For a lot of the endpoints we use marshmallow schemas (untyped), but in general they are all based on our SQLAlchemy ORM model. We are moving to FastAPI, where Pydantic is central.
Attached are the Pydantic models generated using https://github.com/tiangolo/pydantic-sqlalchemy, with some incomplete modifications.
Implementation
I suggest typing up models in src/api/schemas/pydantic
for each endpoint, starting with the most urgent ones (i.e. the ones will be worked against from the frond end refactoring).
Once models are created, I think they should be added as validation of the endpoints, for example through a validation-decorator:
def validate_output(Model: pydantic.BaseModel):
def _validate_output(func):
@wraps(func)
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
Model(**ret) # Validates
return ret
return inner
return _validate_output
...
class DummyResultModel(pydantic.BaseModel):
count: int
results: List[str]
class DummyResource():
@validate_output(DummyResultModel)
def get(self, *args, **kwargs):
# return {count: None, results: [12]} # Would fail validation
return {count: 3, results: ["a", "b", "c"]} # Would pass validation
Using related issues
Issues have been created for each API endpoint needing typing, with higher priority at the top of the list. The MRs for these issues should target this issue's branch and not dev.
- Pick up the first open, unassigned issue from the top of the list and assign yourself
- From that issue's page, use the arrow just to the right of
Create merge request
and change the source branch fromdev
to1986-add-typing-to-all-of-our-api-endpoints
- Create merge request and branch
- Once the MR into
1986-add-typing-to-all-of-our-api-endpoints
has completed, manually close the issue. Auto-close on merge is only supported when merging to the default branch (dev
).