glowka / drf-yasg-json-api Goto Github PK
View Code? Open in Web Editor NEWAutomated generation of Swagger/OpenAPI 2.0 specifications from Django Rest Framework JSON API endpoints.
License: Other
Automated generation of Swagger/OpenAPI 2.0 specifications from Django Rest Framework JSON API endpoints.
License: Other
drf-yasg-json-api generates incorrect schema for non-model views (rest_framework.viewsets.ViewSet). Here is a test that shows it:
diff --git a/tests/test_schema/test_get.py b/tests/test_schema/test_get.py
index c850bdf..1438584 100644
--- a/tests/test_schema/test_get.py
+++ b/tests/test_schema/test_get.py
@@ -7,10 +7,12 @@ import drf_yasg.inspectors
from django.conf.urls import url
from drf_yasg import openapi
from drf_yasg.generators import OpenAPISchemaGenerator
+from drf_yasg.utils import swagger_auto_schema
from rest_framework import mixins
from rest_framework import routers
from rest_framework import views
from rest_framework import viewsets
+from rest_framework.response import Response
from rest_framework_json_api import django_filters
from rest_framework_json_api import filters
from rest_framework_json_api import pagination
@@ -242,3 +244,31 @@ def test_get__filter():
request_parameters_schema = swagger['paths']['/projects/']['get']['parameters']
assert request_parameters_schema[0]['name'] == 'filter[archived]'
+
+
+def test_get_nonmodel_many():
+ class ProjectSerializer(serializers.Serializer):
+ pk = serializers.IntegerField()
+ name = serializers.CharField()
+
+ class ProjectViewSet(viewsets.ViewSet):
+ renderer_classes = [renderers.JSONRenderer]
+ parser_classes = [parsers.JSONParser]
+ swagger_schema = base.BasicSwaggerAutoSchema
+ resource_name = 'projects'
+
+ @swagger_auto_schema(responses={200: ProjectSerializer(many=True)})
+ def list(self, request):
+ projects = [{'pk': 1, 'name': 'test'}]
+ serializer = ProjectSerializer(data=projects, many=True)
+ return Response(serializer.data)
+
+ router = routers.DefaultRouter()
+ router.register(r'projects', ProjectViewSet, **compatibility._basename_or_base_name('projects'))
+
+ generator = OpenAPISchemaGenerator(info=openapi.Info(title="", default_version=""), patterns=router.urls)
+
+ swagger = generator.get_schema(None, True)
+
+ response_schema = swagger['paths']['/projects/']['get']['responses']['200']['schema']
+ assert response_schema.type != 'array'
It works fine for single item view, but for list view it generates non-json-api schema:
[
{
"id": "integer",
"name": "string"
}
]
In DJA they support string paths like:
included_serializers = {
'categories': 'api.serializers.task.TaskCategorySerializer',
'tasks': 'api.serializers.task.TaskSerializer'
}
I have some relationships that I need both sides to be includable and my serializers are usually in the same file.
Eample:
class TaskListSerializer(JSONAPISerializers.ModelSerializer):
included_serializers = {
'tasks': 'api.serializers.task.TaskSerializer'
}
class Meta:
model = TaskList
fields = ['id', 'name', 'description', 'tasks']
class TaskSerializer(JSONAPISerializers.ModelSerializer):
included_serializers = {
'lists': TaskListSerializer
}
class Meta:
model = Task
fields = ['id', 'name', 'description', 'lists']
Every time I try to use the path string it works in DJA but I get the following error when using this plugin:
TypeError: 'str' object is not callable
Is there a possibility of getting path support for included_serializers (ex: 'api.serializers.task.TaskSerializer')?
I am getting an assertion error while trying to generate schema for a ViewSet which contains a djangorestframework-recursive
- RecursiveField
assert use_references is True, "Can not create schema for RecursiveField when use_references is False"
AssertionError: Can not create schema for RecursiveField when use_references is False
serializers.py
class ResourceSerializer(serializers.ModelSerializer):
children = serializers.ListField(read_only=True, source='get_children', child=RecursiveField())
class Meta:
model = Resource
fields = ('id', 'children', 'slug')
Could you please help me out?
Hello, I am receiving this error after running the server although there are no custom swagger_auto_schema declared in the API.
raise AssertionError("TYPE_ARRAY requires the items attribute")
AssertionError: TYPE_ARRAY requires the items attribute
Any help on this is appreciated.
Hello,
I am trying to update a project in new versions of Django, DRF etc, and I am having some issues with drf-yasg-json-api
compatibility with new version of django-rest-framework-json-api
.
I will try to fix this in a fork. Can I submit a PR for this?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.