首页 文章

Swagger指定两次相同的路径

提问于
浏览
0

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

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

对于前者我有一个名为/ oauth / token的 endpoints ,我希望用OAuth授权代码流的一组参数和带有不同参数集的client_credentials流的相同 endpoints / oauth / token文档进行记录 .

/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流的相同 endpoints

/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

1 回答

  • 2

    由于问题是关于OAuth2而不是具有不同参数的单个 endpoints ,因此解决方案实际上是不同的 .

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

    这些是使用Security Definitions Object描述的,它位于顶部Swagger object .

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

    完成后,您可以指定所需的安全方案 . 您可以在Swagger objectOperation level处为所有操作指定它 .

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

相关问题