Giter Site home page Giter Site logo

Comments (5)

adamw avatar adamw commented on June 3, 2024

Sorry for the late reply :)

In OpenAPI docs, we can only have a single output per status code. So while this will work server-side, the documentation will be inprecise.

I suspect that what you're after is having the body represented as a hierarchy. You can also then specify the discrimination strategy that you're using. So you'd need something as follows:

sealed trait A 
sealed trait Success extends A derives Schema, Encoder.AsObject, Decoder
case class B(b: String) extends Success 
case class C(c: String) extends Success
case object D extends A derives Schema, Encoder.AsObject, Decoder

@main
def tapirOneOf =
  val myEndpoint = endpoint.get
    .in("path")
    .out(oneOf[A](
      oneOfVariant(statusCode(StatusCode.Ok).and(jsonBody[Success])),
      oneOfVariant(statusCode(StatusCode.NoContent).and(jsonBody[D.type])),
    ))

Relevant documentation sections:
https://tapir.softwaremill.com/en/latest/endpoint/oneof.html
https://tapir.softwaremill.com/en/latest/endpoint/schemas.html#sealed-traits-coproducts

from tapir.

susliko avatar susliko commented on June 3, 2024

@adamw Thanks for the reply :)

What I meant here is that the schema, which is generated for the case with multiple oneOfVariant for the same status code, is already imprecise: it lacks the 200 B case, while the code compiles and runs well (here's a runnable gist). And it was an unexpected behavior for me (I expected oneOf schema with variants B and C to be generated for the 200 application/json response).

I do realize that tailoring response type hierarchies to the status codes solves this, but it's a constraint which is not tracked in compile time and hence is easy to violate.

from tapir.

adamw avatar adamw commented on June 3, 2024

Yes, I can see that it might be problematic. I suppose we could create an inline oneOf or anyOf schema for such cases (where there are multiple oneOfVariants for a single status code).

from tapir.

susliko avatar susliko commented on June 3, 2024

Great, I'll make a pr

from tapir.

adamw avatar adamw commented on June 3, 2024

Awesome :) I suspect the root of the problem is the .headOption here:

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.