com-lihaoyi / upickle

uPickle: a simple, fast, dependency-free JSON & Binary (MessagePack) serialization library for Scala
https://com-lihaoyi.github.io/upickle
MIT License
722 stars 165 forks source link

Strictness on keyset match when reading / parsing to case class #537

Closed vreuter closed 10 months ago

vreuter commented 1 year ago

Hello! I have a test case that's failing at the moment, and the behavior suggests that it's because the presence of extra keys (beyond those corresponding to the fields of a case class) is not problematic when attempting to parse JSON to a value of the case class, when the reader in question is in scope by virtue of the case class having been written with a derives declaration.

  1. Is this true?
  2. If so, is there a way to get alternative, stricter parse behavior (i.e., require an exact match between the keyset in the JSON object being parsed, and the fields of the target case class), without writing a custom reader?

I understand the value of having more lax / less strict parsing behavior, particularly in the context of trait hierarchies, but in my case there's no hierarchy and I'd like to opt for stricter behavior if it's not too much additional effort. If it's helpful, I could describe the test case and why it suggests to me that the behavior is as I describe above.

Thanks in advance!

lihaoyi commented 1 year ago

Yeah we ignore unknown keys to allow for graceful schema evolution and backwards compatibility: an upstream component can start adding new keys and the downstream component can begin recognizing them later, without the need for a lockstep update of both components

Adding a flag to make it strict is certainly possible, and there are use cases it would be handy

vreuter commented 1 year ago

Makes total sense, thanks for the reply! Do you prefer I close this, then, or leave open as a small feature for the future?

lihaoyi commented 1 year ago

Let's leave it open as a feature request

lihaoyi commented 10 months ago

@vreuter this fix should have gone out in the latest version 3.1.4

vreuter commented 10 months ago

Awesome, thanks @lihaoyi !