-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/bugfix PATCH & Serializer(partial=True) behaviour.
- Loading branch information
Showing
5 changed files
with
316 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from unittest import mock | ||
|
||
from django.db import models | ||
from rest_framework import mixins, parsers, serializers, viewsets | ||
|
||
from tests import assert_schema, generate_schema | ||
|
||
|
||
class PNM1(models.Model): | ||
field = models.IntegerField() | ||
|
||
|
||
class PNM2(models.Model): | ||
field_relation = models.ForeignKey(PNM1, on_delete=models.CASCADE) | ||
|
||
|
||
class XSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = PNM1 | ||
fields = '__all__' | ||
|
||
|
||
class YSerializer(serializers.ModelSerializer): | ||
field_relation = XSerializer() | ||
field_relation_partial = XSerializer(source='field_relation', partial=True) | ||
|
||
class Meta: | ||
model = PNM2 | ||
fields = '__all__' | ||
|
||
|
||
class XViewset(mixins.UpdateModelMixin, viewsets.GenericViewSet): | ||
parser_classes = [parsers.JSONParser] | ||
serializer_class = YSerializer | ||
queryset = PNM2.objects.all() | ||
|
||
|
||
@mock.patch('drf_spectacular.settings.spectacular_settings.COMPONENT_SPLIT_REQUEST', False) | ||
def test_nested_partial_on_split_request_false(no_warnings): | ||
# without split request, PatchedY and Y have the same properties (minus required). | ||
# PATCH only modifies outermost serializer, nested serializers must stay unaffected. | ||
assert_schema( | ||
generate_schema('/x/', XViewset), 'tests/test_split_request_false.yml' | ||
) | ||
|
||
|
||
@mock.patch('drf_spectacular.settings.spectacular_settings.COMPONENT_SPLIT_REQUEST', True) | ||
def test_nested_partial_on_split_request_true(no_warnings): | ||
# with split request, behaves like above, however response schemas are always unpatched. | ||
# nested request serializers are only affected by their manual partial flag and not due to PATCH. | ||
assert_schema( | ||
generate_schema('/x/', XViewset), 'tests/test_split_request_true.yml' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: '' | ||
version: 0.0.0 | ||
paths: | ||
/x/{id}/: | ||
put: | ||
operationId: x_update | ||
description: '' | ||
parameters: | ||
- in: path | ||
name: id | ||
schema: | ||
type: integer | ||
description: A unique integer value identifying this pn m2. | ||
required: true | ||
tags: | ||
- x | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Y' | ||
required: true | ||
security: | ||
- cookieAuth: [] | ||
- basicAuth: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Y' | ||
description: '' | ||
patch: | ||
operationId: x_partial_update | ||
description: '' | ||
parameters: | ||
- in: path | ||
name: id | ||
schema: | ||
type: integer | ||
description: A unique integer value identifying this pn m2. | ||
required: true | ||
tags: | ||
- x | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/PatchedY' | ||
security: | ||
- cookieAuth: [] | ||
- basicAuth: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Y' | ||
description: '' | ||
components: | ||
schemas: | ||
PatchedX: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
readOnly: true | ||
field: | ||
type: integer | ||
PatchedY: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
readOnly: true | ||
field_relation: | ||
$ref: '#/components/schemas/X' | ||
field_relation_partial: | ||
$ref: '#/components/schemas/PatchedX' | ||
X: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
readOnly: true | ||
field: | ||
type: integer | ||
required: | ||
- field | ||
- id | ||
Y: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
readOnly: true | ||
field_relation: | ||
$ref: '#/components/schemas/X' | ||
field_relation_partial: | ||
$ref: '#/components/schemas/PatchedX' | ||
required: | ||
- field_relation | ||
- field_relation_partial | ||
- id | ||
securitySchemes: | ||
basicAuth: | ||
type: http | ||
scheme: basic | ||
cookieAuth: | ||
type: apiKey | ||
in: cookie | ||
name: Session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: '' | ||
version: 0.0.0 | ||
paths: | ||
/x/{id}/: | ||
put: | ||
operationId: x_update | ||
description: '' | ||
parameters: | ||
- in: path | ||
name: id | ||
schema: | ||
type: integer | ||
description: A unique integer value identifying this pn m2. | ||
required: true | ||
tags: | ||
- x | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/YRequest' | ||
required: true | ||
security: | ||
- cookieAuth: [] | ||
- basicAuth: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Y' | ||
description: '' | ||
patch: | ||
operationId: x_partial_update | ||
description: '' | ||
parameters: | ||
- in: path | ||
name: id | ||
schema: | ||
type: integer | ||
description: A unique integer value identifying this pn m2. | ||
required: true | ||
tags: | ||
- x | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/PatchedYRequest' | ||
security: | ||
- cookieAuth: [] | ||
- basicAuth: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Y' | ||
description: '' | ||
components: | ||
schemas: | ||
PatchedXRequest: | ||
type: object | ||
properties: | ||
field: | ||
type: integer | ||
PatchedYRequest: | ||
type: object | ||
properties: | ||
field_relation: | ||
$ref: '#/components/schemas/XRequest' | ||
field_relation_partial: | ||
$ref: '#/components/schemas/PatchedXRequest' | ||
X: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
readOnly: true | ||
field: | ||
type: integer | ||
required: | ||
- field | ||
- id | ||
XRequest: | ||
type: object | ||
properties: | ||
field: | ||
type: integer | ||
required: | ||
- field | ||
Y: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
readOnly: true | ||
field_relation: | ||
$ref: '#/components/schemas/X' | ||
field_relation_partial: | ||
$ref: '#/components/schemas/X' | ||
required: | ||
- field_relation | ||
- field_relation_partial | ||
- id | ||
YRequest: | ||
type: object | ||
properties: | ||
field_relation: | ||
$ref: '#/components/schemas/XRequest' | ||
field_relation_partial: | ||
$ref: '#/components/schemas/PatchedXRequest' | ||
required: | ||
- field_relation | ||
- field_relation_partial | ||
securitySchemes: | ||
basicAuth: | ||
type: http | ||
scheme: basic | ||
cookieAuth: | ||
type: apiKey | ||
in: cookie | ||
name: Session |