-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for djangorestframework-dataclasses
- Loading branch information
Showing
7 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from drf_spectacular.extensions import OpenApiSerializerExtension | ||
from drf_spectacular.plumbing import get_doc | ||
from drf_spectacular.utils import Direction | ||
|
||
|
||
class OpenApiDataclassSerializerExtensions(OpenApiSerializerExtension): | ||
target_class = "rest_framework_dataclasses.serializers.DataclassSerializer" | ||
match_subclasses = True | ||
|
||
def get_name(self): | ||
"""Use the dataclass name in the schema, instead of the serializer prefix (which can be just Dataclass).""" | ||
return self.target.dataclass_definition.dataclass_type.__name__ | ||
|
||
def strip_library_doc(self, schema): | ||
"""Strip the DataclassSerializer library documentation from the schema.""" | ||
from rest_framework_dataclasses.serializers import DataclassSerializer | ||
if 'description' in schema and schema['description'] == get_doc(DataclassSerializer): | ||
del schema['description'] | ||
return schema | ||
|
||
def map_serializer(self, auto_schema, direction: Direction): | ||
""""Generate the schema for a DataclassSerializer.""" | ||
schema = auto_schema._map_serializer(self.target, direction, bypass_extensions=True) | ||
return self.strip_library_doc(schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
import typing | ||
|
||
import pytest | ||
from django.urls import path | ||
from rest_framework.decorators import api_view | ||
|
||
from drf_spectacular.utils import extend_schema | ||
from tests import assert_schema, generate_schema | ||
|
||
|
||
@pytest.mark.contrib('rest_framework_dataclasses') | ||
@pytest.mark.skipif(sys.version_info < (3, 7), reason='dataclass required by package') | ||
def test_rest_framework_dataclasses(no_warnings): | ||
from dataclasses import dataclass | ||
|
||
from rest_framework_dataclasses.serializers import DataclassSerializer | ||
|
||
@dataclass | ||
class Person: | ||
name: str | ||
length: int | ||
|
||
@dataclass | ||
class Group: | ||
name: str | ||
leader: Person | ||
members: typing.List[Person] | ||
|
||
class GroupSerializer(DataclassSerializer): | ||
class Meta: | ||
dataclass = Group | ||
|
||
@extend_schema(responses=GroupSerializer) | ||
@api_view(['GET']) | ||
def named(request): | ||
pass # pragma: no cover | ||
|
||
@extend_schema(responses=DataclassSerializer(dataclass=Person)) | ||
@api_view(['GET']) | ||
def anonymous(request): | ||
pass # pragma: no cover | ||
|
||
urlpatterns = [ | ||
path('named', named), | ||
path('anonymous', anonymous), | ||
] | ||
assert_schema( | ||
generate_schema(None, patterns=urlpatterns), | ||
'tests/contrib/test_rest_framework_dataclasses.yml' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: '' | ||
version: 0.0.0 | ||
paths: | ||
/anonymous: | ||
get: | ||
operationId: anonymous_retrieve | ||
tags: | ||
- anonymous | ||
security: | ||
- cookieAuth: [] | ||
- basicAuth: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Person' | ||
description: '' | ||
/named: | ||
get: | ||
operationId: named_retrieve | ||
tags: | ||
- named | ||
security: | ||
- cookieAuth: [] | ||
- basicAuth: [] | ||
- {} | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Group' | ||
description: '' | ||
components: | ||
schemas: | ||
Group: | ||
type: object | ||
properties: | ||
name: | ||
type: string | ||
leader: | ||
$ref: '#/components/schemas/Person' | ||
members: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/Person' | ||
required: | ||
- leader | ||
- members | ||
- name | ||
Person: | ||
type: object | ||
properties: | ||
name: | ||
type: string | ||
length: | ||
type: integer | ||
required: | ||
- length | ||
- name | ||
securitySchemes: | ||
basicAuth: | ||
type: http | ||
scheme: basic | ||
cookieAuth: | ||
type: apiKey | ||
in: cookie | ||
name: sessionid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters