简单的 Swagger.v2 数组定义没有响应

Simple Swagger.v2 array definition not responding

我正在努力学习招摇。为 returns

的简单 api 创建模拟服务器

A) {id: someid, name: some name}

形式的对象

b) 这些对象的数组

我的第一部分工作正常,但第二部分不工作。知道的人可以看看下面我的 YAML 定义吗?

swagger: "2.0"

info:
  version: 1.0.0
  title: Simple API
  description: A simple API to learn how to write OpenAPI Specification

schemes:
  - http
host: localhost:8080
basePath: /

这里定义了两条路径,第一个(/api/dataset)有效,第二个(/api/datasets)无效。

paths:
  /api/dataset: 
    get:
      summary: summary
      description: desc
      responses:
        200:
          description: dataset
          schema:
            $ref: '#/definitions/dataset'

  /api/datasets: 
    get:
      summary: summary
      description: desc
      responses:
        200:
          description: datasets list
          schema:
            $ref: '#/definitions/datasets'

这些是定义,我怀疑我在这里做错了......

definitions:
  dataset:
    type: object
    properties:
      dataset_id:
        type: string
      name:
        type: string
    required: 
      - dataset_id
      - name
    example:
      dataset_id: FunnyJokesData
      name: FunnyJokesData
  datasets:
    type: array
    items: 
      $ref: '#/definitions/dataset'
    example:
      - dataset_id: example_01
        name: example_01
      - dataset_id: example_02
        name: example_02
      - dataset_id: example_03
        name: example_03

生成具有此定义的存根服务器后,curl 响应 /api/dataset 有一个响应正文:

$ curl -X GET "http://localhost:8080/api/dataset" -H "accept: application/json" % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 64 0 64 0 0 4000 0 --:--:-- --:--:-- --:--:-- 4000{ "dataset_id": "FunnyJokesData", "name": "FunnyJokesData" }

但“/api/datasets”的 curl 响应为空:

$ curl -X GET "http://localhost:8080/api/datasets" -H "accept: application/json" % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

我不明白为什么一个有效而另一个无效。

感谢观看

我猜 "won't work" 只适用于 Node.js 服务器存根,因为 Python/Flask 存根 returns 字符串 "do some magic!" 而不是 JSON 两个端点。

看起来Swagger Codegen的Node.js生成器不支持数组级example,所以相应的响应为空。您可以在此处提交错误报告:https://github.com/swagger-api/swagger-codegen/issues.

一种似乎适用于 Node.js 生成器的解决方法是改用 response examples

  /api/datasets: 
    get:
      summary: summary
      description: desc
      responses:
        200:
          description: datasets list
          schema:
            $ref: '#/definitions/datasets'

          examples:
            application/json:
              - dataset_id: example_01
                name: example_01
              - dataset_id: example_02
                name: example_02
              - dataset_id: example_03
                name: example_03