-
Notifications
You must be signed in to change notification settings - Fork 524
Use strict json encoder in REST API v2 endpoints #2611
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
Use strict json encoder in REST API v2 endpoints #2611
Conversation
|
This is a single-line fix but with a quite large test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposing to use strict json formatting, since the non-strict variation doesn't create a valid json on integer keyed map; but I am a bit concerned that this change would create other issues. Two potential issues -
- Does this change affect only block requests ? If not, it might be an issue.
- What about the msgptool -d ? It being used to decode blocks as well. Do we need to change it as well ? I.e. it make the decompress/recompress non canonical.
|
Based on the difference in |
winder
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing this up again. This would only affect endpoints that have a format option. Since msgpack is available to fetch the canonical data, I think we can ignore any slight differences that this change may cause. These are the affected endpoints:
/v2/transactions/pending
/v2/transactions/pending/{txid}
/v2/blocks/{round}
/v2/accounts/{address}
The TealDryrun command protocol.JSONHandle for reading the request, do you think that should use the strict encoder?
|
What would happen if we would follow the same pattern used in |
|
Thank you for reviewing the PR.
It has
It does not matter (added a test to protocol/codec_test.go) but I changed it to be in sync with rest of the code. Updated the PR and fixed TODO in rest-proof-endpoint test. |
* Fix TODO in rest-proof-endpoint * Fix protocol/codec test * Fund logicsig account in handler test
49d091e to
cee70b3
Compare
This looks overkill, although |
I agree that we need to emit valid jsons here; and for most of the endpoints, it would not be an issue. However, when it comes for some, it does. Two examples:
by fixing it as is now, we're hosing ourselves since it would require us to break backward compatibility in the future. |
|
The caller needs to stick to msgp in these scenarios. Or we can implement "cjson" (canonical json) format later if needed. |
I like the |
|
The |
Codecov Report
@@ Coverage Diff @@
## master #2611 +/- ##
==========================================
+ Coverage 46.97% 47.01% +0.03%
==========================================
Files 348 348
Lines 55716 55723 +7
==========================================
+ Hits 26172 26197 +25
+ Misses 26600 26585 -15
+ Partials 2944 2941 -3
Continue to review full report at Codecov.
|
|
Adding ":b64" or ":int" suffixes to REST API json a bit bizarre for me. Our SDKs have REST API models, can load json data and then serialize them into mspg if needed. We do have models generator for Java and Go, and need to extend it for Python/JS to keep it in sync with REST automatically. |
|
I think I'd like a policy that msgpack is always canonical and json is always best-effort and for convenience where API consumers find json easier than msgpack. |
|
Would this handle correctly a non-utf8 map keys ? v.Map["a\xc5z"] = "def"I think that the root of the issue here is that the objects we're trying to return via the REST API were not designed to be returned using that interface. For the cases that we did design ( i.e. we == @winder ), we have no issues. |
|
Looks reasonable to me. I believe we always use msgpack for the authoritative version of any data (blocks, transactions, account state, etc), when storing or signing things, so the question for this change is just about the clients of this REST API. Seems good to emit valid JSON, if we can deal with existing clients (if any) that are expecting integer map keys. I agree with Pavel that adding |
jasonpaulos
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a good change to me. I remember users have ran into this issue before, and I believe we've told them to use the msgpack response as a workaround. This PR fixes the underlying issue, which is great.
Summary
As #1699 demonstrates, api v2 uses
JSONHandlethat incorrectly encodes maps with numeric fields. Switching toJSONStrictHandlefixes the issue since it was specially created for this purpose.Test Plan
A test added