Skip to content

Commit 508962b

Browse files
committed
docs(): add polymorphic array swagger example
1 parent 933d756 commit 508962b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

content/recipes/swagger.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,22 @@ In order to combine schemas, you can use `oneOf`, `anyOf` or `allOf` keywords ([
338338
pet: Cat | Dog;
339339
```
340340

341+
If you want to define a polymorphic array (i.e. an array whose members span multiple schemas), you should use a raw definition (see above) to define your type by hand.
342+
```typescript
343+
type Pet = Cat | Dog;
344+
345+
@ApiProperty({
346+
type: 'array',
347+
items: {
348+
oneOf: [
349+
{ $ref: getSchemaPath(Cat) },
350+
{ $ref: getSchemaPath(Dog) },
351+
],
352+
},
353+
});
354+
pets: Pet[];
355+
```
356+
341357
> info **Hint** `getSchemaPath()` function is imported from `@nestjs/swagger`.
342358

343359
Both `Cat` and `Dog` must be defined as extra models using the `@ApiExtraModels()` decorator (at the class-level).

0 commit comments

Comments
 (0)