sanic-org / sanic-openapi

Easily document your Sanic API with a UI
https://sanic-openapi.readthedocs.io/
MIT License
505 stars 107 forks source link

[Bug] Content-Type inconsistency between doc.produces and doc.response #184

Closed wereii closed 3 years ago

wereii commented 4 years ago

Bug description

Using

@doc.produces({"status": str})

does not set (it actually does in the code, but sets it to None) content type on the route.

vs.

@doc.response(200, {"status": str})

This does automagically set the content type to application/json on the route.

Screenshots The screenshots of Swagger UI if this bug is related to it. image

To Reproduce

from sanic.blueprints import Blueprint
from sanic.response import json
from sanic_openapi import doc

health_BP = Blueprint("health", url_prefix="/status")

@health_BP.route("/produces")
@doc.produces({"status": str}, description='Always returns {"test": "OK"}.')
async def status_produces(_req):
    return json({"status": "OK"})

@health_BP.route("/response")
@doc.response(200, {"test": str}, description='Always returns {"test": "OK"}.')
async def status_response(_req):
    return json({"test": "ok"})

Expected behavior Getting automatic content-type... similar behaviour of the same/similar features/logic between these two doc.produces and doc.response(200, ...) - in this instance it's detection of Content-Type.

Basically I would expect that doc.produces is a shorthand for doc.responce(200, ...) but even looking under the hood, it seems to be somewhat different (why?).

Environment:

swagger.json ```json { "basePath": null, "definitions": {}, "host": null, "info": { "version": "1.0.0", "title": "API", "description": "", "termsOfService": "", "contact": { "email": null }, "license": { "name": null, "url": null } }, "paths": { "/status/produces": { "get": { "operationId": "health.status_produces", "consumes": [ "application/json" ], "produces": [ null ], "tags": [ "health" ], "parameters": [], "responses": { "200": { "schema": { "type": "object", "properties": { "status": { "type": "string" } } }, "description": "Always returns {\"test\": \"OK\"}." } } } }, "/status/response": { "get": { "operationId": "health.status_response", "consumes": [ "application/json" ], "produces": [ "application/json" ], "tags": [ "health" ], "parameters": [], "responses": { "200": { "schema": { "type": "object", "properties": { "test": { "type": "string" } } }, "description": "Always returns {\"test\": \"OK\"}." } } } } }, "schemes": [ "http" ], "security": null, "securityDefinitions": null, "swagger": "2.0", "tags": [ { "name": "health" } ] } ```
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.