Skip to content
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

fix: Do not wrap an example list value in another list #1250

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,18 +1408,18 @@ class TmpView(views.APIView):

def build_listed_example_value(value: Any, paginator, direction):
if not paginator or direction == 'request':
return [value]
return value if type(value) is list else [value]

sentinel = object()
schema = paginator.get_paginated_response_schema(sentinel)

if schema is sentinel:
return [value]
return value if type(value) is list else [value]

def drilldown_schema_example(schema, sentinel):
# Recursively travel schema properties to build example.
if schema is sentinel:
return [value]
return value if type(value) is list else [value]
if 'properties' in schema.keys():
return {
field_name: drilldown_schema_example(field_schema, sentinel)
Expand Down
58 changes: 55 additions & 3 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class BSerializer(serializers.Serializer):
value={"field": 222},
request_only=True,
),
OpenApiExample(
'Serializer C Example List',
value=[{"field": 333}, {"field": 444}],
response_only=True,
),
]
)
class CSerializer(serializers.Serializer):
Expand Down Expand Up @@ -180,7 +185,16 @@ class PaginatedExamplesViewSet(ExampleTestWithExtendedViewSet):
'results': [{'field': 111}],
},
'summary': 'Serializer C Example RO'
}
},
'SerializerCExampleList': {
'value': {
'count': 123,
'next': 'http://api.example.org/accounts/?offset=400&limit=100',
'previous': 'http://api.example.org/accounts/?offset=200&limit=100',
'results': [{'field': 333}, {'field': 444}],
},
'summary': 'Serializer C Example List'
},
}


Expand Down Expand Up @@ -236,11 +250,22 @@ class PaginatedExamplesViewSet(ExampleTestWithExtendedViewSet):
'results': [{'field': 111}],
},
'summary': 'Serializer C Example RO'
}
},
'SerializerCExampleList': {
'value': {
'pagination': {
'count': 123,
'next': 'http://api.example.org/accounts/?offset=400&limit=100',
'previous': 'http://api.example.org/accounts/?offset=200&limit=100',
},
'results': [{'field': 333}, {'field': 444}],
},
'summary': 'Serializer C Example List'
},
}


def test_example_request_response_listed_examples(no_warnings):
def test_example_request_response_singular_examples(no_warnings):
@extend_schema(
request=ASerializer(many=True),
responses=ASerializer(many=True),
Expand All @@ -263,6 +288,29 @@ class XView(generics.CreateAPIView):
}


def test_example_request_response_listed_examples(no_warnings):
@extend_schema(
request=ASerializer(many=True),
responses=ASerializer(many=True),
examples=[
OpenApiExample('Ex', [{'id': '2345'}, {'id': '2345'}])
]
)
class XView(generics.CreateAPIView):
pass

schema = generate_schema('e', view=XView)
operation = schema['paths']['/e']['post']
assert operation['requestBody']['content']['application/json'] == {
'schema': {'type': 'array', 'items': {'$ref': '#/components/schemas/A'}},
'examples': {'Ex': {'value': [{'id': '2345'}, {'id': '2345'}]}}
}
assert operation['responses']['201']['content']['application/json'] == {
'schema': {'type': 'array', 'items': {'$ref': '#/components/schemas/A'}},
'examples': {'Ex': {'value': [{'id': '2345'}, {'id': '2345'}]}}
}


def test_examples_list_detection_on_non_200_decoration(no_warnings):
class ExceptionSerializer(serializers.Serializer):
api_status_code = serializers.CharField()
Expand Down Expand Up @@ -375,5 +423,9 @@ class PaginatedExamplesViewSet(ExampleTestWithExtendedViewSet):
'SerializerCExampleRO': {
'value': [{'field': 111}],
'summary': 'Serializer C Example RO'
},
'SerializerCExampleList': {
'value': [{'field': 333}, {'field': 444}],
'summary': 'Serializer C Example List'
}
}
5 changes: 5 additions & 0 deletions tests/test_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ paths:
value:
- field: 111
summary: Serializer C Example RO
SerializerCExampleList:
value:
- field: 333
- field: 444
summary: Serializer C Example List
description: ''
post:
operationId: schema_create
Expand Down
Loading