-
Notifications
You must be signed in to change notification settings - Fork 255
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
AssertionError on resolve_serializer #126
Comments
@jayvdb i agree with you on this. the question is where to catch this properly. your fix may hide an underlying issue. i would like to understand what went wrong. pretty much all in any case there needs to be more info provided to the user. thats for sure. |
#120 (comment) already says how to reproduce. |
i get what you used but not how you used it. would you be so kind to narrow it down with a snippet. i'll happily debug it then. |
I used it just like described in the README. It was also a But then there is also File "/usr/lib/python3.8/site-packages/drf_spectacular/generators.py", line 162, in get_schema
paths=self.parse(request, public),
File "/usr/lib/python3.8/site-packages/drf_spectacular/generators.py", line 142, in parse
operation = view.schema.get_operation(path, path_regex, method, self.registry)
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 66, in get_operation
request_body = self._get_request_body()
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 789, in _get_request_body
component = self.resolve_serializer(serializer, 'request')
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 930, in resolve_serializer
component.schema = self._map_serializer(serializer, direction)
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 585, in _map_serializer
schema = self._map_basic_serializer(serializer, direction)
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 644, in _map_basic_serializer
schema = self._map_serializer_field(field, direction)
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 408, in _map_serializer_field
schema = self._map_serializer_field(field.child, direction)
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 390, in _map_serializer_field
meta = self._get_serializer_field_meta(field)
File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 618, in _get_serializer_field_meta
if field.read_only:
AttributeError: 'LazyChildProxy' object has no attribute 'read_only' To get it working I did --- a/drf_spectacular/openapi.py
+++ b/drf_spectacular/openapi.py
@@ -695,18 +695,18 @@ class AutoSchema(ViewInspector):
def _get_serializer_field_meta(self, field):
meta = {}
- if field.read_only:
+ if getattr(field, 'read_only', None):
meta['readOnly'] = True
- if field.write_only:
+ if getattr(field, 'write_only', None):
meta['writeOnly'] = True
- if field.allow_null:
+ if getattr(field, 'allow_null', None):
meta['nullable'] = True
- if field.default is not None and field.default != empty and not callable(field.default):
+ if getattr(field, 'default', None) is not None and field.default != empty and not callable(field.default):
default = field.to_representation(field.default)
if isinstance(default, set):
default = list(default)
meta['default'] = default
- if field.help_text:
+ if getattr(field, 'help_text', None):
meta['description'] = str(field.help_text)
return meta
I dont really want it fixed; it isnt important; there are more important PRs and issues to be solved, including several that were closed without being fixed. |
It would also be good to have a policy change, to stop using assert like this, maybe converting all of them to
|
robustified checked all |
I just hit this.
I suggest that assert is never used without a second arg which explains the failed assertion. There are code quality tools which can enforce this. If you want these assertions to cause issues to be raised, it helps to have assertion messages let people raise bugs which can be used to identify the problem.
Since this component is py36+, it could also use
should_be
instead ofassert
, which auto-voodoo creates useful assertion error messages.And as usual, I would prefer these are warnings. In my case it is a custom lazy-ily defined serializer and it could be safely ignored as inspecting it wont work anyway. The following worked for me.
Originally posted by @jayvdb in #120 (comment)
The text was updated successfully, but these errors were encountered: