Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON codec maps None to empty body, keeps content type #3623

Open
sangamon opened this issue Mar 19, 2024 · 0 comments
Open

JSON codec maps None to empty body, keeps content type #3623

sangamon opened this issue Mar 19, 2024 · 0 comments

Comments

@sangamon
Copy link

Currently, Codec#json(), used by TapirJsonCirce, will interpret an output type of Option[*] as designating an optional body: If the value is None, the response will override the underlying JSON codec's rendering to null and produce an empty body instead, which is no legal JSON, but still declare a content type of application/json.

import sttp.tapir._
import sttp.tapir.json.circe._
val codec = implicitly[Codec[String, Option[String], CodecFormat.Json]]
s"${codec.format.mediaType}: '${codec.encode(None)}'" // application/json: ''

This could either be helped by omitting the Content-type header upon empty body (and perhaps forcing a 204 status code?), or by removing the in-band signaling, i.e. encoding Option values according to the underlying codec and handling the optional body case elsewhere.

Discussion: https://softwaremill.community/t/why-map-none-to-empty-string-in-json-codec/381

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant