-->

扬鞭HashMap的属性类型(Swagger HashMap property type)

2019-08-31 08:00发布

有什么办法来定义模型部分是一个HashMap或通用对象类型? 我有一个返回产品REST服务,这些产品可以有不同的选择。 选项属性基本上是一个HashMap,其中id是选项名称和其价值是期权价值。

Answer 1:

是的,它是可能的。

在所述OpenAPI(FKA扬鞭)2.0和3.0,一个HashMap总是<string, something>图:

  • 关键是始终是一个字符串,并不需要定义。
  • 值类型是你想要的,并与定义additionalProperties

比方说,你要描述一个<string, string> HashMap中像这样的:

{
  "key1": "value1",
  "key2": "value2"
}

相应的OpenAPI 2.0定义将是:

definitions:
  StringStringMap:
    type: object
    additionalProperties:
      type: string

在3.0 OpenAPI的定义是:

components:
  schemas:
    StringStringMap:
      type: object
      additionalProperties:
        type: string


<string, object>散列映射像这样

{
  "key1": {"someData": "data", "someOtherData": true},
  "key2": {"someData": "data2", "someOtherData": false}
}

将被定义这种方式中的OpenAPI 2.0:

definitions:
  ComplexObject:
    type: object
    properties:
      someData:
        type: string
      someOtherData:
        type: boolean

  StringObjectMap:
    type: object
    additionalProperties:
      $ref: "#/definitions/ComplexObject"

在OpenAPI的3.0:

components:
  schemas:
    ComplexObject:
      type: object
      properties:
        someData:
          type: string
        someOtherData:
          type: boolean

    StringObjectMap:
      type: object
      additionalProperties:
        $ref: "#/definitions/ComplexObject"

我刚刚在这个覆盖深入我的OpenAPI(FKA扬鞭教程)的第4部分 。

该OpenAPI的(FKA。扬鞭)规范简单解释这也 。



文章来源: Swagger HashMap property type