Skip to content

Commit

Permalink
do not wrap an example list value in another list
Browse files Browse the repository at this point in the history
  • Loading branch information
Perrin Samuels committed May 31, 2024
1 parent 2501bfd commit b2c8237
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
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

0 comments on commit b2c8237

Please sign in to comment.