10
OpenApiTypes.OBJECT
means that the response object can have any amount of fields. Of course Swagger UI cannot know those fields in advance and thus it shows {}
, which may be unintuitive but it is correct.
What you want is to give your response some structure. Everything in spectacular revolves around serializers/components. If you don’t want to have explicit serializers, you can create implicit ones with inline_serializer
from drf_spectacular.utils import extend_schema, OpenApiExample, inline_serializer
@extend_schema(
examples=[OpenApiExample(
value=[
{'title': 'A title'},
{'title': 'Another title'},
],
)],
responses={
200: inline_serializer(
name='Article',
fields={
'title': serializers.CharField(),
}
)
}
)
@api_view(['GET'])
def list_articles(request):
pass
0
Also it is possible to use simple dictionary instead of inline_serializer
:
@extend_schema(
...
responses={
(200, 'text/html'): {
'description': 'Simple HTML page',
'type': 'string',
'example': '<html>Example text</html>'
},
(202, 'application/json'): {
'description': 'JSON response',
'type': 'object',
'properties': {
'title': {
'type': 'string',
'minLength': 1,
'maxLength': 128
}
},
'required': [
'title'
]
},
}
)
...
- [Django]-Django : custom management command not registered using app config
- [Django]-Django: two sessions in the same browser
Source:stackexchange.com