tapir icon indicating copy to clipboard operation
tapir copied to clipboard

Can we add `SchemaType.Intersection` for `SProduct`s?

Open oyvindberg opened this issue 1 year ago • 1 comments

So let me describe a use case I'm fairly sure is familiar.

I have bunch of data types. Some product types appear both in a sum type and alone. I prefer to use discriminators in sum types.

The way this typically works out is that you get two unrelated types in JSON Schema, where one has a 1 suffix, one of the two have the discriminator field, and they share the entire structure otherwise.

I'd like for this to be rendered as two types in JSON Schema:

  • Foo (without discriminator)
  • FooDisc (with discriminator)

Where FooDisc would be expressed using allOf in JSON Schema. This way the relationship would be clear, there would be less duplication.

I can try a PR myself, just wanted to check if it looks worthwhile first

oyvindberg avatar May 15 '24 21:05 oyvindberg

Interesting - definitely better than Foo and Foo1 schemas. I don't think we can add a new SchemaType implementation, though, as that would break binary compatibility I think? But maybe that's not needed - and it would be enough to add this logic at the tapir schema -> openapi schema translation stage? (in the TapirSchemaToJsonSchema class)

adamw avatar May 16 '24 20:05 adamw