且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swagger:<string, Object> 的映射

更新时间:2021-12-14 07:58:47

使用 additionalProperties 是用 OpenAPI (fka. Swagger) 规范描述 hashmap 的正确方法,但 Swagger UI 目前不渲染它们.

Using additionalPropertiesis the proper way to describe hashmap with OpenAPI (fka. Swagger) Specification but Swagger UI do not render them for now.

在此处跟踪问题 https://github.com/swagger-api/swagger-ui/issues/1248

同时你可以使用这个技巧:定义地图对象相同类型的非必需属性(default在下面的示例中)并在描述中给出一些提示:

Meanwhile you can use this trick: define a non required property (defaultin the example below) of the same type of the map's objects and give some hint within the description:

swagger: "2.0"
info:
  version: 1.0.0
  title: Hashmap
  
paths: {}

definitions:
  MapItem:
    properties:
      firstname:
        type: string
      lastname:
        type: string
  Map:
    description: a (key, MapItem) map. `default`is an example key
    properties:
      default:
        $ref: '#/definitions/MapItem'
    additionalProperties:
      $ref: '#/definitions/MapItem'

此描述不会修改 API 合约并改进文档.

This description does not modify API contract and improves documentation.