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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/sanic_openapi/decorators.md
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
Expand Up @@ -184,18 +184,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}

y = YamlStyleParametersParser(inspect.getdoc(_handler))
autodoc_endpoint = y.to_openAPI_2()

Expand Down
32 changes: 32 additions & 0 deletions tests/test_decorators.py
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