mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-04 17:24:30 +03:00
Use PrimaryKeyRelatedField pkfield in openapi (#8315)
This commit is contained in:
parent
59ae95b22f
commit
b79099f7ba
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user