Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions doc/swagger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion flask_restx/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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

Expand Down
54 changes: 54 additions & 0 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down