Use PrimaryKeyRelatedField pkfield in openapi (#8315)

This commit is contained in:
kanazawa 2022-12-11 01:48:43 +09:00 committed by GitHub
parent 59ae95b22f
commit b79099f7ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -387,6 +387,8 @@ class AutoSchema(ViewInspector):
'items': self.map_field(field.child_relation)
}
if isinstance(field, serializers.PrimaryKeyRelatedField):
if getattr(field, "pk_field", False):
return self.map_field(field=field.pk_field)
model = getattr(field.queryset, 'model', None)
if model is not None:
model_field = model._meta.pk

View File

@ -19,6 +19,7 @@ from rest_framework.renderers import (
from rest_framework.request import Request
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator
from ..models import BasicModel
from . import views
@ -144,6 +145,22 @@ class TestFieldMapping(TestCase):
assert data['properties']['ro_field']['nullable'], "ro_field nullable must be true"
assert data['properties']['ro_field']['readOnly'], "ro_field read_only must be true"
def test_primary_key_related_field(self):
class PrimaryKeyRelatedFieldSerializer(serializers.Serializer):
basic = serializers.PrimaryKeyRelatedField(queryset=BasicModel.objects.all())
uuid = serializers.PrimaryKeyRelatedField(queryset=BasicModel.objects.all(),
pk_field=serializers.UUIDField())
char = serializers.PrimaryKeyRelatedField(queryset=BasicModel.objects.all(),
pk_field=serializers.CharField())
serializer = PrimaryKeyRelatedFieldSerializer()
inspector = AutoSchema()
data = inspector.map_serializer(serializer=serializer)
assert data['properties']['basic']['type'] == "integer"
assert data['properties']['uuid']['format'] == "uuid"
assert data['properties']['char']['type'] == "string"
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
class TestOperationIntrospection(TestCase):