NestJS Swagger:在 ApiProperty 装饰器中描述地图

JWo*_*JWo 5 json swagger typescript nestjs

我在 InfluxDB 前面有一个 NestJS API。在 API 中,我想通过来自 nestjs/swagger 的 ApiProptery 装饰器添加属性描述。
我的问题是我不知道如何为地图创建正确的描述。

这是我的模型:

import { Precision } from '../shared/enums';
import { IsEnum, IsInt, IsOptional } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsPrimitive } from '../shared/decorator/decorators';

export class CreateMeasurementDto {
  @IsOptional()
  @IsInt()
  @ApiPropertyOptional()
  timestamp: number;

  @IsOptional()
  @IsEnum(Precision)
  @ApiPropertyOptional({ enum: Precision })
  precision: Precision;

  @ApiProperty({
    description:
      'Key/value pairs; values can be of type string, boolean or number.',
    type: Map,
  })
  @IsPrimitive()
  datapoints: Map<string, string | boolean | number>;
}
Run Code Online (Sandbox Code Playgroud)

我在 SwaggerUi 架构部分得到的是:

CreateMeasurementDto{
    timestamp   number
    precision   string
                Enum:[ s, ms, u, ns ]
    datapoints* Map {
                }
}
Run Code Online (Sandbox Code Playgroud)

我想至少给出一个例子或描述地图的一个元素。两者都会很棒。
地图允许使用字符串作为键,而值可以是字符串、布尔值或数字。

这是一个可能的有效载荷,可以接受:

{
    "precision": "s",
    "datapoints": {
            "voltage": 123.6456,
            "current": 123
        }
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*ran 6

使用最新版本的nestjs/swagger版本 4,您可以定义原始定义 Swagger 文档

@ApiProperty({
  type: 'object',
  additionalProperties: {
    oneOf: [
      { type: 'string' },
      { type: 'number' },
      { type: 'boolean' }
    ]
  }
})
datapoints: Map<string, string | boolean | number>;
Run Code Online (Sandbox Code Playgroud)