当前位置: 首页 > 知识库问答 >
问题:

openapi, springdoc with Swagger注释将自由形式的应用程序/x-ows-form-urlencoded请求正文拆分为单个字符

谷梁涵忍
2023-03-14

我正在尝试从Springfox迁移到springdoc,以实现我们的招摇过市页面,有一个endpoint我很难与springdoc合作。它模仿OAuth2令牌endpoint,接收一个应用程序/x-www-form-urlencoded请求体,允许使用不同的授权类型。我有下面生成的openapi文档。根据Swagger页面,当使用object类型的模式时,应该允许自由形式的数据。然而,当我传入在Springfox和swagger 2上工作的示例值时(grant_type=authorization_code)

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中是否做错了什么,或者我在大摇大摆的页面上错误地放入了请求正文?

OpenAPI YAML将在https://editor.swagger.io/使用

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

共有1个答案

宰父淳
2023-03-14

您的定义看起来不错。这是SwaggerUI中与OpenAPI 3示例关键字和表单数据相关的错误(或限制?)。请在问题跟踪器中报告:https://github.com/swagger-api/swagger-ui/issues

我找不到一种有效的方法来为表单数据提供多个示例。

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

      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

您真的需要记录令牌endpoint的有效负载吗?如果它是符合RFC 6749的标准OAuth 2.0令牌endpoint,您可以在securitySchemes中定义它:

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: {}

 类似资料:
  • 我目前正在开发wp8。在应用程序C#中,我通过从textbox创建一个json对象(bm),成功地将json中的POST方法执行到我的api中。短信。下面是我的代码。我该如何使用相同的文本框。文本并以的形式发布。代码是什么?

  • 我的要求很简单。 响应是xml格式的。 我尝试了很多地方的例子,但似乎没有任何效果。它返回401 Unauthorized,如果请求的格式不正确,目标API就会抛出这个错误。

  • 我正在使用Jersey RESTful Web服务框架开发一个REST服务。 需要使用url编码的表单content-type,并将其解释/验证为bean。 当资源使用application/json或application/xml时,这是可能的,但是在application/x-www-form-urlencoded的情况下,我会收到'415-Unsupported Media type'响应。

  • Jersey文档给出了如何在资源上注入HttpSession的示例。我应该如何注入(或以其他方式访问)在请求中发送的表单参数,并使用“Content Type:application/x-www-form-urlencoded”?我看到这些都是作为参数在方法上传递的,而且似乎没有注释,让我相信这里有一些怪癖? 我目前使用的(naive)工厂实现如下,JerseyHttpServletRequest

  • RESTAPI在映射到Java对象时采用输入内容类型:application/x-www-form-urlencoded,如 在表单输入请求中,我正在设置my_name和my_phone的值,但MyRequest对象的myName和myPhone为null。 我正在使用Jackson annotations 2.3 jar 有什么建议吗?可能有什么问题?

  • 问题内容: 我有一些要以表单编码形式发布到服务器的参数: 我正在这样发送我的请求(当前没有参数) 如何在请求中包含表单编码的参数? 问题答案: 对于上载表单编码的POST请求,我建议使用FormData对象。 示例代码: