Swagger UI 2.1坚持"获取资源列表"

 书友76086805 发布于 2022-12-10 12:38

我有一个最近创建的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 .

在此输入图像描述 在此输入图像描述

我正在使用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.

Ron.. 5

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

如果你看一下#/definitions/MatchData/properties/stats,你会看到你定义了type: array但是你没有在它旁边提供一个'items'属性来说明它是哪个数组(这是必须的).您可能打算type: integer像上面的属性一样使用它,这与它一起使用format: int64.

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

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

1 个回答
  • 我已经测试了你的规范,虽然我没有得到同样的错误,但规范确实无效.

    如果你看一下#/definitions/MatchData/properties/stats,你会看到你定义了type: array但是你没有在它旁边提供一个'items'属性来说明它是哪个数组(这是必须的).您可能打算type: integer像上面的属性一样使用它,这与它一起使用format: int64.

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

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

    2022-12-11 02:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有