Skip to content

Commit

Permalink
Merge pull request #207 from denisovkv/feature/using_both_produces_an…
Browse files Browse the repository at this point in the history
…d_response

Using both produces and response
  • Loading branch information
ahopkins authored Mar 16, 2021
2 parents 18c719b + 5061b17 commit 9cd6d76
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
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 @@ -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
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

0 comments on commit 9cd6d76

Please sign in to comment.