Skip to content

Commit 4956876

Browse files
Merge pull request #1056 from hgrsd/swagger-array-polymorphic-type
docs(): add polymorphic array swagger example
2 parents 67d371f + 1e38f89 commit 4956876

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

content/recipes/swagger.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ 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+
343+
```typescript
344+
type Pet = Cat | Dog;
345+
346+
@ApiProperty({
347+
type: 'array',
348+
items: {
349+
oneOf: [
350+
{ $ref: getSchemaPath(Cat) },
351+
{ $ref: getSchemaPath(Dog) },
352+
],
353+
},
354+
})
355+
pets: Pet[];
356+
```
357+
341358
> info **Hint** `getSchemaPath()` function is imported from `@nestjs/swagger`.
342359

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

0 commit comments

Comments
 (0)