问题
I have a class that one of the properties can be string or array of strings, not sure how should I define it in swagger
@ApiProperty({
description: `to email address`,
type: ???, <- what should be here?
required: true,
})
to: string | Array<string>;
update
As @Youba suggeted answer I tried
@ApiProperty({
description: `to email address(es)`,
additionalProperties: {
oneOf: [
{ type: 'string' },
{ type: 'Array<string>' },
],
},
required: true,
})
and
@ApiProperty({
description: `to email address(es)`,
additionalProperties: {
oneOf: [
{ type: 'string' },
{ type: 'string[]' },
],
},
required: true,
})
and
@ApiProperty({
description: `to email address(es)`,
additionalProperties: {
oneOf: [
{ type: 'string' },
{ type: '[string]' },
],
},
required: true,
})
but the result is like below image, which is not correct
回答1:
Please try
@ApiProperty({
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string'
}
}
]
})
Array<TItem>
can be expressed in OpenAPI with {type: 'array', items: { type: TItem } }
来源:https://stackoverflow.com/questions/64939247/nestjs-swagger-mixed-types