Skip to content

Commit

Permalink
Merge pull request #1305 from tfranzel/pr1147
Browse files Browse the repository at this point in the history
Pr1147
  • Loading branch information
tfranzel authored Oct 3, 2024
2 parents 09c5138 + 508a246 commit 0dea78c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,18 @@ def safe_ref(schema: _SchemaType) -> _SchemaType:

def append_meta(schema: _SchemaType, meta: _SchemaType) -> _SchemaType:
if spectacular_settings.OAS_VERSION.startswith('3.1'):
schema = schema.copy()
meta = meta.copy()

schema_nullable = meta.pop('nullable', None)
meta_nullable = schema.pop('nullable', None)

if schema_nullable or meta_nullable:
if 'type' in schema:
schema['type'] = [schema['type'], 'null']
if isinstance(schema['type'], str):
schema['type'] = [schema['type'], 'null']
else:
schema['type'] = [*schema['type'], 'null']
elif '$ref' in schema:
schema = {'oneOf': [schema, {'type': 'null'}]}
elif len(schema) == 1 and 'oneOf' in schema:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,54 @@ def test_extend_schema(no_warnings):
)


@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_extend_schema_field_with_dict_oas_3_1(no_warnings):
@extend_schema_field({"type": "string"})
class CustomField(serializers.CharField):
pass

class XSerializer(serializers.Serializer):
field1 = CustomField(read_only=True, allow_null=True)
field2 = CustomField(read_only=True, allow_null=True)
field3 = CustomField(read_only=True, allow_null=True)

@extend_schema(request=XSerializer, responses=XSerializer)
@api_view(['POST'])
def view_func(request, format=None):
pass # pragma: no cover

schema = generate_schema('x', view_function=view_func)

assert schema['components']['schemas']['X']['properties'] == {
'field1': {'readOnly': True, 'type': ['string', 'null']},
'field2': {'readOnly': True, 'type': ['string', 'null']},
'field3': {'readOnly': True, 'type': ['string', 'null']}
}


@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_extend_schema_field_with_schema_as_oas_3_1(no_warnings):
@extend_schema_field({'type': ['string', 'integer']})
class CustomField(serializers.CharField):
pass

class XSerializer(serializers.Serializer):
field1 = CustomField(read_only=True, allow_null=True)
field2 = CustomField(read_only=True, allow_null=True)

@extend_schema(request=XSerializer, responses=XSerializer)
@api_view(['POST'])
def view_func(request, format=None):
pass # pragma: no cover

schema = generate_schema('x', view_function=view_func)

assert schema['components']['schemas']['X']['properties'] == {
'field1': {'readOnly': True, 'type': ['string', 'integer', 'null']},
'field2': {'readOnly': True, 'type': ['string', 'integer', 'null']},
}


def test_layered_extend_schema_on_view_and_method_with_meta(no_warnings):
class XSerializer(serializers.Serializer):
field = serializers.IntegerField()
Expand Down

0 comments on commit 0dea78c

Please sign in to comment.