在 Swagger 2.0 的定义中使用 "security" 关键字和 属性 名称

Use the "security" keyword a property name in a definition in Swagger 2.0

我的定义部分中有一个对象需要有一个 属性 命名的安全性,但是当我这样做时,swagger 编辑器给我错误消息:

Semantic error at definitions.User.properties.security.properties security requirements must match a security definition Jump to line 49

这是我的定义

definitions:
  Security:
    type: object
    properties:
      name:
        type: string
  User:
    type: object
    required: 
     - can
     - username
    properties:
      can:
        type: integer
        readOnly: true
      username:
        type: string
        format: email
        readOnly: true
      security:
        $ref: '#/definitions/Security'

有什么办法可以避免错误吗?

似乎是 Swagger 编辑器中的一个错误,需要报告。你所拥有的与 Old Swagger 完全没问题 Editor whereas the new Swagger Editor 正在报告你提到的问题。

bug 已于 2017 年 在 Swagger Editor 3.2.0+ 中修复。

问题是名为 security 的模型属性被错误地标记为安全定义的一部分。实际上 security 只有在全局级别或路径级别使用时才应被视为保留关键字;模型 属性 可以命名为 security.

我还看到了这个工具,它可以让你直观地构建你的 API 定义,然后将它们导出到 swagger(或导入):

https://apibldr.com

也许它可以帮助您建立 API 定义。