Skip to content

Commit c9e37ba

Browse files
Behostonsigvef
authored andcommitted
OpenAPI: Use 201 status code for POST requests. (encode#7206)
1 parent efac4f5 commit c9e37ba

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

rest_framework/schemas/openapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,9 @@ def _get_responses(self, path, method):
558558
response_schema = paginator.get_paginated_response_schema(response_schema)
559559
else:
560560
response_schema = item_schema
561-
561+
status_code = '201' if method == 'POST' else '200'
562562
return {
563-
'200': {
563+
status_code: {
564564
'content': {
565565
ct: {'schema': response_schema}
566566
for ct in self.response_media_types

tests/schemas/test_openapi.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ class View(generics.GenericAPIView):
275275
inspector.view = view
276276

277277
responses = inspector._get_responses(path, method)
278-
assert responses['200']['content']['application/json']['schema']['required'] == ['text']
279-
assert list(responses['200']['content']['application/json']['schema']['properties'].keys()) == ['text']
280-
assert 'description' in responses['200']
278+
assert '201' in responses
279+
assert responses['201']['content']['application/json']['schema']['required'] == ['text']
280+
assert list(responses['201']['content']['application/json']['schema']['properties'].keys()) == ['text']
281+
assert 'description' in responses['201']
281282

282283
def test_response_body_nested_serializer(self):
283284
path = '/'
@@ -302,7 +303,7 @@ class View(generics.GenericAPIView):
302303
inspector.view = view
303304

304305
responses = inspector._get_responses(path, method)
305-
schema = responses['200']['content']['application/json']['schema']
306+
schema = responses['201']['content']['application/json']['schema']
306307
assert sorted(schema['required']) == ['nested', 'text']
307308
assert sorted(list(schema['properties'].keys())) == ['nested', 'text']
308309
assert schema['properties']['nested']['type'] == 'object'

0 commit comments

Comments
 (0)