swaggernestjsasyncapi

How to create payload in AsyncApi?


My code works ok, but I need help with my documentation in nestjs-asyncapi and swagger. In my code, I have an object in the array, but in the documentation I have only an object.

enter image description here

This is my nest js decorator:

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

And now I created this part of my async API in Async API studio and everything looks ok, exactly like I want. However, I don't know how I should update the decorator in order to have array<Object>.

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

This is what I want:

enter image description here

Should I provide more information? Should I rewrite somehow the whole decorator or only some parts?


Solution

  • I find out that right now nestjs-asyncapi doesn't know how to do it. So one way is to update the script that will do that.

    Or I choose one other option. I added into my dto class another dto as an array. Both are in the same file.

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

    This is decorator in my code:

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

    And here is result as image:

    enter image description here