Skip to content

Use PrimaryKeyRelatedField pkfield in openapi #8315

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

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ def map_field(self, field):
'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
Expand Down
17 changes: 17 additions & 0 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rest_framework.request import Request
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator

from ..models import BasicModel
from . import views


Expand Down Expand Up @@ -129,6 +130,22 @@ class Meta:
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):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy