为什么我的swagger-server会产生错误?

时间:2018-03-20 07:11:09

标签: node.js yaml swagger

在Swagger编辑器中从“Generate Server”启动nodejs-server中的index.js时,我发现了关于swagger yaml的错误。

    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/2: Not a valid parameter definition
    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/2: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/2: Missing required property: $ref
  #/paths/~1botManagement~1deleteScenario~1/delete/parameters/1: Not a valid parameter definition
    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/1: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/1: Missing required property: $ref
  #/paths/~1botManagement~1deleteScenario~1/delete/parameters/0: Not a valid parameter definition
    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/0: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1deleteScenario~1/delete/parameters/0: Missing required property: $ref
  #/paths/~1botManagement~1stopScenario~1/post/parameters/2: Not a valid parameter definition
    #/paths/~1botManagement~1stopScenario~1/post/parameters/2: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1stopScenario~1/post/parameters/2: Missing required property: $ref
  #/paths/~1botManagement~1stopScenario~1/post/parameters/1: Not a valid parameter definition
    #/paths/~1botManagement~1stopScenario~1/post/parameters/1: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1stopScenario~1/post/parameters/1: Missing required property: $ref
  #/paths/~1botManagement~1stopScenario~1/post/parameters/0: Not a valid parameter definition
    #/paths/~1botManagement~1stopScenario~1/post/parameters/0: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1stopScenario~1/post/parameters/0: Missing required property: $ref
  #/paths/~1botManagement~1restartScenario~1/post/parameters/2: Not a valid parameter definition
    #/paths/~1botManagement~1restartScenario~1/post/parameters/2: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1restartScenario~1/post/parameters/2: Missing required property: $ref
  #/paths/~1botManagement~1restartScenario~1/post/parameters/1: Not a valid parameter definition
    #/paths/~1botManagement~1restartScenario~1/post/parameters/1: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1restartScenario~1/post/parameters/1: Missing required property: $ref
  #/paths/~1botManagement~1restartScenario~1/post/parameters/0: Not a valid parameter definition
    #/paths/~1botManagement~1restartScenario~1/post/parameters/0: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1restartScenario~1/post/parameters/0: Missing required property: $ref
  #/paths/~1botManagement~1startScenario~1/post/parameters/2: Not a valid parameter definition
    #/paths/~1botManagement~1startScenario~1/post/parameters/2: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1startScenario~1/post/parameters/2: Missing required property: $ref
  #/paths/~1botManagement~1startScenario~1/post/parameters/1: Not a valid parameter definition
    #/paths/~1botManagement~1startScenario~1/post/parameters/1: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1startScenario~1/post/parameters/1: Missing required property: $ref
  #/paths/~1botManagement~1startScenario~1/post/parameters/0: Not a valid parameter definition
    #/paths/~1botManagement~1startScenario~1/post/parameters/0: Not a valid parameter definition
      #: Missing required property: schema
      #: Missing required property: type
    #/paths/~1botManagement~1startScenario~1/post/parameters/0: Missing required property: $ref

yaml文件在下面。

swagger: "2.0"
info:
  description: I wrote something but can't describe it.
  version: 1
  title: I wrote something but can't describe it.
  termOfService: I wrote something but can't describe it.
  contact:
    email: xxxx@xxxx
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
host: xxxx.xxxx.xxxx.xxxx
basePath: /v1
tags:
  - name: I wrote something but can't describe it.
schemes:
  - https
paths:
  /XXXXXXXXXX:
    get:
      summary: I wrote something but can't describe it.
      description: I wrote something but can't describe it.
      responses:
        200:
          description: Successful response
          schema:
            type: object
            properties:
              pid:
                type: integer
                format: int64
                example: 0
              name:
                type: string
                example: sample
              pm2_env:
                type: object
                example: {username:sample, windowsHide:true...}
              pm_id:
                type: integer
                format: int64
                example: 0
              monit:
                type: object
                example: {memory:0, cpu:0}
        404:
          description: API not found
        500:
          description: Internal server error

  /xxxxx/xxxxx:
    post:
      summary: I wrote something but can't describe it.
      description: I wrote something but can't describe it.
      parameters:
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: true
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
      responses:
        200:
          description: Successful response
          schema:
            type: object
            properties:
              scenario:
                type: string
                example: sample.js
              status:
                type: string
                example: I wrote something but can't describe it.
        400:
          description: I wrote something but can't describe it.
        404:
          description: "API not found"
        500:
          description: "Internal server error"

  /xxxxx/xxxxx/:
    post:
      summary: I wrote something but can't describe it.
      description:
          I wrote something but can't describe it.
      parameters:
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: true
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
      responses:
        200:
          description: Successful response
          schema:
            type: object
            properties:
              scenario:
                type: string
                example: sample.js
              status:
                type: string
                example: I wrote something but can't describe it.
        400:
          description: Bad request
        404:
          description: API not found
        500:
          description: Internal server error

  /xxxxx/xxxxx/:
    post:
      summary: I wrote something but can't describe it.
      description:
          I wrote something but can't describe it.
      parameters:
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: true
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
      responses:
        200:
          description: Successful response
          schema:
            type: object
            properties:
              scenario:
                type: string
                example: sample.js
              status:
                type: string
                example: I wrote something but can't describe it.
        400:
          description: Bad request
        404:
          description: API not found
        500:
          description: Internal server error

  /xxxxx/xxxxx/:
    delete:
      summary: I wrote something but can't describe it.
      description:
          I wrote something but can't describe it.
      parameters:
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: true
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
        - name: xxxxx
          in: formData
          description: I wrote something but can't describe it.
          required: false
          type: string
      responses:
        200:
          description: Successful response
          schema:
            type: object
            properties:
              scenario:
                type: string
                example: sample.js
              status:
                type: string
                example: I wrote something but can't describe it.
        400:
          description: Bad request
        404:
          description: API not found
        500:
          description: Internal server error

它看起来是“$ ref”的缺失属性。 我知道这个属性是用什么的。但我不需要任何其他文档的参考。 在这种情况下,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的定义有语法错误。

1)version需要字符串值,因此它应为version: '1',而不是version: 1。没有引号1被解析为数字。

2)example: {username:sample, windowsHide:true...}无效YAML,应为

example:
  username: sample
  windowsHide: true

或使用JSON语法:

example: {"username": "sample", "windowsHide": true}

与其他内联对象示例相同。

3)使用in: formData参数的操作应在application/x-www-form-urlencoded关键字中指定multipart/form-dataconsumes

consumes:
  - application/x-www-form-urlencoded


如果没有看到你的实际定义,很难说还有什么可能是错的。您可以将YAML粘贴到https://editor.swagger.io以检查语法错误,或者如果您的定义有公共网址,请使用在线验证器

http://online.swagger.io/validator/debug?url=YOUR_URL