DEV Community

jm9900guy
jm9900guy

Posted on

Decorate Swagger DTO Descriptions for @ApiProperty() Dynamically?

Hello,

I am trying to figure out if there is a way to dynamically add descriptions to a Nest.js DTO @ApiProperty() decorator property.

Take this example for instance:

import { SomeDataFetchingClass } from '../some-dir/SomeDataFetchingClass';

export class SomeDTO extends AnotherClass {
@SomePropertyOne()
@ApiProperty({ description: 'I am a description set manually.' })

@SomePropertyTwo()
@ApiProperty({ description: SomeDataFetchingClass.fetchPropertyTwoDescription() })
}

The above example shows how I am trying to dynamically set the @ApiProperty() description property for @SomePropertyTwo(). I have created a class that fetches data from a remote api then returns a string to this description property. The problem I'm running into is that when the nest.js project runs it's bootstrapping process I cannot seem to get the application to wait for the data fetching class to resolve with the remote data in order to populate the description prop.

My question to the community would be: Has anyone else successfully documented their swagger api documentation dynamically rather than setting each property's description value manually? Any suggestions would be gratefully appreciated.

Thanks

Top comments (1)

Collapse
 
darraghor profile image
Darragh O'Riordan

You could try to do the fetching OUTSIDE of the bootstrapping? I haven't tried this though

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  await app.listen(configuredPort);
}

DO YOUR FETCHING HERE AND SET TO SOME EXPORTED CONST?
export const myDescription = fetch(blah blah)

// eslint-disable-next-line @typescript-eslint/no-floating-promises
bootstrap();
Enter fullscreen mode Exit fullscreen mode