-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A (somewhat complex) type hint doesn't work #207
Comments
hi @jacobian, nice catch! brand new feature and people are already pushing the limits 😄 that bug was a small oversight. added your case to the test cases. it should now work as expected fyi: sometimes there is no getting around a one off serializer for complicated situations (not here tough). in those cases you can prevent having a misleading schema serializer laying around by using this: @extend_schema_field(inline_serializer(
name='RatingSummarySerializer',
fields={
'component': serializers.CharField(),
'counts': serializers.DictField(child=serializers.IntegerField())
}
))
def get_rating_summary(self, contractor: Contractor):
.... |
Wow, that was super-fast -- thank you! And thanks for the note on |
you're welcome. please close the issue if all works out fine |
Yup, verified that it works in my specific case -- thank you again! |
Describe the bug
I've got a
SerializerMethodField
that returns a moderately complex object of the form:So, I did this:
But, it doesn't work; I get this error message:
Warning #0: could not resolve type for "<class 'xxx.rest.serializers.RatingSummary'>". defaulting to "string"
To Reproduce
It looks like the specific part that makes it fail is having the return type be
List[ < any complex type >]
-- that is,List[int]
orList[string]
works correctly, butList[Dict[str, str]]
, or any non-primitive type, fails.Expected behavior
I'd love for this to work!
Workaround
There is an OK workaround, which is to define a serializer rather than the type hint, and annotate that on with
@extend_schema_field
- e.g.:This isn't perfect because
RatingSummarySerializer
isn't actually a serializer -- it's really just there for schema generation -- so that makes the code slightly confusing. But it's an acceptable workaround, IMO.The text was updated successfully, but these errors were encountered: