Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using both produces and response #207

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion docs/sanic_openapi/decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,4 @@ async def test(request):
And the responses will be:
![](../_static/images/decorators/response.png)

Please note that when you use `response()` decorator, the default response(with status 200) will not be document by default.
Please note that when you use `response()` and `produces()` decorators together, the `response()` decorator with status 200 will have no effect.
14 changes: 8 additions & 6 deletions sanic_openapi/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,20 @@ def build_spec(app, loop):

responses = {}

if len(route_spec.response) == 0:
responses["200"] = {
"schema": serialize_schema(route_spec.produces.field) if route_spec.produces else None,
"description": route_spec.produces.description if route_spec.produces else None,
}

for (status_code, routefield) in route_spec.response:
responses["{}".format(status_code)] = {
"schema": serialize_schema(routefield.field),
"description": routefield.description,
}

if route_spec.produces:
responses["200"] = {
"schema": serialize_schema(route_spec.produces.field),
"description": route_spec.produces.description,
}
elif not responses:
responses["200"] = {"schema": None, "description": None}

endpoint = remove_nulls(
{
"operationId": route_spec.operation or route.name,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ def test(request):
assert swagger_json["paths"]["/"]["post"]["responses"] == responses


@pytest.mark.parametrize(
"produces_args, produces_kwargs, response_args, responses",
[
([], {}, [], {"200": {}}),
([doc.String], {}, [200, {}], {"200": {"schema": {"type": "string"}}}),
(
[TestSchema],
{"content_type": "application/json"},
[201, {}],
{
"200": {"schema": {"$ref": "#/definitions/TestSchema"}},
"201": {"schema": {"type": "object", "properties": {}}},
},
),
],
)
def test_produces_and_response(app, produces_args, produces_kwargs, response_args, responses):
@app.post("/")
@doc.produces(*produces_args, **produces_kwargs)
@doc.response(*response_args)
def test(request):
return text("test")

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
print(swagger_json["paths"]["/"]["post"]["responses"])
assert swagger_json["paths"]["/"]["post"]["responses"] == responses


def test_tag(app):
@app.get("/")
@doc.tag("test")
Expand Down