smithy4s icon indicating copy to clipboard operation
smithy4s copied to clipboard

Json.read ignores extra characters

Open kubukoz opened this issue 10 months ago • 8 comments

Tested with 0.18.29.

import smithy4s.json.Json
val s = "{}aaaaaaaaaaa"
// s: String = {}aaaaaaaaaaa

Json.readDocument(s)
// res0: Either[PayloadError, Document] = Right({})

kubukoz avatar Feb 09 '25 00:02 kubukoz

Not sure this actually qualifies as a bug.

Baccata avatar Feb 11 '25 14:02 Baccata

I think it does, it's not typical for a parser to accept garbage at the end, especially given that there's no nice way to check if anything is left after decoding

kubukoz avatar Feb 11 '25 15:02 kubukoz

Well that's an improvement request then. We have not defined a behaviour, and I'd much prefer it to be generally lenient than overly constrained.

The fact that it's not typical doesn't make it incorrect : it successfully reads a bit of data.

Baccata avatar Feb 11 '25 15:02 Baccata

Can we agree that it breaks the principle of least surprise? Besides, it gives you no indication of any unparsed characters, so to extract that sort of information you'd have to do another pass through the input. I'd be OK with this if we returned a (A, Blob /*leftovers*/) but the current shape is simply a footgun

kubukoz avatar Feb 12 '25 14:02 kubukoz

Can we agree that it breaks the principle of least surprise

I'm not even sure we can agree about that here. The that you bumped into this behaviour is more surprising than the fact it doesn't yell at you. The real question is "how did you bump into it ?"

Baccata avatar Feb 12 '25 16:02 Baccata

The jsoniter-scala parser has a configuration option to check remaining input: https://github.com/plokhotnyuk/jsoniter-scala/blob/1a06f96de85f53964b63000139c5a35b3191f146/jsoniter-scala-core/shared/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/ReaderConfig.scala#L34

plokhotnyuk avatar Feb 12 '25 16:02 plokhotnyuk

Can we agree that it breaks the principle of least surprise

I'm not even sure we can agree about that here. The that you bumped into this behaviour is more surprising than the fact it doesn't yell at you. The real question is "how did you bump into it ?"

By trying to provoke a decoding failure in one of the ways that usually works 😄 by adding text to a textarea that gets piped to a validator.

kubukoz avatar Feb 12 '25 21:02 kubukoz

Anyhow, if you want to enable that option that Andriy pointed to, I don't mind much.

Baccata avatar Feb 13 '25 16:02 Baccata