swagger 如何在AsyncApi中创建有效负载?

6za6bjd0  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(96)

我的代码工作正常,但我需要帮助我的文档在nestjs-asyncapiswagger。在我的代码中,数组中有一个对象,但在文档中我只有一个对象。


的数据
这是我的nest js decorator:

@AsyncApiSub({
    channel: 'myChannel',
    description: 'Read all features - result',
    message: {
        name: 'getFeaturesResponse',
        payload: {
            type: () => Feature,
        },
        headers: AsyncApiHeaders.subHeaders(),
    },
})

字符串
现在我在Async API Studio中创建了我的异步API的这一部分,一切看起来都很好,完全符合我的要求。但是我不知道我应该如何更新装饰器才能有数组

"Feature": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "idFeature": {
          "type": "string",
          "description": "unique id of Feature",
          "example": "example"
        },
      },
    },
    "required": [
      "idFeature",
      "name",
      "description"
    ]
  },


这就是我想要的



我应该提供更多信息吗?我应该重写整个装饰器还是只重写一部分?谢谢你的帮助。

ffscu2ro

ffscu2ro1#

我发现现在nestjs-asyncapi不知道怎么做。因此,一种方法是更新将执行此操作的脚本。
或者我选择另一个选择。我在我的dto类中添加了另一个dto作为数组。两者都在同一个文件中。

export class Feature {
    @ApiProperty({
        type: 'string',
        description: 'unique id of Feature',
        example: 'a5f3252b-1007-449d-a75d-e99ecf88bc06',
    })
    @IsNotEmpty()
    @IsString()
    idFeature: string;

    constructor(
        idFeature: string,
    ) {
        this.idFeature = idFeature;
    }
}

export class FeatureArray {
    @ApiPropertyOptional({
        type: Feature,
        isArray: true,
        description: 'settings of Feature',
    })
    @ValidateNested({ each: true })
    @Type(() => Feature)
    @IsArray()
    featureArray: Feature[];

    constructor(
        feature: Feature[],
    ) {
        this.featureArray = feature;
    }
}

字符串
这是我代码中的装饰器:

@AsyncApiSub({
        channel: 'myChannel',
        description: '',
        message: {
            name: 'Name',
            payload: {
                type: () => FeatureArray,
            },
            headers: AsyncApiHeaders.subHeaders(),
        },
    })


这里是结果作为图像:


的数据

相关问题