我最近创建了一个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.
答案 0 :(得分:5)
我已经测试了你的规格,虽然我没有收到同样的错误,但规格确实无效。
如果您查看#/definitions/MatchData/properties/stats
,您会看到您定义了type: array
但是您没有在其旁边提供'items'属性来说明它是哪个数组(这是强制性的) )。您可能打算使用type: integer
之类的属性,与format: int64
一起使用。
由于我不知道您打算提供什么,因此很难提供准确的解决方案,但如果您根据自己的意图添加评论,我可以提供更详细的答案。
经过一些额外的测试,我发现UI中存在一个错误。在进行修改并加载规范后,除非您单击Expand Operations
链接,否则操作本身不会展开。我打开了一个关于它的issue,随时可以在那里关注它。
答案 1 :(得分:0)
这个问题可能是由于yaml文件中的一些缩进错误,实际上没有在Swagger编辑器中显示。检查所有定义,以及它们是否在预览中按预期显示,您可以在Swagger编辑器中看到(特别是检查MatchData)。
你也可以尝试给予:
responses:
200:
description: Returns match data
schema:
type: array
items:
schema:
$ref: '#/definitions/MatchData'
答案 2 :(得分:0)
对于我们的情况,我们使用Swagger-php,我们有: * @SWG \ Response( *响应= 200, * description =" app response" * @SWG \ Schema( * type =" array" *) *),
但我们错过了#34; * @SWG \ Items(ref ="#/ definitions / pet")"。删除" @SWG \ Schema(",它可以工作,例如。
* @SWG\Response(
* response=200,
* description="app response"
* ),