2.0 KiB
tags, project, status
| tags | project | status | ||||
|---|---|---|---|---|---|---|
|
chiron-relay | Closed |
Project: chiron-relay
Description
During testing an issue was found with the way that fastAPI was handling pydantic validation with html form data. This resulted in persistent 422 error codes, even with proper data that should have pass validation.
Troubleshooting Steps:
- Verify pydantic model is correct
- Verify that data matches pydantic model
- Include checking for data types and type casting
- Verify test function was sending data correctly as form data and not JSON
- Verify endpoint correctly accepted data
- Include testing with fastAPI.Depend()
- Verify Environment is correctly set up
- Ensure all dependancies are correctly installed
- Recreated venv
- Test with different versions of python
- Test on different machines
- Verify fastAPI is accepting data from testClient correctly
- Created test endpoint
- Created new test to send data to the test endpoint
Implemented Solution
The final implement solution was to handle data validation for form data semi manually. Rather than let fastAPI handle the validation by dependency injection, instead manually get the form data in the endpoint function and use the pydantic model to validate the data, raising a RequestValidationError if the data is not valid. Raising the exception allows my already existing exception handling middleware to still function as designed.
Root Cause
All testing and evidence points to an issue with fastAPI's handling of dependency injection in relation to form data.
Next Steps
In cases where form data needs to handled or validated its highly recommended to follow the above Implement Solution and use await request.form() to access form data and then create an instance of the required pydantic model to handle validation. Error handling for this should raise RequestValidationError inline with how fastAPI typically handles data validation issues