Exception on enum fields with React adapter
Closed this issue · 4 comments
Hi,
Using Django 1.11.1, Rest framework 3.7.7, drf-schema-adapter 0.9.52, and the ReactJsonSchemaAdapter adapter, it seems I'm getting an exception accessing the schema of any model that contains a field with a choices
argument (https://docs.djangoproject.com/en/2.0/ref/models/fields/#choices).
The exception is as follow:
__Traceback:__
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
95. return self.dispatch(request, *args, **kwargs)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
494. response = self.handle_exception(exc)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception
454. self.raise_uncaught_exception(exc)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
491. response = handler(request, *args, **kwargs)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/rest_framework/views.py" in options
505. data = self.metadata_class().determine_metadata(request, self)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/drf_auto_endpoint/metadata.py" in determine_metadata
113. return adapter(metadata)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/drf_auto_endpoint/adapters.py" in __call__
32. return self.render(config)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/drf_auto_endpoint/adapters.py" in render
394. config['fields'] = super(ReactJsonSchemaAdapter, self).render(config)
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/drf_auto_endpoint/adapters.py" in render
24. adapted.append(self.adapt_field(field))
File "/usr/share/python/scirius/local/lib/python2.7/site-packages/drf_auto_endpoint/adapters.py" in adapt_field
301. new_field['schema']['enum'] = [x[0] for x in field['choices']]
Exception Type: KeyError at /rest/rules/transformations/rules/
Exception Value: 0
I have been able work-around it with this small patch, not sure it's the right way to do it though:
--- adapters.py.old 2018-04-10 09:48:55.506893000 +0000
+++ adapters.py 2018-04-10 09:48:35.834893000 +0000
@@ -299,6 +299,6 @@
if 'choices' in field:
- new_field['schema']['enum'] = [x[0] for x in field['choices']]
- new_field['schema']['enumNames'] = [x[1] for x in field['choices']]
+ new_field['schema']['enum'] = [x['value'] for x in field['choices']]
+ new_field['schema']['enumNames'] = [x['label'] for x in field['choices']]
# if 'related_endpoint' in field:
Hi,
the ReactJsonSchemaAdapter has not received a lot of move lately so it might very well be outdated.
Let me double-check but your fix does seem correct.
Hi,
Things look ok so far, I'll let you know if I find any issue.
Thanks
Just opened a PR with the fix mentioned above.
Thanks, I'll take a look at it this evening and merge if everything is ok