From 6e6063e486f85dbd94f7cd5537b0a60104f32196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=20V=C3=AEjdea?= Date: Sun, 26 Nov 2017 13:42:41 +0100 Subject: [PATCH 1/2] Make drf-openapi work without DRF API versioning. --- README.rst | 6 +++--- drf_openapi/entities.py | 5 ++++- drf_openapi/templates/drf_openapi/index.html | 12 +++++++++++- drf_openapi/views.py | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index a51f639..b032e03 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,7 @@ This project was born to bridge the gap between DRF and OpenAPI. The high-level - Can be dropped into any existing DRF project without any code change necessary. - Provide clear disctinction between request schema and response schema. -- Provide a versioning mechanism for each schema. Support defining schema by version range syntax, e.g. :code:`>1.0, <=2.0` +- If desired, provide a versioning mechanism for each schema. Support defining schema by version range syntax, e.g. :code:`>1.0, <=2.0` - Support multiple response codes, not just :code:`200` - All this information should be bound to view methods, not view classes. @@ -67,8 +67,8 @@ DRF itself decides to support OpenAPI officially, if at all. 4. Constraints ---------------- -Currently DRF OpenAPI only supports DRF project that has `versioning `_ enabled. -I have only tested `URLPathVersioning `_ but I intend to suppor the full range of +DRF OpenAPI supports DRF projects both with and without `versioning `_ enabled. +That said, I have only tested `URLPathVersioning `_ but I intend to support the full range of versioning scheme supported by DRF. 5. Examples diff --git a/drf_openapi/entities.py b/drf_openapi/entities.py index c173b81..f0cd6c6 100644 --- a/drf_openapi/entities.py +++ b/drf_openapi/entities.py @@ -192,10 +192,13 @@ def get_link(self, path, method, view, version=None): response_schema, error_status_codes = self.get_response_object( response_serializer_class, method_func.__doc__) if response_serializer_class else ({}, {}) + url = path + if self.version: + url = path.replace('{version}', self.version) # can't use format because there may be other param return OpenApiLink( response_schema=response_schema, error_status_codes=error_status_codes, - url=path.replace('{version}', self.version), # can't use format because there may be other param + url=url, action=method.lower(), encoding=encoding, fields=fields, diff --git a/drf_openapi/templates/drf_openapi/index.html b/drf_openapi/templates/drf_openapi/index.html index e8bcba4..43e4b3e 100644 --- a/drf_openapi/templates/drf_openapi/index.html +++ b/drf_openapi/templates/drf_openapi/index.html @@ -9,10 +9,20 @@ margin: 0; padding: 0; } + {% if not request.version %} + span.api-info-version { + display: none; + } + {% endif %} - + diff --git a/drf_openapi/views.py b/drf_openapi/views.py index c9cf8c0..abca3b3 100644 --- a/drf_openapi/views.py +++ b/drf_openapi/views.py @@ -13,7 +13,7 @@ class SchemaView(APIView): url = '' title = 'API Documentation' - def get(self, request, version): + def get(self, request, version=''): generator = OpenApiSchemaGenerator( version=version, url=self.url, From 6c2a8825b2cbf1e70fc1b84f6f262ce4957939db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=20V=C3=AEjdea?= Date: Fri, 15 Dec 2017 11:01:19 +0100 Subject: [PATCH 2/2] Preserve protocol when appending ?format=openapi --- drf_openapi/templates/drf_openapi/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drf_openapi/templates/drf_openapi/index.html b/drf_openapi/templates/drf_openapi/index.html index 43e4b3e..2f5df89 100644 --- a/drf_openapi/templates/drf_openapi/index.html +++ b/drf_openapi/templates/drf_openapi/index.html @@ -18,7 +18,7 @@