【问题标题】:Unable to include json schema in swagger document无法在招摇文档中包含 json 架构
【发布时间】:2020-02-28 12:20:56
【问题描述】:

以前使用 RAML 来定义 API 定义,我的工作是研究切换到使用 Swagger v3,因为它现在支持 YAML 样式语法。

我希望将带有 json 模式和示例的非常小的 RAML 文档迁移到 swagger 3,我正在努力将 json 模式导入 swagger

我一直在使用https://swagger.io/docs/ 的文档并查看其他各种博客文章

这是我正在使用并试图进入 swagger 的 json 架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "analytics": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "date": {
              "type": "string",
              "format": "date"
            },
            "submission": {
              "type": "string"
            },
            "source": {
              "type": "string"
            },
            "model": {
              "type": "string"
            },
            "count": {
              "type": "integer"
            }
          },
          "additionalProperties": false,
          "required": [
            "date",
            "submission",
            "source",
            "model",
            "count"
          ]
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "analytics"
  ]
}

现在查看 swagger 文档和其他帖子,我应该使用 components 标记定义架构,因为它可以作为 $ref: '#/components/schemas/validresponse' 引用。

这是我使用 swagger 文档创建的

components:
  schemas:
    validresponse:
      additionalProperties: true
      analytics:
        type: object
        properties: 
          analytics:
            type: array
            items:
              type: object
              properties :
                date:
                  type: string
                  format: date
                submission:
                  type: string
                source:
                  type: string
                model:
                  type: string
                count:
                  type: integer
                required: 
                  - date
                  - submission
                  - source
                  - model
                  - count

我正在使用 app.swaggerhub.com 编辑器,但出现错误

"不应该有额外的属性 additionalProperty: analytics " 在有效响应行上突出显示

如果我删除了附加属性行,那么我仍然会收到错误

我在我的 swagger 文档中没有提到 AdditionalProperty

我不知道为什么这不起作用。我希望这是我做错了一些简单的事情

【问题讨论】:

    标签: json swagger jsonschema


    【解决方案1】:

    改变

    components:
      schemas:
        validresponse:
          additionalProperties: true
          analytics:
            type: object
            properties: 
              analytics:
                type: array
                ...
    

    进入

    components:
      schemas:
        validresponse:
          additionalProperties: true
                         # <--- no "analytics:" node here.
          type: object   # <--- "type: object" must be indented same as additionalProperties above
          properties: 
            analytics:
              type: array
              ...
    

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 1970-01-01
      • 2023-03-11
      • 2020-07-02
      • 2022-09-30
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2017-12-24
      相关资源
      最近更新 更多