softwaremill / tapir

Rapid development of self-documenting APIs
https://tapir.softwaremill.com
Apache License 2.0
1.37k stars 422 forks source link

Respond in valid JSON Array when using streams #4116

Open grzegorz-bielski opened 1 month ago

grzegorz-bielski commented 1 month ago

Hello!

In http4s there is streamJsonArrayEncoderOf which allows you to use fs2.Stream but encode it as a valid JSON Array.

When in one of our services we switched to Tapir for endpoints definitions, we noticed that this functionality is missing, or at the very least we are not sure how to achieve it.

For us, this is very useful, as for short streams, it allows for easier integration with different tools that might not understand NDJSON or other approaches.

As a workaround we roll out our own stream json array encoding, but It'd be nice to have this built-in in Tapir, maybe as an opt-in.

adamw commented 4 weeks ago

This is definitely doable (a matter or providing a way of translating a Stream[F, MyCaseClass] to Stream[F, Byte]), though it would need to reside in a fresh module depending on: circe, fs2 and tapir. A bit specialised ... but maybe worth it?

And I suppose the gist of the integration would be to copy this method: https://github.com/http4s/http4s/blob/50f988442c15193b3495ca695997c14a2dd778b6/circe/src/main/scala/org/http4s/circe/CirceInstances.scala#L291-L315