Skip to content

Commit

Permalink
bugfix @extend_schema_field raw schema already in OAS3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Oct 3, 2024
1 parent d05e5be commit e6bcf3f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ def append_meta(schema: _SchemaType, meta: _SchemaType) -> _SchemaType:

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
23 changes: 23 additions & 0 deletions tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ def view_func(request, format=None):
}


@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 e6bcf3f

Please sign in to comment.