sanic-org / sanic-openapi

Easily document your Sanic API with a UI

Home Page:https://sanic-openapi.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[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.

image

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)

Same here, i cant edit path variables.

Captura de Tela 2021-08-25 às 18 04 55

@ahopkins is there something we need to add in the @doc decorator so that the path variable should be edited on browser? I didnt find any info about this on the project readthedocs.

Hi you can use the "path" value in the location param:

@app.get("/trends/<woeid:int>")
@doc.consumes(doc.Integer(name="woeid"), location="path")

path_example

I hope this help.

@doc.consumes(doc.Integer(name="woeid"), location="path")

Hi you can use the "path" value in the location param:

@app.get("/trends/<woeid:int>")
@doc.consumes(doc.Integer(name="woeid"), location="path")

path_example

I hope this help.

Worked. Thanks

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?

commented

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.