-
Notifications
You must be signed in to change notification settings - Fork 270
/
test_regressions.py
535 lines (396 loc) · 18.3 KB
/
test_regressions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import uuid
from unittest import mock
from django.conf.urls import url
from django.db.models import fields
from django.db import models
from django.urls import path
from rest_framework import serializers, viewsets, mixins, routers, views, generics
from rest_framework.decorators import action, api_view
from rest_framework.views import APIView
from drf_spectacular.extensions import OpenApiSerializerExtension
from drf_spectacular.generators import SchemaGenerator
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_serializer
from drf_spectacular.validation import validate_schema
from tests import generate_schema, get_response_schema, get_request_schema
def test_primary_key_read_only_queryset_not_found(no_warnings):
# the culprit - looks like a feature not a bug.
# https://github.com/encode/django-rest-framework/blame/4d9f9eb192c5c1ffe4fa9210b90b9adbb00c3fdd/rest_framework/utils/field_mapping.py#L271
class M1(models.Model):
pass # pragma: no cover
class M2(models.Model):
m1_r = models.ForeignKey(M1, on_delete=models.CASCADE)
m1_rw = models.ForeignKey(M1, on_delete=models.CASCADE)
class M2Serializer(serializers.ModelSerializer):
class Meta:
fields = ['m1_rw', 'm1_r']
read_only_fields = ['m1_r'] # this produces the bug
model = M2
class M2Viewset(viewsets.ReadOnlyModelViewSet):
serializer_class = M2Serializer
queryset = M2.objects.none()
schema = generate_schema('m2', M2Viewset)
validate_schema(schema)
props = schema['components']['schemas']['M2']['properties']
assert props['m1_rw']['type'] == 'integer'
assert props['m1_r']['type'] == 'integer'
def test_path_implicit_required(no_warnings):
class M2Serializer(serializers.Serializer):
pass # pragma: no cover
class M2Viewset(viewsets.GenericViewSet):
serializer_class = M2Serializer
@extend_schema(parameters=[OpenApiParameter('id', str, 'path')])
def retrieve(self, request, *args, **kwargs):
pass # pragma: no cover
schema = generate_schema('m2', M2Viewset)
validate_schema(schema)
def test_free_form_responses(no_warnings):
class XAPIView(APIView):
@extend_schema(responses={200: OpenApiTypes.OBJECT})
def get(self, request):
pass # pragma: no cover
class YAPIView(APIView):
@extend_schema(responses=OpenApiTypes.OBJECT)
def get(self, request):
pass # pragma: no cover
generator = SchemaGenerator(patterns=[
url(r'^x$', XAPIView.as_view(), name='x'),
url(r'^y$', YAPIView.as_view(), name='y'),
])
schema = generator.get_schema(request=None, public=True)
validate_schema(schema)
@mock.patch(
target='drf_spectacular.settings.spectacular_settings.APPEND_COMPONENTS',
new={'schemas': {'SomeExtraComponent': {'type': 'integer'}}}
)
def test_append_extra_components(no_warnings):
class XSerializer(serializers.Serializer):
id = serializers.UUIDField()
class XAPIView(APIView):
@extend_schema(responses={200: XSerializer})
def get(self, request):
pass # pragma: no cover
generator = SchemaGenerator(patterns=[
url(r'^x$', XAPIView.as_view(), name='x'),
])
schema = generator.get_schema(request=None, public=True)
assert len(schema['components']['schemas']) == 2
validate_schema(schema)
def test_serializer_retrieval_from_view(no_warnings):
class UnusedSerializer(serializers.Serializer):
pass # pragma: no cover
class XSerializer(serializers.Serializer):
id = serializers.UUIDField()
class YSerializer(serializers.Serializer):
id = serializers.UUIDField()
class X1Viewset(mixins.ListModelMixin, viewsets.GenericViewSet):
serializer_class = UnusedSerializer
def get_serializer(self):
return XSerializer()
class X2Viewset(mixins.ListModelMixin, viewsets.GenericViewSet):
def get_serializer_class(self):
return YSerializer
router = routers.SimpleRouter()
router.register('x1', X1Viewset, basename='x1')
router.register('x2', X2Viewset, basename='x2')
generator = SchemaGenerator(patterns=router.urls)
schema = generator.get_schema(request=None, public=True)
validate_schema(schema)
assert len(schema['components']['schemas']) == 2
assert 'Unused' not in schema['components']['schemas']
def test_retrieve_on_apiview_get(no_warnings):
class XSerializer(serializers.Serializer):
id = serializers.UUIDField()
class XApiView(APIView):
authentication_classes = []
@extend_schema(
parameters=[OpenApiParameter('id', OpenApiTypes.INT, OpenApiParameter.PATH)],
responses={200: XSerializer},
)
def get(self, request):
pass # pragma: no cover
schema = generate_schema('x', view=XApiView)
operation = schema['paths']['/x']['get']
assert operation['operationId'] == 'x_retrieve'
operation_schema = get_response_schema(operation)
assert '$ref' in operation_schema and 'type' not in operation_schema
def test_list_on_apiview_get(no_warnings):
class XSerializer(serializers.Serializer):
id = serializers.UUIDField()
class XApiView(APIView):
authentication_classes = []
@extend_schema(
parameters=[OpenApiParameter('id', OpenApiTypes.INT, OpenApiParameter.PATH)],
responses={200: XSerializer(many=True)},
)
def get(self, request):
pass # pragma: no cover
schema = generate_schema('x', view=XApiView)
operation = schema['paths']['/x']['get']
assert operation['operationId'] == 'x_list'
operation_schema = get_response_schema(operation)
assert operation_schema['type'] == 'array'
def test_multi_method_action(no_warnings):
class DummySerializer(serializers.Serializer):
id = serializers.UUIDField()
class UpdateSerializer(serializers.Serializer):
id = serializers.UUIDField()
class CreateSerializer(serializers.Serializer):
id = serializers.UUIDField()
class XViewset(viewsets.GenericViewSet):
serializer_class = DummySerializer
# basic usage
@extend_schema(request=UpdateSerializer, methods=['PUT'])
@extend_schema(request=CreateSerializer, methods=['POST'])
@action(detail=False, methods=['PUT', 'POST'])
def multi(self, request, *args, **kwargs):
pass # pragma: no cover
# bolt-on decorator variation
@extend_schema(request=CreateSerializer)
@action(detail=False, methods=['POST'])
def multi2(self, request, *args, **kwargs):
pass # pragma: no cover
@extend_schema(request=UpdateSerializer)
@multi2.mapping.put
def multi2put(self, request, *args, **kwargs):
pass # pragma: no cover
schema = generate_schema('x', XViewset)
validate_schema(schema)
def get_req_body(s):
return s['requestBody']['content']['application/json']['schema']['$ref']
assert get_req_body(schema['paths']['/x/multi/']['put']) == '#/components/schemas/Update'
assert get_req_body(schema['paths']['/x/multi/']['post']) == '#/components/schemas/Create'
assert get_req_body(schema['paths']['/x/multi2/']['put']) == '#/components/schemas/Update'
assert get_req_body(schema['paths']['/x/multi2/']['post']) == '#/components/schemas/Create'
def test_serializer_class_on_apiview(no_warnings):
class XSerializer(serializers.Serializer):
field = serializers.UUIDField()
class XView(views.APIView):
serializer_class = XSerializer # not supported by DRF but pick it up anyway
def get(self, request):
pass # pragma: no cover
def post(self, request):
pass # pragma: no cover
schema = generate_schema('x', view=XView)
validate_schema(schema)
comp = '#/components/schemas/X'
assert get_response_schema(schema['paths']['/x']['get'])['$ref'] == comp
assert get_response_schema(schema['paths']['/x']['post'])['$ref'] == comp
assert schema['paths']['/x']['post']['requestBody']['content']['application/json']['schema']['$ref'] == comp
def test_customized_list_serializer():
class X(models.Model):
position = models.IntegerField()
class XSerializer(serializers.ModelSerializer):
class Meta:
model = X
fields = ("id", "position")
class XListUpdateSerializer(serializers.ListSerializer):
child = XSerializer()
class XAPIView(generics.GenericAPIView):
model = X
serializer_class = XListUpdateSerializer
def put(self, request, *args, **kwargs):
pass # pragma: no cover
schema = generate_schema('x', view=XAPIView)
validate_schema(schema)
operation = schema['paths']['/x']['put']
comp = '#/components/schemas/X'
assert get_request_schema(operation)['type'] == 'array'
assert get_request_schema(operation)['items']['$ref'] == comp
assert get_response_schema(operation)['type'] == 'array'
assert get_response_schema(operation)['items']['$ref'] == comp
assert operation['operationId'] == 'x_update'
assert len(schema['components']['schemas']) == 1 and 'X' in schema['components']['schemas']
def test_api_view_decorator(no_warnings):
@extend_schema(responses=OpenApiTypes.FLOAT)
@api_view(['GET'])
def pi(request):
pass # pragma: no cover
schema = generate_schema('x', view_function=pi)
validate_schema(schema)
operation = schema['paths']['/x']['get']
assert get_response_schema(operation)['type'] == 'number'
def test_api_view_decorator_multi(no_warnings):
@extend_schema(request=OpenApiTypes.FLOAT, responses=OpenApiTypes.INT, methods=['POST'])
@extend_schema(responses=OpenApiTypes.FLOAT, methods=['GET'])
@api_view(['GET', 'POST'])
def pi(request):
pass # pragma: no cover
schema = generate_schema('x', view_function=pi)
validate_schema(schema)
operation = schema['paths']['/x']['get']
assert get_response_schema(operation)['type'] == 'number'
operation = schema['paths']['/x']['post']
assert get_request_schema(operation)['type'] == 'number'
assert get_response_schema(operation)['type'] == 'integer'
def test_pk_and_no_id(no_warnings):
class XModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
class YModel(models.Model):
x = models.OneToOneField(XModel, primary_key=True, on_delete=models.CASCADE)
class YSerializer(serializers.ModelSerializer):
class Meta:
model = YModel
fields = '__all__'
class YViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = YSerializer
queryset = YModel.objects.all()
schema = generate_schema('y', YViewSet)
validate_schema(schema)
assert schema['components']['schemas']['Y']['properties']['x']['format'] == 'uuid'
def test_drf_format_suffix_parameter(no_warnings):
from rest_framework.urlpatterns import format_suffix_patterns
@extend_schema(responses=OpenApiTypes.FLOAT)
@api_view(['GET'])
def pi(request, format=None):
pass # pragma: no cover
urlpatterns = [path(r'/pi', pi)]
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
generator = SchemaGenerator(patterns=urlpatterns)
schema = generator.get_schema(request=None, public=True)
assert len(schema['paths']) == 2
format_parameter = schema['paths']['/pi{format}']['get']['parameters'][0]
assert format_parameter['name'] == 'format'
assert format_parameter['required'] is False
assert format_parameter['in'] == 'path'
def test_regex_path_parameter_discovery(no_warnings):
@extend_schema(responses=OpenApiTypes.FLOAT)
@api_view(['GET'])
def pi(request, foo):
pass # pragma: no cover
urlpatterns = [url(r'^/pi/<int:precision>', pi)]
generator = SchemaGenerator(patterns=urlpatterns)
schema = generator.get_schema(request=None, public=True)
parameter = schema['paths']['/pi/{precision}']['get']['parameters'][0]
assert parameter['name'] == 'precision'
assert parameter['in'] == 'path'
assert parameter['schema']['type'] == 'integer'
def test_lib_serializer_naming_collision_resolution(no_warnings):
""" parity test in tests.test_warnings.test_serializer_name_reuse """
def x_lib1():
class XSerializer(serializers.Serializer):
x = serializers.UUIDField()
return XSerializer
def x_lib2():
class XSerializer(serializers.Serializer):
x = serializers.IntegerField()
return XSerializer
x_lib1, x_lib2 = x_lib1(), x_lib2()
class XAPIView(APIView):
@extend_schema(request=x_lib1, responses=x_lib2)
def post(self, request):
pass # pragma: no cover
class Lib2XSerializerRename(OpenApiSerializerExtension):
target_class = x_lib2 # also accepts import strings
def get_name(self):
return 'RenamedLib2X'
schema = generate_schema('/x', view=XAPIView)
operation = schema['paths']['/x']['post']
assert get_request_schema(operation)['$ref'] == '#/components/schemas/X'
assert get_response_schema(operation)['$ref'] == '#/components/schemas/RenamedLib2X'
def test_owned_serializer_naming_override_with_ref_name(no_warnings):
def x_owned1():
class XSerializer(serializers.Serializer):
x = serializers.UUIDField()
return XSerializer
def x_owned2():
class XSerializer(serializers.Serializer):
x = serializers.IntegerField()
class Meta:
ref_name = 'Y'
return XSerializer
x_owned1, x_owned2 = x_owned1(), x_owned2()
class XAPIView(APIView):
@extend_schema(request=x_owned1, responses=x_owned2)
def post(self, request):
pass # pragma: no cover
schema = generate_schema('/x', view=XAPIView)
operation = schema['paths']['/x']['post']
assert get_request_schema(operation)['$ref'] == '#/components/schemas/X'
assert get_response_schema(operation)['$ref'] == '#/components/schemas/Y'
def test_custom_model_field_from_typed_field(no_warnings):
class CustomIntegerField(fields.IntegerField):
pass # pragma: no cover
class CustomTypedFieldModel(models.Model):
custom_int_field = CustomIntegerField()
class XSerializer(serializers.ModelSerializer):
class Meta:
model = CustomTypedFieldModel
fields = '__all__'
class XAPIView(APIView):
@extend_schema(responses=XSerializer)
def get(self, request):
pass # pragma: no cover
schema = generate_schema('/x', view=XAPIView)
component = schema['components']['schemas']['X']
assert component['properties']['custom_int_field']['type'] == 'integer'
def test_custom_model_field_from_base_field(no_warnings):
class CustomIntegerField(fields.Field):
def get_internal_type(self):
return 'IntegerField'
class CustomBaseFieldModel(models.Model):
custom_int_field = CustomIntegerField()
class XSerializer(serializers.ModelSerializer):
class Meta:
model = CustomBaseFieldModel
fields = '__all__'
class XAPIView(APIView):
@extend_schema(responses=XSerializer)
def get(self, request):
pass # pragma: no cover
schema = generate_schema('/x', view=XAPIView)
component = schema['components']['schemas']['X']
assert component['properties']['custom_int_field']['type'] == 'integer'
def test_follow_field_source_through_intermediate_property(no_warnings):
class FieldSourceTraversalModel2(models.Model):
y = models.IntegerField(choices=[(1, '1'), (2, '2'), (3, '3')])
class FieldSourceTraversalModel1(models.Model):
@property
def x(self) -> FieldSourceTraversalModel2: # property is required for traversal
return # pragma: no cover
class XSerializer(serializers.ModelSerializer):
x = serializers.ReadOnlyField(source='x.y')
class Meta:
model = FieldSourceTraversalModel1
fields = '__all__'
class XAPIView(APIView):
@extend_schema(responses=XSerializer)
def get(self, request):
pass # pragma: no cover
# this checks if field type is correctly estimated AND field was initialized
# with the model parameters (choices)
schema = generate_schema('/x', view=XAPIView)
assert schema['components']['schemas']['X']['properties']['x']['readOnly'] is True
assert 'enum' in schema['components']['schemas']['XEnum']
assert schema['components']['schemas']['XEnum']['type'] == 'integer'
def test_viewset_list_with_envelope(no_warnings):
class XSerializer(serializers.Serializer):
x = serializers.IntegerField()
def enveloper(serializer_class, list):
@extend_schema_serializer(many=False)
class EnvelopeSerializer(serializers.Serializer):
status = serializers.BooleanField()
data = XSerializer(many=list)
class Meta:
ref_name = 'Enveloped{}{}'.format(
serializer_class.__name__.replace("Serializer", ""),
"List" if list else "",
)
return EnvelopeSerializer
class XViewset(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
@extend_schema(responses=enveloper(XSerializer, True))
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs) # pragma: no cover
@extend_schema(
responses=enveloper(XSerializer, False),
parameters=[OpenApiParameter('id', int, OpenApiParameter.PATH)],
)
def retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs) # pragma: no cover
schema = generate_schema('x', viewset=XViewset)
operation_list = schema['paths']['/x/']['get']
assert operation_list['operationId'] == 'x_list'
assert get_response_schema(operation_list)['$ref'] == '#/components/schemas/EnvelopedXList'
operation_retrieve = schema['paths']['/x/{id}/']['get']
assert operation_retrieve['operationId'] == 'x_retrieve'
assert get_response_schema(operation_retrieve)['$ref'] == '#/components/schemas/EnvelopedX'