Swagger HashMap属性类型 [英] Swagger HashMap property type

查看:716
本文介绍了Swagger HashMap属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模型部分中是否可以定义HashMap或通用对象类型? 我有一个返回产品的REST服务,这些产品可以有不同的选择. options属性基本上是一个HashMap,其中id是选项名称,其值是选项值.

Is there any way to define a HashMap or Generic Object type in the models section? I have a REST service that returns products and those products can have different options. The options property are basically a HashMap, where the id is the option name and its value is the option value.

推荐答案

是的.

在OpenAPI(fka.Swagger)2.0和3.0中,哈希图始终是<string, something>映射:

In OpenAPI (fka. Swagger) 2.0 and 3.0, a hashmap is always a <string, something> map:

  • 键始终是字符串,不需要定义.
  • 所需的值类型是additionalProperties定义的类型.
  • The key is always a string and do not need to be defined.
  • The value type is what you want and is defined with additionalProperties.

假设您要描述这样的<string, string>哈希图:

Let's say you want to describe a <string, string> hashmap like this one:

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

相应的OpenAPI 2.0定义为:

The corresponding OpenAPI 2.0 definition will be:

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

在OpenAPI 3.0中,定义为:

In OpenAPI 3.0 the definition will be:

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


像这样的<string, object>哈希图


A <string, object> hashmap like this

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

将在OpenAPI 2.0中以这种方式定义:

will be defined this way in OpenAPI 2.0:

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

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

以及在OpenAPI 3.0中:

and in OpenAPI 3.0:

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

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

我刚刚在 OpenAPI(fka .Swagger)规范对此也作了简要说明..

这篇关于Swagger HashMap属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆