【问题标题】:Swagger same path specified twice两次指定相同的路径
【发布时间】:2015-05-07 22:40:13
【问题描述】:

在一个由 Swagger-UI 呈现的 API 规范中,是否有可能多次出现相同的路径?

我应该创建单独的 api 规范并加载两个 Swagger-UI 实例吗?处理这个问题的最佳方法是什么?

例如。我有一个名为 /oauth/token 的端点,我想用一组参数记录 OAuth 授权代码流,并使用相同的端点 /oauth/token 文档记录 client_credentials 流的不同参数集。

/oauth/token:
    post:
      summary: token endpoint for authorization_code flow
      parameters:
        - name: code
          type: string
          description: Required for Authorization Code Flow
          in: formData
          required: true
        - name: grant_type
          type: string
          description: Grant Type should be specified as authorization_code
          in: formData
          required: true
          default: authorization_code
          enum:
          - authorization_code
          - client_credentials
        - name: client_id
          type: string
          description: Consumer Key
          in: formData
          required: true
        - name: client_secret
          type: string
          description: Consumer Secret
          in: formData
          required: true
        - name: endOtherSessions
          in: formData
          type: boolean
          required: false
          default: false
          description: Optional parameter. Default is false - do not allow concurrent login. Send true to end any other user sessions.
      tags:
        - OAuth2 Authorization Code

client_credentials 流的相同端点

/oauth/token2:
     post:
       summary: token for client credentials
       parameters:
         - name: grant_type
           type: string
           description: Grant Type should be specified as client_credentials
           in: formData
           required: true
           default: client_credentials
           enum:
          - authorization_code
          - client_credentials
         - name: client_id
           type: string
           description: Consumer Key
           in: formData
           required: true
         - name: client_secret
           type: string
           description: Consumer Secret
           in: formData
           required: true
       tags:
         - OAuth2 Client Credentials

【问题讨论】:

  • 您是否只要求 OAuth2 端点?因为如果是这样,您根本不需要指定这些。 Swagger 对 OAuth2 安全性的描述有所不同。
  • 它适用于 oauth2 和常规资源端点。 Oauth2 是我第一次遇到需要重复相同路径的情况。
  • 你能举一个非 oauth2 路径的例子吗?因为它真的与它无关。我可以为您提供 oauth2 的具体解决方案,但更愿意看到您遇到的实际问题。
  • 谢谢罗恩。到目前为止,我没有关于非 oauth 的具体问题。你能帮忙解决一下 oauth2 的具体情况吗?

标签: swagger swagger-ui


【解决方案1】:

由于问题是关于 OAuth2 而不是具有不同参数的单个端点,那么解决方案实际上是不同的。

Swagger 有一种特定的方式来记录授权方法,包括 4 种常见的 OAuth2 流程。

这些使用位于顶部Swagger objectSecurity Definitions Object 进行描述。

您可以在其中定义一个或多个 OAuth2 流。规范本身为implicit 流程提供了一个示例,但其他规范遵循类似的结构。区别在于提供哪些字段,即authorizationUrltokenUrl(取决于流类型)。

一旦你有了它,你就可以指定需要哪些安全方案。您可以在Swagger objectOperation level 为所有操作指定它。

规范允许您定义一组安全方法是必需的,或者用户可以在给定的集合之间进行选择。

【讨论】:

  • 谢谢罗恩。这有助于记录 api 规范,但没有好的方法可以从 Swagger-UI 显示 OAuth2 安全定义文档,因此我试图复制类似于 api.gettyimages.com/swagger/ui/index.html#!/OAuth 的 swagger 路径和文档(这是 swagger 版本 1.2)
  • 看起来 OAuth 部分是作为 HTML 完成的,而不是使用 Swagger-UI。
  • 好吧,你不能那样做。有一个功能要求 swagger-ui 显示可用的证券,不能说是否/何时实施。 Swagger 并未正式支持 Getty Images 在规范范围内所做的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 2012-07-05
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多