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

Fix the FromJSON instance for Payload #54

Merged
merged 1 commit into from
Dec 18, 2023
Merged

Conversation

enobayram
Copy link
Contributor

This PR fixes a regression introduced by #52. The rewrite of the FromJSON instance in that PR changed the behavior when the exec field doesn't exist.

@enobayram enobayram self-assigned this Dec 18, 2023
@enobayram
Copy link
Contributor Author

Before #52

ghci> :set -XOverloadedStrings 
ghci> :set -XTypeApplications 
ghci> import Chainweb.Api.Payload
ghci> import Data.Aeson
ghci> eitherDecode @Payload "{}"
Left "Error in $: Payload must be exec or cont"
ghci> eitherDecode @Payload "{ \"exec\": null}"
Left "Error in $: Payload must be exec or cont"
ghci> eitherDecode @Payload "{ \"exec\": {\"code\": \"abc\" } }"
Left "Error in $: key \"data\" not found"
ghci> eitherDecode @Payload "{ \"exec\": {\"code\": \"abc\", \"data\":null } }"
Right (ExecPayload (Exec {_exec_code = "abc", _exec_data = Nothing}))
ghci> eitherDecode @Payload "{ \"cont\": { \"pactId\": \"abc\", \"rollback\": true, \"step\":42, \"data\": [] } }" 
Left "Error in $: key \"proof\" not found"
ghci> eitherDecode @Payload "{ \"cont\": { \"pactId\": \"abc\", \"rollback\": true, \"step\":42, \"data\": [], \"proof\":null } }"
Right (ContPayload (Cont {_cont_pactId = "abc", _cont_rollback = True, _cont_step = 42, _cont_data = Array [], _cont_proof = Nothing}))
ghci> 

After #52

ghci> eitherDecode @Payload "{}"
Left "Error in $: key \"exec\" not found"
ghci> eitherDecode @Payload "{ \"exec\": null}"
Left "Error in $: key \"cont\" not found"
ghci> eitherDecode @Payload "{ \"exec\": {\"code\": \"abc\" } }"
Left "Error in $.exec: key \"data\" not found"
ghci> eitherDecode @Payload "{ \"exec\": {\"code\": \"abc\", \"data\":null } }"
Right (ExecPayload (Exec {_exec_code = "abc", _exec_data = Nothing}))
ghci> eitherDecode @Payload "{ \"cont\": { \"pactId\": \"abc\", \"rollback\": true, \"step\":42, \"data\": [] } }" 
Left "Error in $: key \"exec\" not found"
ghci> eitherDecode @Payload "{ \"cont\": { \"pactId\": \"abc\", \"rollback\": true, \"step\":42, \"data\": [], \"proof\":null } }"
Left "Error in $: key \"exec\" not found"

Fixed with this PR:

ghci> eitherDecode @Payload "{}"
Left "Error in $: Payload must be exec or cont"
ghci> eitherDecode @Payload "{ \"exec\": null}"
Left "Error in $: Payload must be exec or cont"
ghci> eitherDecode @Payload "{ \"exec\": {\"code\": \"abc\" } }"
Left "Error in $.exec: key \"data\" not found"
ghci> eitherDecode @Payload "{ \"exec\": {\"code\": \"abc\", \"data\":null } }"
Right (ExecPayload (Exec {_exec_code = "abc", _exec_data = Nothing}))
ghci> eitherDecode @Payload "{ \"cont\": { \"pactId\": \"abc\", \"rollback\": true, \"step\":42, \"data\": [] } }"
Left "Error in $.cont: key \"proof\" not found"
ghci> eitherDecode @Payload "{ \"cont\": { \"pactId\": \"abc\", \"rollback\": true, \"step\":42, \"data\": [], \"proof\":null } }"
Right (ContPayload (Cont {_cont_pactId = "abc", _cont_rollback = True, _cont_step = 42, _cont_data = Array [], _cont_proof = Nothing}))

Copy link
Contributor

@emmanueldenloye emmanueldenloye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@enobayram enobayram merged commit 33022a1 into master Dec 18, 2023
6 checks passed
@enobayram enobayram deleted the enis/fix-payload-fromjson branch December 18, 2023 22:27
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

Successfully merging this pull request may close these issues.

2 participants