def test_empty_required(self):
        path = '/'
        method = 'POST'

        class Serializer(serializers.Serializer):
            read_only = serializers.CharField(read_only=True)
            write_only = serializers.CharField(write_only=True, required=False)

        class View(generics.GenericAPIView):
            serializer_class = Serializer

        view = create_view(
            View,
            method,
            create_request(path)
        )
        inspector = AutoSchema()
        inspector.view = view

        request_body = inspector._get_request_body(path, method)
        # there should be no empty 'required' property, see #6834
        assert 'required' not in request_body['content']['application/json']['schema']

        for response in inspector._get_responses(path, method).values():
            assert 'required' not in response['content']['application/json']['schema']
Exemple #2
0
    def test_request_body(self):
        path = '/'
        method = 'POST'

        class Serializer(serializers.Serializer):
            text = serializers.CharField()
            read_only = serializers.CharField(read_only=True)

        class View(generics.GenericAPIView):
            serializer_class = Serializer

        view = create_view(View, method, create_request(path))
        inspector = AutoSchema()
        inspector.view = view

        request_body = inspector._get_request_body(path, method)
        assert request_body['content']['application/json']['schema'][
            'required'] == ['text']
        assert list(request_body['content']['application/json']['schema']
                    ['properties'].keys()) == ['text']
    def test_serializer_filefield(self):
        path = '/{id}/'
        method = 'POST'

        class ItemSerializer(serializers.Serializer):
            attachment = serializers.FileField()

        class View(generics.CreateAPIView):
            serializer_class = ItemSerializer

        view = create_view(
            View,
            method,
            create_request(path),
        )
        inspector = AutoSchema()
        inspector.view = view

        request_body = inspector._get_request_body(path, method)
        mp_media = request_body['content']['multipart/form-data']
        attachment = mp_media['schema']['properties']['attachment']
        assert attachment['format'] == 'binary'
    def test_parser_mapping(self):
        """Test that view's parsers are mapped to OA media types"""
        path = '/{id}/'
        method = 'POST'

        class View(generics.CreateAPIView):
            serializer_class = views.ExampleSerializer
            parser_classes = [JSONParser, MultiPartParser]

        view = create_view(
            View,
            method,
            create_request(path),
        )
        inspector = AutoSchema()
        inspector.view = view

        request_body = inspector._get_request_body(path, method)

        assert len(request_body['content'].keys()) == 2
        assert 'multipart/form-data' in request_body['content']
        assert 'application/json' in request_body['content']
    def test_request_body(self):
        path = '/'
        method = 'POST'

        class Serializer(serializers.Serializer):
            text = serializers.CharField()
            read_only = serializers.CharField(read_only=True)

        class View(generics.GenericAPIView):
            serializer_class = Serializer

        view = create_view(
            View,
            method,
            create_request(path)
        )
        inspector = AutoSchema()
        inspector.view = view

        request_body = inspector._get_request_body(path, method)
        assert request_body['content']['application/json']['schema']['required'] == ['text']
        assert list(request_body['content']['application/json']['schema']['properties'].keys()) == ['text']
    def test_request_body(self):
        path = '/'
        method = 'POST'

        class ItemSerializer(serializers.Serializer):
            text = serializers.CharField()
            read_only = serializers.CharField(read_only=True)

        class View(generics.GenericAPIView):
            serializer_class = ItemSerializer

        view = create_view(View, method, create_request(path))
        inspector = AutoSchema()
        inspector.view = view

        request_body = inspector._get_request_body(path, method)
        print(request_body)
        assert request_body['content']['application/json']['schema'][
            '$ref'] == '#/components/schemas/Item'

        components = inspector.get_components(path, method)
        assert components['Item']['required'] == ['text']
        assert sorted(list(
            components['Item']['properties'].keys())) == ['read_only', 'text']