From 94a09149b62496b5434a690de84b5972a5d5b554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Legi=C4=99cki?= Date: Mon, 2 Mar 2020 16:32:26 +0100 Subject: [PATCH] OpenAPI: Use 201 status code for POST requests. (#7206) --- rest_framework/schemas/openapi.py | 4 ++-- tests/schemas/test_openapi.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 5277f17a6..cb0407b62 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -558,9 +558,9 @@ class AutoSchema(ViewInspector): 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 diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 7f73c8c30..6f5f42dac 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -275,9 +275,10 @@ class TestOperationIntrospection(TestCase): 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 = '/' @@ -302,7 +303,7 @@ class TestOperationIntrospection(TestCase): 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'