@@ -82,7 +82,18 @@ def test_path_without_parameters(self):
82
82
assert operation == {
83
83
'operationId' : 'ListExamples' ,
84
84
'parameters' : [],
85
- 'responses' : {'200' : {'content' : {'application/json' : {'schema' : {}}}}},
85
+ 'responses' : {
86
+ '200' : {
87
+ 'content' : {
88
+ 'application/json' : {
89
+ 'schema' : {
90
+ 'type' : 'array' ,
91
+ 'items' : {},
92
+ },
93
+ },
94
+ },
95
+ },
96
+ },
86
97
}
87
98
88
99
def test_path_with_id_parameter (self ):
@@ -184,6 +195,83 @@ class View(generics.GenericAPIView):
184
195
assert list (schema ['properties' ]['nested' ]['properties' ].keys ()) == ['number' ]
185
196
assert schema ['properties' ]['nested' ]['required' ] == ['number' ]
186
197
198
+ def test_list_response_body_generation (self ):
199
+ """Test that an array schema is returned for list views."""
200
+ path = '/'
201
+ method = 'GET'
202
+
203
+ class ItemSerializer (serializers .Serializer ):
204
+ text = serializers .CharField ()
205
+
206
+ class View (generics .GenericAPIView ):
207
+ serializer_class = ItemSerializer
208
+
209
+ view = create_view (
210
+ View ,
211
+ method ,
212
+ create_request (path ),
213
+ )
214
+ inspector = AutoSchema ()
215
+ inspector .view = view
216
+
217
+ responses = inspector ._get_responses (path , method )
218
+ assert responses == {
219
+ '200' : {
220
+ 'content' : {
221
+ 'application/json' : {
222
+ 'schema' : {
223
+ 'type' : 'array' ,
224
+ 'items' : {
225
+ 'properties' : {
226
+ 'text' : {
227
+ 'type' : 'string' ,
228
+ },
229
+ },
230
+ 'required' : ['text' ],
231
+ },
232
+ },
233
+ },
234
+ },
235
+ },
236
+ }
237
+
238
+ def test_retrieve_response_body_generation (self ):
239
+ """Test that a list of properties is returned for retrieve item views."""
240
+ path = '/{id}/'
241
+ method = 'GET'
242
+
243
+ class ItemSerializer (serializers .Serializer ):
244
+ text = serializers .CharField ()
245
+
246
+ class View (generics .GenericAPIView ):
247
+ serializer_class = ItemSerializer
248
+
249
+ view = create_view (
250
+ View ,
251
+ method ,
252
+ create_request (path ),
253
+ )
254
+ inspector = AutoSchema ()
255
+ inspector .view = view
256
+
257
+ responses = inspector ._get_responses (path , method )
258
+ assert responses == {
259
+ '200' : {
260
+ 'content' : {
261
+ 'application/json' : {
262
+ 'schema' : {
263
+ 'properties' : {
264
+ 'text' : {
265
+ 'type' : 'string' ,
266
+ },
267
+ },
268
+ 'required' : ['text' ],
269
+ },
270
+ },
271
+ },
272
+ },
273
+ }
274
+
187
275
def test_operation_id_generation (self ):
188
276
path = '/'
189
277
method = 'GET'
@@ -226,10 +314,11 @@ def test_serializer_datefield(self):
226
314
inspector .view = view
227
315
228
316
responses = inspector ._get_responses (path , method )
229
- response_schema = responses ['200' ]['content' ]['application/json' ]['schema' ]['properties' ]
230
- assert response_schema ['date' ]['type' ] == response_schema ['datetime' ]['type' ] == 'string'
231
- assert response_schema ['date' ]['format' ] == 'date'
232
- assert response_schema ['datetime' ]['format' ] == 'date-time'
317
+ response_schema = responses ['200' ]['content' ]['application/json' ]['schema' ]
318
+ properties = response_schema ['items' ]['properties' ]
319
+ assert properties ['date' ]['type' ] == properties ['datetime' ]['type' ] == 'string'
320
+ assert properties ['date' ]['format' ] == 'date'
321
+ assert properties ['datetime' ]['format' ] == 'date-time'
233
322
234
323
def test_serializer_validators (self ):
235
324
path = '/'
@@ -243,45 +332,46 @@ def test_serializer_validators(self):
243
332
inspector .view = view
244
333
245
334
responses = inspector ._get_responses (path , method )
246
- response_schema = responses ['200' ]['content' ]['application/json' ]['schema' ]['properties' ]
335
+ response_schema = responses ['200' ]['content' ]['application/json' ]['schema' ]
336
+ properties = response_schema ['items' ]['properties' ]
247
337
248
- assert response_schema ['integer' ]['type' ] == 'integer'
249
- assert response_schema ['integer' ]['maximum' ] == 99
250
- assert response_schema ['integer' ]['minimum' ] == - 11
338
+ assert properties ['integer' ]['type' ] == 'integer'
339
+ assert properties ['integer' ]['maximum' ] == 99
340
+ assert properties ['integer' ]['minimum' ] == - 11
251
341
252
- assert response_schema ['string' ]['minLength' ] == 2
253
- assert response_schema ['string' ]['maxLength' ] == 10
342
+ assert properties ['string' ]['minLength' ] == 2
343
+ assert properties ['string' ]['maxLength' ] == 10
254
344
255
- assert response_schema ['regex' ]['pattern' ] == r'[ABC]12{3}'
256
- assert response_schema ['regex' ]['description' ] == 'must have an A, B, or C followed by 1222'
345
+ assert properties ['regex' ]['pattern' ] == r'[ABC]12{3}'
346
+ assert properties ['regex' ]['description' ] == 'must have an A, B, or C followed by 1222'
257
347
258
- assert response_schema ['decimal1' ]['type' ] == 'number'
259
- assert response_schema ['decimal1' ]['multipleOf' ] == .01
260
- assert response_schema ['decimal1' ]['maximum' ] == 10000
261
- assert response_schema ['decimal1' ]['minimum' ] == - 10000
348
+ assert properties ['decimal1' ]['type' ] == 'number'
349
+ assert properties ['decimal1' ]['multipleOf' ] == .01
350
+ assert properties ['decimal1' ]['maximum' ] == 10000
351
+ assert properties ['decimal1' ]['minimum' ] == - 10000
262
352
263
- assert response_schema ['decimal2' ]['type' ] == 'number'
264
- assert response_schema ['decimal2' ]['multipleOf' ] == .0001
353
+ assert properties ['decimal2' ]['type' ] == 'number'
354
+ assert properties ['decimal2' ]['multipleOf' ] == .0001
265
355
266
- assert response_schema ['email' ]['type' ] == 'string'
267
- assert response_schema ['email' ]['format' ] == 'email'
268
- assert response_schema ['email' ]['default' ] == 'foo@bar.com'
356
+ assert properties ['email' ]['type' ] == 'string'
357
+ assert properties ['email' ]['format' ] == 'email'
358
+ assert properties ['email' ]['default' ] == 'foo@bar.com'
269
359
270
- assert response_schema ['url' ]['type' ] == 'string'
271
- assert response_schema ['url' ]['nullable' ] is True
272
- assert response_schema ['url' ]['default' ] == 'http://www.example.com'
360
+ assert properties ['url' ]['type' ] == 'string'
361
+ assert properties ['url' ]['nullable' ] is True
362
+ assert properties ['url' ]['default' ] == 'http://www.example.com'
273
363
274
- assert response_schema ['uuid' ]['type' ] == 'string'
275
- assert response_schema ['uuid' ]['format' ] == 'uuid'
364
+ assert properties ['uuid' ]['type' ] == 'string'
365
+ assert properties ['uuid' ]['format' ] == 'uuid'
276
366
277
- assert response_schema ['ip4' ]['type' ] == 'string'
278
- assert response_schema ['ip4' ]['format' ] == 'ipv4'
367
+ assert properties ['ip4' ]['type' ] == 'string'
368
+ assert properties ['ip4' ]['format' ] == 'ipv4'
279
369
280
- assert response_schema ['ip6' ]['type' ] == 'string'
281
- assert response_schema ['ip6' ]['format' ] == 'ipv6'
370
+ assert properties ['ip6' ]['type' ] == 'string'
371
+ assert properties ['ip6' ]['format' ] == 'ipv6'
282
372
283
- assert response_schema ['ip' ]['type' ] == 'string'
284
- assert 'format' not in response_schema ['ip' ]
373
+ assert properties ['ip' ]['type' ] == 'string'
374
+ assert 'format' not in properties ['ip' ]
285
375
286
376
287
377
@pytest .mark .skipif (uritemplate is None , reason = 'uritemplate not installed.' )
0 commit comments