Skip to content

Commit

Permalink
fix: extend_schema_field with dict param and oas 3.1
Browse files Browse the repository at this point in the history
the dict was being mutated, so every subsequent use of the field would have more stuff appended to the schema
  • Loading branch information
Eric Butler committed Jan 18, 2024
1 parent aeca119 commit 6f3a656
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections
import copy
import functools
import hashlib
import inspect
Expand Down Expand Up @@ -525,6 +526,9 @@ def safe_ref(schema: _SchemaType) -> _SchemaType:

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

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

Expand Down
22 changes: 22 additions & 0 deletions tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ 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

assert_schema(
generate_schema('x', view_function=view_func),
'tests/test_extend_schema_field_with_dict_oas_3_1.yml'
)


def test_layered_extend_schema_on_view_and_method_with_meta(no_warnings):
class XSerializer(serializers.Serializer):
field = serializers.IntegerField()
Expand Down
64 changes: 64 additions & 0 deletions tests/test_extend_schema_field_with_dict_oas_3_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
openapi: 3.1.0
info:
title: ''
version: 0.0.0
paths:
/x:
post:
operationId: x_create
tags:
- x
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/X'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/X'
multipart/form-data:
schema:
$ref: '#/components/schemas/X'
security:
- cookieAuth: []
- basicAuth: []
- {}
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/X'
description: ''
components:
schemas:
X:
type: object
properties:
field1:
type:
- string
- 'null'
readOnly: true
field2:
type:
- string
- 'null'
readOnly: true
field3:
type:
- string
- 'null'
readOnly: true
required:
- field1
- field2
- field3
securitySchemes:
basicAuth:
type: http
scheme: basic
cookieAuth:
type: apiKey
in: cookie
name: sessionid

0 comments on commit 6f3a656

Please sign in to comment.