diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ea0152da..c112695e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -46,6 +46,7 @@ Bug Fixes :: * Adjust field tests for Python 3.14 (``staticmethod`` around ``functools.partial`` used as a class attribute). [python-restx] + * Support response-header documentation on marshalling decorators (#601). [2836048681] .. _section-1.3.1: 1.3.1 diff --git a/doc/swagger.rst b/doc/swagger.rst index 62a3854c..01979e88 100644 --- a/doc/swagger.rst +++ b/doc/swagger.rst @@ -618,6 +618,10 @@ just use the `@api.response` `headers` parameter. pass +Response-specific headers can also be documented with the ``headers`` parameter of +``@api.marshal_with()`` and ``@api.marshal_list_with()``. This only documents the +headers; return a response tuple to set their actual HTTP values. + Documenting expected/request headers is done through the `@api.expect` decorator .. code-block:: python diff --git a/flask_restx/namespace.py b/flask_restx/namespace.py index 6d55fabe..117a5f5e 100644 --- a/flask_restx/namespace.py +++ b/flask_restx/namespace.py @@ -247,8 +247,11 @@ def marshal_with( :param bool as_list: Indicate that the return type is a list (for the documentation) :param int code: Optionally give the expected HTTP response code if its different from 200 + :param dict headers: Response headers to document """ + marshal_kwargs = kwargs.copy() + marshal_kwargs.pop("headers", None) def wrapper(func): doc = { @@ -264,7 +267,7 @@ def wrapper(func): ), # Mask values can't be determined outside app context } func.__apidoc__ = merge(getattr(func, "__apidoc__", {}), doc) - return marshal_with(fields, ordered=self.ordered, **kwargs)(func) + return marshal_with(fields, ordered=self.ordered, **marshal_kwargs)(func) return wrapper diff --git a/tests/test_swagger.py b/tests/test_swagger.py index c4a5ca7b..f1c1ae43 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -1342,6 +1342,60 @@ def get(self): "description": "An header", } + @pytest.mark.parametrize("decorator_name", ["marshal_with", "marshal_list_with"]) + def test_marshal_decorator_with_headers(self, api, client, decorator_name): + person = api.model("Person", {"name": restx.fields.String}) + data = {"name": "Jane Doe", "extra": "not marshalled"} + expected = {"name": "Jane Doe"} + schema = {"$ref": "#/definitions/Person"} + if decorator_name == "marshal_list_with": + data = [data] + expected = [expected] + schema = {"type": "array", "items": schema} + + decorator = getattr(api, decorator_name)( + person, + code=201, + description="Created", + envelope="data", + headers={ + "X-Header": "A response header", + "X-Count": {"type": int, "description": "A count"}, + }, + ) + + @api.route("/test/") + class TestResource(restx.Resource): + @api.response(400, "Validation") + @decorator + def get(self): + return data, 201, {"X-Header": "actual value"} + + @decorator + def post(self): + return data, 201, {"X-Header": "actual value"} + + specs = client.get_specs() + for method in ("get", "post"): + responses = specs["paths"]["/test/"][method]["responses"] + assert responses["201"] == { + "description": "Created", + "schema": {"properties": {"data": schema}}, + "headers": { + "X-Header": {"type": "string", "description": "A response header"}, + "X-Count": {"type": "integer", "description": "A count"}, + }, + } + response = getattr(client, method)("/test/") + assert response.status_code == 201 + assert response.get_json() == {"data": expected} + assert response.headers["X-Header"] == "actual value" + assert "X-Count" not in response.headers + + assert specs["paths"]["/test/"]["get"]["responses"]["400"] == { + "description": "Validation" + } + def test_api_and_response_header(self, api, client): @api.route("/test/") @api.header("X-HEADER", "A class header")