首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >openapi,具有Swagger注释的springdoc将自由表单应用程序/x-www- form -urlencoded请求体拆分为单个字符。

openapi,具有Swagger注释的springdoc将自由表单应用程序/x-www- form -urlencoded请求体拆分为单个字符。
EN

Stack Overflow用户
提问于 2022-01-28 19:31:14
回答 1查看 421关注 0票数 2

我正试图从Springfox迁移到Swagger页面的springdoc,有一个端点让我很难使用springdoc。它模拟一个OAuth2令牌端点,接收一个应用程序/x表单-urlencoded请求体,允许不同的授权类型。下面有生成的openapi文档。根据Swagger页面,当使用类型对象的架构时,应该允许自由形式的数据。但是,当我传入在Springfox和swagger 2 (grant_type=authorization_code&code=xxxxxxxxxx&client_id=xxxxxxxxxx),上工作的示例值时,请求是这样构建的(注意正文):

代码语言:javascript
运行
复制
curl -X 'POST' \
  'http://localhost:8080/v1/token' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d '0=g&1=r&2=a&3=n&4=t&5=_&6=t&7=y&8=p&9=e&10=%3D&11=a&12=u&13=t&14=h&15=o&16=r&17=i&18=z&19=a&20=t&21=i&22=o&23=n&24=_&25=c&26=o&27=d&28=e&29=%26&30=c&31=o&32=d&33=e&34=%3D&35=x&36=x&37=x&38=x&39=x&40=x&41=x&42=x&43=x&44=x&45=%26&46=c&47=l&48=i&49=e&50=n&51=t&52=_&53=i&54=d&55=%3D&56=x&57=x&58=x&59=x&60=x&61=x&62=x&63=x&64=x&65=x'

是我在openapi yaml中做错了什么,还是我把请求体不正确地放在了swagger页面上?

自吹自擂文件:

OpenAPI YAML将用于https://editor.swagger.io/

代码语言:javascript
运行
复制
openapi: 3.0.1
info:
  title: Test API
  description: Testing
  version: "1.0"
servers:
  - url: http://localhost:8080
    description: Generated server url
security:
  - api: []
paths:
  /v1/token:
    post:
      tags:
        - token-controller
      description: Oauth 2 Access Token.
      operationId: getOauthAccessToken
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
            examples:
              authorization_code grant type:
                description: authorization_code grant type
                value: grant_type=authorization_code&code=xxxxxxxxxx&client_id=xxxxxxxxxx
              client_credentials grant type:
                description: client_credentials grant type
                value: grant_type=client_credentials&client_id=xxxxxxxxxx&client_secret=xxxxxxxxxx
              refresh_token grant type:
                description: refresh_token grant type
                value: grant_type=refresh_token&refresh_token=xxxxxxxxxx
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OauthAccessTokenResponseView_V1'
components:
  schemas:
    OauthAccessTokenResponseView_V1:
      type: object
      properties:
        scope:
          type: string
        access_token:
          type: string
        refresh_token:
          type: string
        token_type:
          type: string
        expires_in:
          type: integer
          format: int64
      description: 'The Oauth 2 Access Token response: https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/'
  securitySchemes:
    api:
      type: http
      scheme: bearer
EN

回答 1

Stack Overflow用户

发布于 2022-01-29 11:32:30

你的定义很好。这是一个bug (或限制?)在Swagger中与OpenAPI 3、examples关键字和表单数据相关。请在问题跟踪器:https://github.com/swagger-api/swagger-ui/issues中报告

我找不到为表单数据提供多个examples的工作方法。

从文档的角度来看,我建议更新请求体schema,以定义准确的预期properties。下面是使用oneOf和属性级example值的一个可能的变体。但不幸的是,还有其他带有呈现的已知 问题,并使用oneOf“试用”表单数据。

代码语言:javascript
运行
复制
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              oneOf:
                - $ref: '#/components/schemas/AuthorizationCodeTokenRequest'
                - $ref: '#/components/schemas/ClientCredentialsTokenRequest'
                - $ref: '#/components/schemas/RefreshTokenRequest'
      ...

components:
  schemas:
    AuthorizationCodeTokenRequest:
      type: object
      required:
        - grant_type
        - code
        - client_id
      properties:
        grant_type:
          type: string
          enum: [authorization_code]
        code:
          type: string
          example: xxxxxxxxxx
        client_id:
          type: string
          example: xxxxxxxxxx
    ClientCredentialsTokenRequest:
      type: object
      required:
        - grant_type
        - client_id
        - client_secret
      properties:
        grant_type:
          type: string
          enum: [client_credentials]
        client_id:
          type: string
          example: xxxxxxxxxx
        client_secret:
          type: string
          format: password
          example: xxxxxxxxxx
    RefreshTokenRequest:
      type: object
      required:
        - grant_type
        - refresh_token
      properties:
        grant_type:
          type: string
          enum: [refresh_token]
        refresh_token:
          type: string
          format: password
          example: xxxxxxxxxx

您实际需要记录令牌端点的有效负载吗?如果它是一个标准的OAuth 2.0令牌端点(按照RFC 6749 ),您可以用securitySchemes来定义它:

代码语言:javascript
运行
复制
paths:
  /something:
    get:
      description: One of the endpoints that require OAuth Bearer token.
      security:
        - oauth2: []  # <-----
      responses:
        ...

components:
  securitySchemes:
    oauth2:           # <-----
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: '???' # TODO
          tokenUrl: /v1/token
          refreshUrl: '???' # TODO
          scopes: {}
        clientCredentials:
          tokenUrl: /v1/token
          refreshUrl: '???' # TODO
          scopes: {}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70899304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档