diff --git a/CHANGELOG.md b/CHANGELOG.md index fa62c25a..8715080a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Ideally, within a virtual environment. Changelog ========= +### 2.4.7 - March 28, 2019 +- 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..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) + 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 diff --git a/tests/test_documentation.py b/tests/test_documentation.py index cf81c07b..ed36e433 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 'handlers' in documentation and 'overview' in documentation + def test_marshmallow_return_type_documentation():