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

Support annotating a PATCH @action with @extend_schema to use PatchedTRequest #1227

Open
johnthagen opened this issue Apr 25, 2024 · 3 comments

Comments

@johnthagen
Copy link
Contributor

I have an @action of the form:

    @action(
        methods=[HTTPMethod.PATCH],
        detail=False,
        url_path="bulk-partial-update",
    )
    def bulk_partial_update(self, request: Request) -> Response:
        ...

And a ViewSet of the form:

@extend_schema_view(
    bulk_partial_update=extend_schema(
        request=TSerializer(many=True), responses=TSerializer(many=True)
    ),
)
class TViewSet(ModelViewSet):
    queryset = T.objects.all()
    ...

I was hoping that because the @action was tagged as HTTPMethod.PATCH that drf-spectacular would automatically use the PatchedTRequest form of the generated T for the request side of the API since PATCH is used in HTTP for partial updates.

But when I looked at the generated OpenAPI spec, I found it looked like:

  /T/bulk-partial-update/:
    patch:
      operationId: t_bulk_partial_update_partial_update
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/TRequest'
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/T'
          description: ''

Specifically I was hoping that requestBody would use PatchedTRequest.

@johnthagen
Copy link
Contributor Author

This code seems relevant to this issue

def is_patched_serializer(serializer, direction) -> bool:
return bool(
spectacular_settings.COMPONENT_SPLIT_PATCH
and getattr(serializer, 'partial', None)
and not getattr(serializer, 'read_only', None)
and not (spectacular_settings.COMPONENT_SPLIT_REQUEST and direction == 'response')
)

@johnthagen
Copy link
Contributor Author

I was able to manually add partial=True to work around this.

@extend_schema_view(
    bulk_partial_update=extend_schema(
        request=TSerializer(many=True, partial=True), responses=TSerializer(many=True)
    ),
)

@johnthagen
Copy link
Contributor Author

johnthagen commented May 1, 2024

So the only issue I have remaining is that for bulk partial updates, I need to have the required id present, but all other fields marked optional. partial=True solves the part of making all fields optional, but is there any clean way to also describe "even for this request, the id field must exist"?

As an example of what the partial update data looks like:

[
  {"id": 1, "name": "fred"},
  {"id": 2, "age": 10}
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant