Collection is not shown as required in the Open API Documentation

I have the following case class:

case class ProductToOpen(
    newAccounts: Seq[NewAccount] = Seq(NewAccount()),
    someRequired: Boolean = true,
    ....
)

This creates me the following documentation:


So it works for objects and simple types but not for the collection.

Is this a bug or is there a way to achieve this?

It’s a feature :wink: Although a pretty contentious one. The rationale is that collections might be empty, which makes them optional. However on the other hand, you might want to differentiate between an empty collection and an absent one. So you might say both that a collection which might be empty is “required” (but has to be there), or not (no elements might be provided, making it an optional field).

There’s some more discussion and background here: `asIterable` always creates optional properties · Issue #993 · softwaremill/tapir · GitHub (rationale why this is so and work-arounds are given in this comment: `asIterable` always creates optional properties · Issue #993 · softwaremill/tapir · GitHub)

Thanks for the explanation.

I have the problem that a client that calls the service without the collection receives a decoding failure. So I am not a fan of this feature :wink:.

I used Iron to indicate that there has to be at least one element and this works as expected:

1 Like

Well, I hope the comments on the issue also explain why making an empty collection “required” wouldn’t work well. Maybe that’s simply a shortcoming of Tapir’s design around schemas and codecs - but luckily it’s overridable and there are work-arounds.