-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
when getting shcema, happy an exception #7069
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
Comments
i think , when if public ,should give public but not reques =None.
maby change:
|
I tried the following test on the master branch (commit: https://github.com/encode/django-rest-framework/tree/609f708a27bd38496b912c44742287c57e7af912): diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py
index 35d676d6..8dafb47e 100644
--- a/tests/schemas/test_openapi.py
+++ b/tests/schemas/test_openapi.py
@@ -1070,3 +1070,15 @@ class TestGenerator(TestCase):
assert 'components' in schema
assert 'schemas' in schema['components']
assert 'Duplicate' in schema['components']['schemas']
+
+ def test_conditional_serializer(self):
+ router = routers.SimpleRouter()
+ router.register('account', views.ConditionalViewSet, basename="account")
+ urlpatterns = router.urls
+
+ generator = SchemaGenerator(patterns=urlpatterns)
+
+ request = create_request('/')
+ schema = generator.get_schema(request=request)
+
+ print(schema)
diff --git a/tests/schemas/views.py b/tests/schemas/views.py
index 1c8235b4..d3eeb6a6 100644
--- a/tests/schemas/views.py
+++ b/tests/schemas/views.py
@@ -11,7 +11,7 @@ from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.schemas.openapi import AutoSchema
from rest_framework.views import APIView
-from rest_framework.viewsets import GenericViewSet
+from rest_framework.viewsets import ModelViewSet, GenericViewSet
class ExampleListView(APIView):
@@ -215,3 +215,11 @@ class ExampleAutoSchemaDuplicate2(generics.GenericAPIView):
serializer = self.get_serializer(data=now.date(), datetime=now)
return Response(serializer.data)
+
+
+class ConditionalViewSet(ModelViewSet):
+ def get_serializer_class(self):
+ if self.request.method == 'PATCH':
+ return ExampleSerializer
+ else:
+ return ExampleSerializerModel I just had to change the I had the following Exception if I don't always return a serializer (which makes sense).
Can give another try with the master branch and create a PR with a failing test (in case it does not work)? |
That's exactly right. Your get_serializer() needs to work without a request. Maybe there's a docs PR for this but... |
Checklist
version:
django 2.2
django rest framework 3.10.3
Steps to reproduce
Example code is follow:
user Autoschema
Expected behavior
get shcema
Actual behavior
File "H:\trian\whlrest\traingroup\api.py", line 34, in get_serializer_class
if self.request.method == 'PATCH':
AttributeError: 'NoneType' object has no attribute 'method'
when i use drf_yasg, drf_yasg can return correct schema.
The text was updated successfully, but these errors were encountered: