From 77fdd9b004f606551fe40c8b1b313cc47e723fe0 Mon Sep 17 00:00:00 2001 From: Antti Kaihola Date: Wed, 27 Mar 2019 15:15:11 +0200 Subject: [PATCH 1/5] Fixed API documentation when using selectable output types --- CHANGELOG.md | 3 +++ hug/api.py | 2 +- tests/test_documentation.py | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa62c25a..620c4337 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Ideally, within a virtual environment. Changelog ========= +### 2.4.7 - TBD +- Fixed API documentation with selectable output types + ### 2.4.6 - March 25, 2019 - Fixed issue #753 - 404 not found does not respect default output format. - Documented the `--without-cython` option in `CONTRIBUTING.md` diff --git a/hug/api.py b/hug/api.py index 2a006a1f..6e6be414 100644 --- a/hug/api.py +++ b/hug/api.py @@ -310,7 +310,7 @@ def handle_404(request, response, *args, **kwargs): response.data = hug.output_format.json(to_return, indent=4, separators=(',', ': ')) response.content_type = 'application/json; charset=utf-8' else: - response.data = self.output_format(to_return) + response.data = self.output_format(to_return, request, response) response.content_type = self.output_format.content_type response.status = falcon.HTTP_NOT_FOUND diff --git a/tests/test_documentation.py b/tests/test_documentation.py index cf81c07b..03fd6127 100644 --- a/tests/test_documentation.py +++ b/tests/test_documentation.py @@ -20,6 +20,7 @@ """ import json +from unittest import mock import marshmallow from falcon import Request @@ -149,6 +150,22 @@ def extend_with(): assert '/echo' in documentation['handlers'] assert '/test' not in documentation['handlers'] + +def test_basic_documentation_output_type_accept(): + """Ensure API documentation works with selectable output types""" + accept_output = hug.output_format.accept( + {'application/json': hug.output_format.json, + 'application/pretty-json': hug.output_format.pretty_json}, + default=hug.output_format.json) + with mock.patch.object(api.http, '_output_format', accept_output, create=True): + handler = api.http.documentation_404() + response = StartResponseMock() + + handler(Request(create_environ(path='v1/doc')), response) + + documentation = json.loads(response.data.decode('utf8'))['documentation'] + assert set(documentation) == {'handlers', 'overview'} + def test_marshmallow_return_type_documentation(): From 14f5f5504e8f7455fec0f31a4782eee081b2a73b Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Wed, 27 Mar 2019 23:21:37 -0700 Subject: [PATCH 2/5] Update api.py --- hug/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hug/api.py b/hug/api.py index 6e6be414..90f90321 100644 --- a/hug/api.py +++ b/hug/api.py @@ -310,7 +310,7 @@ def handle_404(request, response, *args, **kwargs): response.data = hug.output_format.json(to_return, indent=4, separators=(',', ': ')) response.content_type = 'application/json; charset=utf-8' else: - response.data = self.output_format(to_return, request, response) + response.data = self.output_format(to_return, request=request, response=response) response.content_type = self.output_format.content_type response.status = falcon.HTTP_NOT_FOUND From f456822851a267bce6ee07f041fba2239d36772f Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Wed, 27 Mar 2019 23:23:02 -0700 Subject: [PATCH 3/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 620c4337..7504cc70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ Ideally, within a virtual environment. Changelog ========= -### 2.4.7 - TBD +### 2.4.7 - March 27, 2019 - Fixed API documentation with selectable output types ### 2.4.6 - March 25, 2019 From a263d3417e441aaf8424793e686c80dd2f1043f4 Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Wed, 27 Mar 2019 23:32:11 -0700 Subject: [PATCH 4/5] Update test_documentation.py --- tests/test_documentation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_documentation.py b/tests/test_documentation.py index 03fd6127..ed36e433 100644 --- a/tests/test_documentation.py +++ b/tests/test_documentation.py @@ -164,7 +164,7 @@ def test_basic_documentation_output_type_accept(): handler(Request(create_environ(path='v1/doc')), response) documentation = json.loads(response.data.decode('utf8'))['documentation'] - assert set(documentation) == {'handlers', 'overview'} + assert 'handlers' in documentation and 'overview' in documentation def test_marshmallow_return_type_documentation(): From fb468b01976fc7bc124b566f19b1952cae8a4aae Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Thu, 28 Mar 2019 00:08:57 -0700 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7504cc70..8715080a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ Ideally, within a virtual environment. Changelog ========= -### 2.4.7 - March 27, 2019 +### 2.4.7 - March 28, 2019 - Fixed API documentation with selectable output types ### 2.4.6 - March 25, 2019