Skip to content

Commit 14b2b5b

Browse files
committed
bugfix @extend_schema_field raw schema already in OAS3.1
1 parent d05e5be commit 14b2b5b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

drf_spectacular/plumbing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import collections
2-
import copy
32
import functools
43
import hashlib
54
import inspect
@@ -534,7 +533,10 @@ def append_meta(schema: _SchemaType, meta: _SchemaType) -> _SchemaType:
534533

535534
if schema_nullable or meta_nullable:
536535
if 'type' in schema:
537-
schema['type'] = [schema['type'], 'null']
536+
if isinstance(schema['type'], str):
537+
schema['type'] = [schema['type'], 'null']
538+
else:
539+
schema['type'] = [*schema['type'], 'null']
538540
elif '$ref' in schema:
539541
schema = {'oneOf': [schema, {'type': 'null'}]}
540542
elif len(schema) == 1 and 'oneOf' in schema:

tests/test_extend_schema.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,29 @@ def view_func(request, format=None):
257257
}
258258

259259

260+
@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
261+
def test_extend_schema_field_with_schema_as_oas_3_1(no_warnings):
262+
@extend_schema_field({'type': ['string', 'integer']})
263+
class CustomField(serializers.CharField):
264+
pass
265+
266+
class XSerializer(serializers.Serializer):
267+
field1 = CustomField(read_only=True, allow_null=True)
268+
field2 = CustomField(read_only=True, allow_null=True)
269+
270+
@extend_schema(request=XSerializer, responses=XSerializer)
271+
@api_view(['POST'])
272+
def view_func(request, format=None):
273+
pass # pragma: no cover
274+
275+
schema = generate_schema('x', view_function=view_func)
276+
277+
assert schema['components']['schemas']['X']['properties'] == {
278+
'field1': {'readOnly': True, 'type': ['string', 'integer', 'null']},
279+
'field2': {'readOnly': True, 'type': ['string', 'integer', 'null']},
280+
}
281+
282+
260283
def test_layered_extend_schema_on_view_and_method_with_meta(no_warnings):
261284
class XSerializer(serializers.Serializer):
262285
field = serializers.IntegerField()

0 commit comments

Comments
 (0)