如何链接到 Swagger 中的另一个端点

Adr*_*enW 6 yaml swagger swagger-2.0 swagger-editor

我正在为需要非常详细和干净的文档的未来公共 API 编写 Swagger 规范。有没有办法在swagger.yml文件中的其他位置引用/链接/指向另一个端点?

例如,这是我想要实现的目标:

paths:
  /my/endpoint:
    post:
      tags:
        - Some tag
      summary: Do things
      description: >
        This endpoint does things.
        See /my/otherEndpoint for stuff  # Here I would like to have some kind of hyperlink
      operationId: doThings
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        ...
      responses:
        ...
  /my/otherEndpoint:  # This is the endpoint to be referenced to
    get:
      ...
Run Code Online (Sandbox Code Playgroud)

我发现这$ref无济于事,因为它只是用引用的内容替换了自己。

Swagger 可以做这样的事情吗?

Hel*_*len 12

如果 Swagger UI配置了该选项,则它会为标记和操作提供永久链接deepLinking: true。这些永久链接是基于标签名称和operationId(或者如果没有operationId- 基于端点名称和 HTTP 动词)生成的。

index.html#/tagName
index.html#/tagName/operationId
Run Code Online (Sandbox Code Playgroud)

您可以在 Markdown 标记中使用这些永久链接:

index.html#/tagName
index.html#/tagName/operationId
Run Code Online (Sandbox Code Playgroud)

笔记:

  • Markdown 链接(如上)当前在新的浏览器选项卡中打开(如target="_blank") - 请参阅问题 #3473
  • HTML 格式的链接<a href="#/tagName/operationId">foobar</a>目前不起作用
  • Swagger Editor 不支持此类固定链接。

  • 是的,这取决于我们用来渲染 openapi 文档的工具。就我而言,我使用 [RapiDoc](https://mrin9.github.io/RapiDoc/)。对于“发送消息”标记的 API,我们可以将链接指定为 -&gt; [发送消息](#tag--send-message)。[更多信息](https://mrin9.github.io/RapiDoc/examples/links) (2认同)