File: test_oas31.py

package info (click to toggle)
python-drf-spectacular 0.28.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,748 kB
  • sloc: python: 14,174; javascript: 114; sh: 61; makefile: 30
file content (91 lines) | stat: -rw-r--r-- 2,825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from unittest import mock

from rest_framework import serializers, viewsets
from rest_framework.views import APIView

from drf_spectacular.utils import extend_schema
from tests import generate_schema
from tests.models import SimpleModel


@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_nullable_sub_serializer(no_warnings):
    class XSerializer(serializers.Serializer):
        f = serializers.FloatField(allow_null=True)

    class YSerializer(serializers.Serializer):
        x = XSerializer(allow_null=True)

    class XAPIView(APIView):
        @extend_schema(responses=YSerializer)
        def get(self, request):
            pass  # pragma: no cover

    schema = generate_schema('x', view=XAPIView)

    assert schema['components']['schemas'] == {
        'X': {
            'properties': {'f': {'format': 'double', 'type': ['number', 'null']}},
            'required': ['f'],
            'type': 'object'
        },
        'Y': {
            'properties': {'x': {'oneOf': [{'$ref': '#/components/schemas/X'}, {'type': 'null'}]}},
            'required': ['x'],
            'type': 'object'
        }
    }


@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_nullable_enum_resolution(no_warnings):
    class XSerializer(serializers.Serializer):
        foo = serializers.ChoiceField(
            choices=[('A', 'A'), ('B', 'B')],
            allow_null=True
        )

    class XAPIView(APIView):
        @extend_schema(responses=XSerializer)
        def get(self, request):
            pass  # pragma: no cover

    schema = generate_schema('x', view=XAPIView)

    assert schema['components']['schemas']['FooEnum'] == {
        'description': '* `A` - A\n* `B` - B',
        'enum': ['A', 'B'],
        'type': 'string',
    }
    assert schema['components']['schemas']['X'] == {
        'properties': {
            'foo': {
                'oneOf': [
                    {'$ref': '#/components/schemas/FooEnum'},
                    {'$ref': '#/components/schemas/NullEnum'}
                ]
            }
        },
        'required': ['foo'],
        'type': 'object'
    }


@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_validator_addition_for_oas31(no_warnings):

    class XSerializer(serializers.Serializer):
        field = serializers.CharField(allow_blank=True, allow_null=True, max_length=40, required=False)

    class XViewset(viewsets.ModelViewSet):
        serializer_class = XSerializer
        queryset = SimpleModel.objects.none()

    schema = generate_schema('x', XViewset)

    assert schema['components']['schemas']['X'] == {
        'properties': {
            'field': {'maxLength': 40, 'type': ['string', 'null']}
        },
        'type': 'object'
    }