jnowak
jnowak

Reputation: 695

drf spectacular generating wrong example per response

Having 2 response schemas for response, wanted to add examples to document error codes.

Wanted error code response example:

{
    "api_status_code": "DATE_PARSE_ERROR",
      "extra": {
        "details": "asdf"
      }
}

but in swagger-ui I'm getting wrapped my error code object with paginated schema within results key:

enter image description here


class ExceptionSerializer(serializers.Serializer):
    api_status_code = serializers.CharField()
    extra = serializers.DictField(required=False)

@extend_schema_view(
    get=extend_schema(
        responses={
            200: OpenApiResponse(
                response=s.ArchiveListViewSerializer,
                examples=[],
            ),
            400: OpenApiResponse(
                response=ExceptionSerializer,
                examples=[
                    OpenApiExample(
                        "DATE_PARSE_ERROR",
                        value={"api_status_code": "DATE_PARSE_ERROR", "extra": {"details": "asdf"}},
                        status_codes=[400],
                        response_only=True,
                    )
                ],
            ),
        },
    )
)
class ArchiveListView(LoginRequiredMixin, ListAPIView):
    model = m.Archive
    serializer_class = s.ArchiveListViewSerializer
    pagination_class = LimitOffsetPagination

I checked what drf spectacular generates and the example is wrong.

      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PaginatedArchiveListViewList'
          description: ''
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Exception'
              examples:
                DATEPARSEERROR:
                  value:
                    count: 123
                    next: http://api.example.org/accounts/?offset=400&limit=100
                    previous: http://api.example.org/accounts/?offset=200&limit=100
                    results:
                    - api_status_code: DATE_PARSE_ERROR
                      extra:
                        details: asdf
                  summary: DATE_PARSE_ERROR
          description: ''

However the response schemas are looking good, it's all about example.

enter image description here

How do I tackle this?

Upvotes: 2

Views: 2706

Answers (1)

jnowak
jnowak

Reputation: 695

Ended up being a bug https://github.com/tfranzel/drf-spectacular/issues/872 Fixed now

Upvotes: 2

Related Questions