Comments (5)
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.
@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.
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 oneOfVariant
s for a single status code).
from tapir.
Great, I'll make a pr
from tapir.
Awesome :) I suspect the root of the problem is the .headOption
here:
from tapir.
Related Issues (20)
- High latency for netty-loom POST processing HOT 4
- Let serverSecurityLogic access path values HOT 3
- [BUG] Hidden body schema appears in OpenAPI specs
- JSON codec maps None to empty body, keeps content type
- Rename NettyIdServer to NettySyncServer
- Move IntersectionTypeMirror & UnionTypeMirror to the iron integration
- Validation of endpoints against a given OpenAPI schema HOT 4
- [BUG] CORSInterceptor doesn't work with Vert.x server
- [BUG] Tapir Endpoint Incorrectly Merging Set-Cookie Headers in HTTP Response HOT 3
- [BUG] .corsInterceptor throws overloaded method with alternatives error HOT 4
- Web Sockets for netty-server-loom
- [BUG] WebSocket routes always trigger error when using as part of an existing Play Application HOT 1
- tapir server to ignore any validations HOT 11
- [BUG] Websockets with zio-http can fail on too early messages on slow hosts
- [BUG] ClosedChannelException when consuming stream response body with Fs2Streams HOT 1
- Issue with Recognition of API path in Tapir version 10.4.1 HOT 5
- Relaxed enumeratum codecs HOT 2
- [BUG] Can no longer return 304 with empty string as body - get 500 internal server error HOT 4
- [Feature] Semi-auto enum schema derivation configuration
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tapir.