【问题标题】:swagger 3: schema for dictionary of dictionariesswagger 3:字典字典的模式
【发布时间】:2018-02-05 14:27:46
【问题描述】:

我想为响应是字典字典的模式建模:

{
  'id1': {
    'type': 'type1',
    'active': true,
  }, 
  'id2': {
    'type': 'type2',
    'active': false,
  },
  ... 
  ... 
}

我已经定义了:

components:
  schemas:
    accountData:
      type: object
      required:
        - type
        - active
      properties:
        type:
          type: string
        active:
          type: boolean

    accounts:
      type: object
      additionalProperties:
        type: object
        properties:
          account-id:
            type: object
            $ref: '#/components/schemas/accountData'

现在我想定义“#/components/schemas/accounts”,它是“#/components/schemas/account”的重复字典。

我对 OpenApi 3 很陌生,我不知道该怎么做。

【问题讨论】:

  • 你试过什么?那怎么行不通?看看这是否有帮助:Swagger HashMap property typeSwagger: map of <string, Object>
  • 所以你有一个&lt;string, Object&gt; 地图。在我之前的评论中查看链接问题的答案。这些答案适用于 OpenAPI 2.0,因此请将 definitions 替换为 components.schemas 以适应 3.0 语法。
  • @helen 我已经更新了这个问题。你能检查一下我做得对吗?

标签: swagger swagger-editor openapi


【解决方案1】:

你快到了。 additonalProperties 中指定的模式对应于&lt;key, value&gt; 字典中的值类型。在您的示例中,值是 accountData 对象,因此您应该使用:

components:
  schemas:
    ...

    accounts:
      type: object
      additionalProperties:
        $ref: '#/components/schemas/accountData'

【讨论】:

  • 帐户架构中的 在哪里?
  • @ArindamChoudhury:在您的示例中,键是 'id1''id2' 等。在 OpenAPI 中,键始终是字符串,并且没有明确定义。
  • @Helen,你知道吗 - 我如何使用 java 注释来做到这一点?
  • @DanBrandt 我不知道,抱歉。考虑 asking a new question 关于 Java 注释。
猜你喜欢
  • 1970-01-01
  • 2020-08-27
  • 2018-07-29
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 2018-01-05
相关资源
最近更新 更多