Swagger:通配符路径参数 [英] Swagger: wildcard path parameters

查看:82
本文介绍了Swagger:通配符路径参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个允许传入任意路径的 API,例如所有这些:

I have an API which allows any arbitrary path to be passed in, for example all of these:

  • /api/tags
  • /api/tags/foo
  • /api/tags/foo/bar/baz

是有效路径.我试着这样描述:

Are valid paths. I tried to describe it as follows:

 /tags{tag_path}:
    get:
      parameters:
        - name: tag_path
          in: path
          required: true
          type: string
          default: "/"

但是,https://generator.swagger.io 对路径中的斜杠进行编码,因此它不会工作.那么有没有办法在 Swagger 中描述我的 API?

However, https://generator.swagger.io encodes slashes in the path, so it doesn't work. So is there a way to describe my API in Swagger?

推荐答案

所以这不会很快得到支持(它甚至没有计划用于 Swagger 3.0),我不得不求助于一种解决方法.

So this is not going to be supported soon (it's not even planned for Swagger 3.0), and I have to resort to a workaround.

如果我有一个路径 /tags{tag_path} 并且我输入这样的内容作为 tag_path: /foo/bar,那么实际查询请求 URL 将是:/tags%2Ffoo%2Fbar.所以,我只是在我的后端添加了对此的支持:/tags* 的端点处理程序 urldecodes 路径(即 %2Ffoo%2Fbar),它变成了 /foo/bar 再次.

If I have a path /tags{tag_path} and I enter something like this as tag_path: /foo/bar, then the actual query request URL will be: /tags%2Ffoo%2Fbar. So, I just added support for that on my backend: the endpoint handler for /tags* urldecodes the path (which is %2Ffoo%2Fbar), and it becomes /foo/bar again.

是的,一个黑客,但它有效,总比没有好.就我而言,标签名称不能包含 / 字符,因此不存在冲突.当然,您的里程可能会有所不同.

Yes, a hack, but it works, and it's better than nothing. In my case, tag names can't contain the / character, so there's no conflict. Your mileage may vary, of course.

这篇关于Swagger:通配符路径参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆