Skip to content

Fix: use 201 status code for POST requests. #7206

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

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@ def _get_responses(self, path, method):
response_schema = paginator.get_paginated_response_schema(response_schema)
else:
response_schema = item_schema

status_code = '201' if method == 'POST' else '200'
return {
'200': {
status_code: {
'content': {
ct: {'schema': response_schema}
for ct in self.response_media_types
Expand Down
9 changes: 5 additions & 4 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ class View(generics.GenericAPIView):
inspector.view = view

responses = inspector._get_responses(path, method)
assert responses['200']['content']['application/json']['schema']['required'] == ['text']
assert list(responses['200']['content']['application/json']['schema']['properties'].keys()) == ['text']
assert 'description' in responses['200']
assert '201' in responses
assert responses['201']['content']['application/json']['schema']['required'] == ['text']
assert list(responses['201']['content']['application/json']['schema']['properties'].keys()) == ['text']
assert 'description' in responses['201']

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

responses = inspector._get_responses(path, method)
schema = responses['200']['content']['application/json']['schema']
schema = responses['201']['content']['application/json']['schema']
assert sorted(schema['required']) == ['nested', 'text']
assert sorted(list(schema['properties'].keys())) == ['nested', 'text']
assert schema['properties']['nested']['type'] == 'object'
Expand Down