From 71ed538a9e6b6696df869a6ae6e5a0ff83996569 Mon Sep 17 00:00:00 2001 From: Lucidiot Date: Fri, 5 Apr 2019 15:40:58 +0200 Subject: [PATCH] Add a nested serializer test case --- tests/schemas/test_openapi.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 6d98d919e..ef233d52c 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -130,6 +130,36 @@ class TestOperationIntrospection(TestCase): assert responses['200']['content']['application/json']['schema']['required'] == ['text'] assert list(responses['200']['content']['application/json']['schema']['properties'].keys()) == ['text'] + def test_response_body_nested_serializer(self): + path = '/' + method = 'POST' + + class NestedSerializer(serializers.Serializer): + number = serializers.IntegerField() + + class Serializer(serializers.Serializer): + text = serializers.CharField() + nested = NestedSerializer() + + class View(generics.GenericAPIView): + serializer_class = Serializer + + view = create_view( + View, + method, + create_request(path), + ) + inspector = AutoSchema() + inspector.view = view + + responses = inspector._get_responses(path, method) + schema = responses['200']['content']['application/json']['schema'] + assert schema['required'] == ['text', 'nested'] + assert list(schema['properties'].keys()) == ['text', 'nested'] + assert schema['properties']['nested']['type'] == 'object' + assert list(schema['properties']['nested']['properties'].keys()) == ['number'] + assert schema['properties']['nested']['required'] == ['number'] + @pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.') @override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.openapi.AutoSchema'})