简体   繁体   中英

Swagger UI 2.1 Stuck “fetching resource list”

I have a RESTful API that I have created recently and I won't remember how to use it in a few months. I decided to document my API using Swagger, however I'm going crazy.

I used http://editor.swagger.io/ to create the YAML file that I then convert into a JSON file Swagger can use. When I put file into Swagger UI it just gets stuck at fetching resource list: localhost/swagger.json and the console says Uncaught TypeError: Cannot read property '$ref' of undefined .

在此处输入图片说明在此处输入图片说明

I'm using version 2.1.0-alpha.5 of Swagger UI.

Here is my spec file:

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.

I've tested your spec, and while I'm not getting the same error you do, the spec is indeed invalid.

If you look at #/definitions/MatchData/properties/stats , you'll see that you define the type: array but you don't provide an 'items' property next to it to say which array it is (and that's mandatory). You may have intended to use type: integer like the properties above it, which goes along with the format: int64 .

Since I don't know what you intended to provide, it's difficult to give an accurate solution, but if you add a comment with what you intended to do, I could provide a more detailed answer.

Upon some additional testing, I discovered that there's a bug in the UI. After you make that modification and the spec loads, the operation itself will not expand unless you click on the Expand Operations link. I've opened an issue about it, feel free to follow it there.

This problem can be due to some indentation errors in the yaml file which actually did not show up in the Swagger editor. Check all your definitions and whether they are getting displayed as expected in the preview that you can see in Swagger editor (especially check MatchData).

You can also try giving:

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

For our case, we used Swagger-php and we have: * @SWG\\Response( * response=200, * description="app response" * @SWG\\Schema( * type="array" * ) * ),

but we missed " * @SWG\\Items(ref="#/definitions/pet")". After removing "@SWG\\Schema(", it works eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM