zio-http icon indicating copy to clipboard operation
zio-http copied to clipboard

Strange handling of empty json array (possibly zio-schema bug?)

Open russwyte opened this issue 1 year ago • 2 comments

Describe the bug Routes that produce JSON don't correctly handle empty collections. The resulting JSON should always have a field with an empty JSON array - but instead the field is omitted altogether. To Reproduce See this gist result of run is:

unexpected: {"bars":[1,2,3]}
expected: {"bars":[1,2,3]}
unexpected: {}
expected: {"bars":[]}

Additional context This causes breakage in codecs when lists are not optional and are empty

The gist also shows a work-around that might shed light on a possible fix?

russwyte avatar Oct 02 '24 18:10 russwyte

This is the issue. It seems like the default should be false - as empty collections are valid data.

russwyte avatar Oct 02 '24 22:10 russwyte

Adding this works - but this seems like it should be the default as it is in zio-json and elsewhere

  val routes = Routes(
    unexpected.implement(foo => ZIO.succeed(foo)),
    expected.implement(foo => ZIO.succeed(foo))
  ) @@ CodecConfig.withConfig(CodecConfig(ignoreEmptyCollections = false))

russwyte avatar Oct 02 '24 22:10 russwyte

@987Nabil Should we change the default here? I too would expect empty collections to be represented in responses (but missing collections to be accepted in requests -- notice the duality).

jdegoes avatar Nov 09 '24 02:11 jdegoes

Yeah. I think we should

987Nabil avatar Nov 09 '24 02:11 987Nabil

Just got bitten by it. It's indeed quite unexpected.

notxcain avatar Dec 12 '24 15:12 notxcain

/bounty $100

jdegoes avatar Jan 22 '25 16:01 jdegoes

💎 $100 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3186 with your implementation plan
  2. Submit work: Create a pull request including /claim #3186 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

❗ Important guidelines:

  • To claim a bounty, you need to provide a short demo video of your changes in your pull request
  • If anything is unclear, ask for clarification before starting as this will help avoid potential rework
  • Low quality AI PRs will not receive review and will be closed
  • Do not ask to be assigned unless you've contributed before

Thank you for contributing to zio/zio-http!

Attempt Started (UTC) Solution Actions
🟢 @987Nabil May 02, 2025, 08:56:13 AM #3481 Reward
🔴 @Ronyonka Feb 03, 2025, 11:13:14 AM WIP

algora-pbc[bot] avatar Jan 22 '25 16:01 algora-pbc[bot]

/attempt #3186

Options

Ronyonka avatar Feb 03 '25 11:02 Ronyonka

@Ronyonka: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

algora-pbc[bot] avatar Feb 10 '25 11:02 algora-pbc[bot]

🎉 The pull request of @987Nabil has been merged. The bounty can be rewarded here

cc @zio

algora-pbc[bot] avatar May 02 '25 12:05 algora-pbc[bot]

Hey, has this been released? Still drops the empty collection field under zio-http 3.2.0.

Tvaroh avatar May 21 '25 07:05 Tvaroh

🎉🎈 @987Nabil has been awarded $100 by ZIO! 🎈🎊

algora-pbc[bot] avatar May 21 '25 15:05 algora-pbc[bot]