softwaremill / softwaremill/tapir

JSON codec maps None to empty body, keeps content type

Open
#3,623 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.5k
Forks
468
Avg merge
5h 37m
Merged PRs (30d)
34

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Codec#json() and its use by TapirJsonCirce, then review the linked discussion to understand the competing behavior options. Determine whether optional JSON values should encode as JSON null or be handled as an absent body, and define regression coverage for the chosen behavior, including content type handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
api
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.