Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extend_schema_field with dict param and oas 3.1 #1147

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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