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

ToJSON/FromJSON instances don't roundtrip correctly when using numeric keys #61

Open
thomasjm opened this issue Dec 7, 2021 · 0 comments

Comments

@thomasjm
Copy link

thomasjm commented Dec 7, 2021

Consider the following:

import Data.Aeson as A
import Data.Aeson.Diff as AD
import Data.Map as M

foo :: Map Int String
foo = mempty

bar :: Map Int String
bar = M.fromList [(123, "asdf")]

encoded = A.encode (AD.diff (A.toJSON foo) (A.toJSON bar))

decoded :: Patch
Right decoded = A.eitherDecode encoded

applied = AD.patch decoded (A.toJSON foo)

In this example, we're computing a diff between two Map Int String values. Note that these still produce string keys when encoded:

λ> putStrLn $ decodeUtf8 $ A.encode bar
{"123":"asdf"}

When we compute the diff and encode it, we get the following:

λ> putStrLn $ decodeUtf8 encoded
[{"op":"add","path":"/123","value":"asdf"}]

This "path" seems to be ambiguous, because when we try to decode it:

λ> decoded
Patch {patchOperations = [Add {changePointer = Pointer {pointerPath = [AKey 123]}, changeValue = String "asdf"}]}

The pointerPath is read in as an AKey rather than the expected OKey. As a result, it fails to apply:

λ> applied
Error "Cannot follow pointer [123]. Expected array but got {}"

From briefly looking over RFC6902, it seems like the right thing to do is actually not to make a distinction between OKey and AKey? It seems like whether a given path component is an "object index" or "array index" should instead depend on which type of JSON object you find during patching. (I.e. if you encounter an array, then try to parse the value as a numeric index, otherwise treat it as a string key for an object.) What do you think?

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