[Bug] Swagger cant show path variable
amitabduls15 opened this issue · comments
I can't enter a path variable on my endpoint in swagger, but when using curl it does.
code:
@bp_login.route('/registration/<level:int>', methods=['POST','OPTIONS'])
@doc.consumes(schemas.registration_request, location="body")
async def registration(request, level):
start = time.time()
try:
req_data = request.json
# print(req_data)
data, err_msg = models.registration(connection, req_data, level)
# print(data)
end = time.time()
if len(data) > 0:
return response.json(models.default_response(data,end-start))
else:
return response.json(models.response_eror(err_msg,end-start), status=501)
except Exception as err:
logger.error(err)
ServerError("OSError occured", status_code=500)
Here is a screenshot of my swagger:
In the screenshot above the path variable I mean is level.
Environment
- OS: arch linux last update 08 juni 2021
- Browser : opera
- Python : 3.9.5
- Sanic : 20.12.3
- sanic-openapi : 21.3.2
I don't know if it's an error in my code or it's a bug, but in sanic-openapi version 0.6.* this code is what I usually use and it works.
thanks
same problem,but in HTTPMethodView way.
class Source(HTTPMethodView):
@doc.summary("监听指定channelid的计时器")
@doc.tag("event")
# @doc.consumes(doc.String(name="channelid", description="channel的唯一id.", required=True), location="uri")
@doc.produces(doc.String(description="sse消息"), description="监听流", content_type="text/event-stream;charset=UTF-8")
async def get(self, request: Request, channelid: str) -> HTTPResponse:
ok = await redis.sismember("timer::channels", channelid)
log.info('redis.sismember("timer::channels", channelid) result', result=ok)
if not ok:
return json({"msg": "未找到channel"}, status=404, ensure_ascii=False)
p = redis.pubsub()
await p.subscribe(f"timer::{channelid}")
response = await request.respond(
content_type="text/event-stream;charset=UTF-8",
headers={
"Connection": "keep-alive",
"Transfer-Encoding": "chunked",
"Cache-Control": "no-cache"
}
)
p = redis.pubsub()
await p.subscribe('timer::golbal')
log.info("subscribe ok")
while True:
message = await p.get_message(ignore_subscribe_messages=True, timeout=0.01)
if message is not None:
log.info("get msg", msg=message)
sse = SSE.from_content(message["data"])
if sse.event == "EOF":
await response.send(SSE(comment="EOF").render(), True)
break
else:
await response.send(message["data"])
return response
timernamespace.add_route(Source.as_view(), "/<channelid:string>", strict_slashes=True)
It appears that adding the parameter to @doc.consumes(...)
with location
can solve this issue as @nuxion mentioned above.
@amitabduls15, do you still have any problem with it?
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.