Fix SerializedPKRelatedField type API expression

A field definded as `SerializedPKRelatedField` should be shown as an
array of child serializer objects in a response value definition in API
view but it is shown as an array of primary key values (usually
`integer` type) of a child serializer.

This fixes it by introducing a new `FieldInspector` to handle the field.
It doesn't affect any other type fields.
This commit is contained in:
Tatsushi Demachi 2018-09-15 06:41:01 +09:00
parent d061b975c4
commit c2c60ce2d7
2 changed files with 11 additions and 1 deletions

View File

@ -279,6 +279,7 @@ SWAGGER_SETTINGS = {
'utilities.custom_inspectors.NullableBooleanFieldInspector',
'utilities.custom_inspectors.CustomChoiceFieldInspector',
'utilities.custom_inspectors.TagListFieldInspector',
'utilities.custom_inspectors.SerializedPKRelatedFieldInspector',
'drf_yasg.inspectors.CamelCaseJSONFilter',
'drf_yasg.inspectors.ReferencingSerializerInspector',
'drf_yasg.inspectors.RelatedFieldInspector',

View File

@ -4,7 +4,16 @@ from rest_framework.fields import ChoiceField
from taggit_serializer.serializers import TagListSerializerField
from extras.api.customfields import CustomFieldsSerializer
from utilities.api import ChoiceField
from utilities.api import ChoiceField, SerializedPKRelatedField
class SerializedPKRelatedFieldInspector(FieldInspector):
def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs):
SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, use_references, **kwargs)
if isinstance(field, SerializedPKRelatedField):
return self.probe_field_inspectors(field.serializer(), ChildSwaggerType, use_references)
return NotHandled
class TagListFieldInspector(FieldInspector):