首页 文章

Swagger UI 2.1坚持“获取资源列表”

提问于
浏览
7

我有一个最近创建的RESTful API,我不记得几个月后如何使用它 . 我决定用Swagger记录我的API,但是我疯了 .

我使用http://editor.swagger.io/创建了YAML文件,然后我将其转换为Swagger可以使用的JSON文件 . 当我将文件放入Swagger UI时,它只是停留在 fetching resource list: localhost/swagger.json 并且控制台说 Uncaught TypeError: Cannot read property '$ref' of undefined .

enter image description here

enter image description here

我正在使用Swagger UI的2.1.0-alpha.5版本 .

这是我的spec文件:

swagger: '2.0'
info:
  title: TITLE
  description: BLAH, BLAH, BLAH, ETC
  version: "1.0b"
host: api.example.com
schemes:
 - http
basePath: /v1
produces:
 - application/json
paths:
  /match.json:
    get:
     #summary: Match Data
      description: Used for getting data about a match
      parameters:
        - name: id
          in: query
          description: The match ID of from a game
          required: true
          type: integer
          format: int32
        - name: key
          in: query
          description: API key used for authentication.
          required: true
          type: string
      responses:
        200:
          description: Returns match data
          schema:
            type: array
            items:
              $ref: '#/definitions/MatchData'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'

definitions:
  MatchData:
    properties:
      info:
        type: integer
        format: int64
        description: General information about the match
      time:
        type: integer
        format: int64
        description: Information about the start/end time
      stats:
        type: array
        format: int64
        description: Stats about the match
  Error:
    required:
     - errorID
      - message
    properties:
      errorID:
        type: string
        description: Error ID.
      message:
        type: string
        description: Information about the error.

3 回答

  • 5

    我已经测试了你的规格,虽然我没有得到同样的错误,但规格确实无效 .

    如果你看 #/definitions/MatchData/properties/stats ,你会看到你定义 type: array 但你必须要't provide an '项目' property next to it to say which array it is (and that' . 您可能打算像上面的属性一样使用 type: integer ,它与 format: int64 一起使用 .

    由于我不知道您打算提供什么,因此很难提供准确的解决方案,但如果您根据自己的意图添加评论,我可以提供更详细的答案 .

    经过一些额外的测试,我发现UI中存在一个错误 . 进行修改并加载规范后,除非单击 Expand Operations 链接,否则操作本身不会展开 . 我已经开了一个issue关于它,随便在那里关注它 .

  • 0

    这个问题可能是由于yaml文件中的一些缩进错误实际上没有出现在Swagger编辑器中 . 检查所有定义,以及它们是否在预览中按预期显示,您可以在Swagger编辑器中看到(特别是检查MatchData) .

    你也可以尝试给予:

    responses:
    200:
      description: Returns match data
      schema:
        type: array
        items:
          schema:
            $ref: '#/definitions/MatchData'
    
  • 0

    对于我们的情况,我们使用了Swagger-php,我们有:* @SWG \ Response(* response = 200,* description =“app response”* @SWG \ Schema(* type =“array”),

    但我们错过了“* @SWG \ Items(ref =”#/ definitions / pet“)” . 删除“@SWG \ Schema(”后,它可以工作,例如

    *     @SWG\Response(
     *         response=200,
     *         description="app response"
     *     ),
    

相关问题