From 1494fcf77eaeac91c231ff2aba43a029e2899f1f Mon Sep 17 00:00:00 2001 From: trik Date: Tue, 6 Feb 2018 10:11:30 +0100 Subject: [PATCH] improve versioning schemes support close #98 close #71 --- drf_openapi/entities.py | 2 +- drf_openapi/utils.py | 6 ++++-- drf_openapi/views.py | 3 ++- examples/snippets/views.py | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/drf_openapi/entities.py b/drf_openapi/entities.py index 70bba2d..3834721 100644 --- a/drf_openapi/entities.py +++ b/drf_openapi/entities.py @@ -134,7 +134,7 @@ def get_links(self, request=None): for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue - link = self.get_link(path, method, view, version=getattr(request, 'version', None)) + link = self.get_link(path, method, view, version=self.version) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) try: diff --git a/drf_openapi/utils.py b/drf_openapi/utils.py index 786f539..87f1fd8 100644 --- a/drf_openapi/utils.py +++ b/drf_openapi/utils.py @@ -13,7 +13,9 @@ def decorator(view_method): view_method.response_serializer = response_serializer @wraps(view_method) - def wrapper(instance, request, version=None, *args, **kwargs): + def wrapper(instance, request, *args, **kwargs): + version, _ = instance.determine_version(request, *args, **kwargs) + if request_serializer and issubclass(request_serializer, VersionedSerializers): instance.request_serializer = request_serializer.get(version) else: @@ -24,7 +26,7 @@ def wrapper(instance, request, version=None, *args, **kwargs): else: instance.response_serializer = response_serializer - response = view_method(instance, request, version=version, *args, **kwargs) + response = view_method(instance, request, *args, **kwargs) if validate_response: response_validator = instance.response_serializer(data=response.data) response_validator.is_valid(raise_exception=True) diff --git a/drf_openapi/views.py b/drf_openapi/views.py index c9cf8c0..6e29986 100644 --- a/drf_openapi/views.py +++ b/drf_openapi/views.py @@ -13,7 +13,8 @@ class SchemaView(APIView): url = '' title = 'API Documentation' - def get(self, request, version): + def get(self, request, *args, **kwargs): + version, _ = self.determine_version(request, *args, **kwargs) generator = OpenApiSchemaGenerator( version=version, url=self.url, diff --git a/examples/snippets/views.py b/examples/snippets/views.py index 4854bc9..d2a0977 100644 --- a/examples/snippets/views.py +++ b/examples/snippets/views.py @@ -28,7 +28,7 @@ class SnippetList(APIView): """ @view_config(response_serializer=SnippetSerializer) - def get(self, request, version, format=None): + def get(self, request, version=None, format=None): res = self.response_serializer(data=_FAKE_SNIPPETS, many=True) res.is_valid(raise_exception=True) return Response(res.validated_data)