Dependency Injection with Pydantic Validation #8330
-
|
Hi, I'm trying to use the Dependency injection feature with Pydantic validation on routes and it fails. Non-working Example: Simple pytest test class: Fixtures: Results in: I have tried to:
Any ideas? Thanks, Brett |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi Brett, Thank you for reporting this, and for the complete example with tests and fixtures. That made it easy to reproduce on the first try, which almost never happens with these reports. This is a bug in The schema is never actually used and we were serializing this. I mean, the return value of a dependency is injected straight into your handler, not serialized or validated. So we were generating something we throw away, and the generation itself was crashing. That is also why The fix is to stop building the return schema for dependencies and I'm working on that. While it's not released yet, a workaround is to drop the return annotation on the dependency functions: def get_dynamodb_client(boto3_session): # no -> DynamoDBClient
return boto3_session.client("dynamodb")Without the return annotation there's nothing to build a schema from, so the error goes away. Your handler's return type stays annotated and validated as normal. Thanks again for taking the time to write this up so clearly. |
Beta Was this translation helpful? Give feedback.
-
|
Closing this discussion because I've opened an issue: #8333 |
Beta Was this translation helpful? Give feedback.
Hi Brett,
Thank you for reporting this, and for the complete example with tests and fixtures. That made it easy to reproduce on the first try, which almost never happens with these reports.
This is a bug in
Depends, not a mistake on your side. Whenenable_validation=True, we walk the dependency tree and try to build a Pydantic schema for the return type of every dependency function. For yourbotocore.Session/boto3.Session/DynamoDBClientdependencies that fails, because those are arbitrary types Pydantic can't model.The schema is never actually used and we were serializing this. I mean, the return value of a dependency is injected straight into your handler, not serialized or validate…