#Swagger - How to represent an Array of Cats OR an Array of Dogs

1 messages · Page 1 of 1 (latest)

vernal hound
#

I've seen an example of a polymorphic array in the docs:

@ApiProperty({
  type: 'array',
  items: {
    oneOf: [
      { $ref: getSchemaPath(Cat) },
      { $ref: getSchemaPath(Dog) },
    ],
  },
})
pets: Cat | Dog;

However in my cats I'm not looking for an array of Cats or Dogs but rather an array of Cats OR an array of Dogs. Does anyone know how to do this? I tried:

@ApiProperty({
  oneOf: [
    {
      type: 'array',
      items: {
        $ref: getSchemaPath(Cat)
      }
    },
    {
      type: 'array',
      items: {
        $ref: getSchemaPath(Dog)
      }
    }
  ]
})
pets: Cat[] | Dog[];

But the generated docs just show

{
  pets: [
    "string"
  ]
}

Thanks

hazy pike
#

Are your Cat and Dog classes decorated with swagger decorators?

vernal hound
#

They have the @ApiProperty() annotations on the fields

hazy pike
#

Got a minimum reproduction?

vernal hound
#
class Cat {
  @ApiProperty()
  id: string
}

class Dog {
  @ApiProperty()
  id: string
}

@ApiExtraModels(Cat, Dog)
class Zoo {
  @ApiProperty({
    oneOf: [
      {
        type: 'array',
        items: {
          $ref: getSchemaPath(Cat)
        }
      },
      {
        type: 'array',
        items: {
          $ref: getSchemaPath(Dog)
        }
      }
    ]
  })
  pets: Cat[] | Dog[]
}
hazy pike
#

In a git rep, not just the class. Something I can install and check locally

vernal hound
#

I'll put something together

hazy pike
#

Looks like a problem in displaying a default value. The schema view looks fine

vernal hound
#

I can probably work around that