Generate simple schema using Swagger in NestJS
lunarEclipse423 opened this issue · comments
Is there an existing issue that is already proposing this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe it
According to docs, in NestJS it is possible to generate schemas like in the first picture using this code:
export class NodeId {
@ApiProperty({
minimum: 40,
maximum: 40,
readOnly: true,
})
id: string;
}
export class Position {
@ApiProperty({ readOnly: true, example: 0 })
x: number;
@ApiProperty({ readOnly: true, example: 0 })
y: number;
}
export class Node {
@ApiProperty()
nodeId: NodeId;
@ApiProperty()
position: Position;
}
In yaml NodeId schema looks like this:
NodeId:
type: object
properties:
id:
type: string
minimum: 40
maximum: 40
readOnly: true
required:
- id
Though, my ultimate goal is being able to produce simple schema for entities like Ids, which has to have a specific structure and could be reusable like in the second picture.
This can be achieved in swagger itself (by editing yaml file manually) like this:
NodeId:
type: string
minimum: 40
maximum: 40
readOnly: true
I'm seeking for the same in NestJS. Sadly, there was no such example in NestJS docs. I'm wondering whether it is possible to generate this kind of schema by swagger module means.
Describe the solution you'd like
Probably, implementing some kind of decorator for that specific case
Teachability, documentation, adoption, migration strategy
No response
What is the motivation / use case for changing the behavior?
There is no clear way for creating simple schemas in swagger module right now
I already told you on Discord, you shouldn't use a class for the nodeId
as that would make the nested values in the schemea. Instead, a simple wrapper decorator could be used here for any nodeId
. Something like:
export const ApiNodeId = () => ApiProperty({
minimum: 40,
maximum: 40,
readOnly: true,
});
And now it can be used as @ApiNodeId()
to document your property