Giter Site home page Giter Site logo

Comments (3)

kamilkloch avatar kamilkloch commented on June 14, 2024 1

Thank you for pointing to https://tapir.softwaremill.com/en/latest/endpoint/schemas.html#wrapper-object-discriminators.

Schema.oneOfWrapped takes an implicit sttp.tapir.generic.Configuration, but (understandably) ignores its .withDiscriminator setting. On the other hand, Schema.derivedSchema does not ignore the .withDiscriminator setting. This means, that the schema configuration logic is split in 2 places: sttp.tapir.generic.Configuration and invocation of either Schema.oneOfWrapped or Schema.derivedSchema.

It looks like the most 2 common semi-automatic schema types (and Json codecs) for coproducts are

  1. wrapper object discriminators, or
  2. field dicriminators.

Arguably, the default tapir schema (no wrapper object discriminators, no field discriminators) does not reflect the default for most common codecs (circe and jsoniter-scala for sure).

Proposition: Schema.oneOfWrapped behavior could be the default behavior for union types (like in circe), changeable to discriminator fields via .withDiscriminator setting in the Configuration. Schema.oneOfWrapped method could be removed altogether, Schema.derivedSchema could be used instead, tapping into the single source of truth via implicit Configuration.

from tapir.

adamw avatar adamw commented on June 14, 2024

As I understand the problem is that there's a mismatch between the generated schema, and the way an object is serialised to JSON. As tapir doesn't know what's your JSON library of configuration, you have to configure it separately (unless you're using pickler).

So in this case, you'll have to provide the schemas by hand, see: https://tapir.softwaremill.com/en/latest/endpoint/schemas.html#wrapper-object-discriminators

from tapir.

adamw avatar adamw commented on June 14, 2024

I'm afraid we can't really change the default strategy for schemas generation for coproducts - even though binary compatible, it would be a breaking change in the core module. Although, I agree that the current situation is not perfect - but I think we'll have to resort to having this properly documented, at least in the 1.x versions.

The situation is different with pickler, where we are free to define the configuration and serialization. That's still very much in the works (cc @kciesielski), so maybe there we can improve basing on your input.

(btw. the defaults on how coproducts are serialised are different for each json library, so it's hard to pick a good candidate ;) but that also would be fixed by pickler, which derives both json codecs & schemas in one go)

from tapir.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.