【问题标题】:NestJS Swagger: Describe Map inside ApiProperty DecoratorNestJS Swagger:在 ApiProperty 装饰器中描述地图
【发布时间】:2020-03-30 08:31:48
【问题描述】:

我在 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>;
}

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

CreateMeasurementDto{
    timestamp   number
    precision   string
                Enum:[ s, ms, u, ns ]
    datapoints* Map {
                }
}

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

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

{
    "precision": "s",
    "datapoints": {
            "voltage": 123.6456,
            "current": 123
        }
}

【问题讨论】:

    标签: json typescript swagger nestjs


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 2012-07-10
    • 2013-03-12
    • 2022-09-24
    • 2021-01-30
    • 2021-11-27
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多