首页 > 解决方案 > 在 Swagger 文档中定义对象数组

问题描述

我现在使用 swagger 已经很长时间了,我们已经开始使用它来记录我们的代码,在一个地方有一个 API 响应,它在包含的块中返回多个对象。

例子:

{
    "data": {
        "id": "1",
        "type": "schoolPositions",
        "attributes": {
            "description": "teases the students",
            "mustHaves": "principle"
        },
        "relationships": {
            "schoolLocation": {
                "data": {
                    "id": "72",
                    "type": "schoolLocations"
                }
            },
            "schoolCompensation": {
                "data": {
                    "id": "75",
                    "type": "schoolCompensations"
                }
            },
            "jobSpecs": {
                "data": [
                    {
                        "id": "82",
                        "type": "schoolAttachments"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "id": "72",
            "type": "schoolLocations",
            "attributes": {
                "city": "Berhampore",
                "state": "West Bengal",
                "postalCode": "742101",
                "country": "India",
                "globalRegionId": 30,
                "regionId": 683
            }
        },
        {
            "id": "75",
            "type": "schoolCompensations",
            "attributes": {
                "salary": "",
                "bonus": "",
                "equity": "",
                "currencyId": null,
                "equityType": "percent",
                "salaryDescription": null
            }
        },
        {
            "id": "82",
            "type": "schoolAttachments",
            "attributes": {
                "attachmentType": "JobSpecificationAttachmentType",
                "fileFileName": "vs.jpg",
                "fileContentType": "image/jpeg",
                "fileFileSize": 2410039,
                "fileUpdatedAt": "2018-12-12T07:06:38Z",
                "downloadUrl": "001-vs.jpg?1544598398",
                "klass": "SchoolAttachments"
            }
        }
    ]

我在互联网和文档上浪费了一整天的时间试图记录包含的部分,但我在某个地方出错了

response 200 do
    key :description, 'School Data'
    schema do
      property :data do
        key :type, :array
        items do
          key :'$ref', :School
        end
      end

      property :included do
        key :type, :array
        items do
          key :'$ref', :SchoolLocations
          key :'$ref', :SchoolCompensations
          key :'$ref', :SchoolAttachments
        end
      end
    end
  end

这仅显示SchoolAttachments包含的部分。

我尝试过使用allOff,但它不起作用。

标签: ruby-on-railsswaggerswagger-2.0code-documentation

解决方案


推荐阅读