Repository navigation
feat(http): add node.allowNumericFieldAlias switch for JSON aliases - #7028
Conversation
995777f to
a975a3d
Compare
| // Last try to lookup by field-index if 'name' is numeric, which indicates a possible | ||
| // unknown field. Skipped when numeric field aliases are disabled, so the digit key is | ||
| // then treated as an unknown field. | ||
| if (field == null && DIGITS.matcher(name).matches() |
There was a problem hiding this comment.
[NIT] Skip empty containers when numeric aliases are disabled
With node.allowNumericFieldAlias = false, a digit key reaches handleMissingField. That method enters its object/array do loop before checking for } or ], so inputs such as {"4":[]} throw a parse exception instead of ignoring the unknown key. The empty repeated field is accepted when the switch is on, and the current OFF-mode tests cover only nonempty containers.
Suggestion: Let the unknown-field handler recognize an immediate closing delimiter, and cover empty objects and arrays in the OFF-mode tests.
There was a problem hiding this comment.
Confirmed. With the switch off, a digit key falls through to handleMissingField, whose do/while reads an element before checking for the closing delimiter, so {"4":[]} and {"2":{}} throw instead of being ignored.
This is the existing unknown-field behavior rather than something specific to numeric aliases: {"unrecognized":[]} and {"unrecognized":{}} already throw today in both modes. Letting handleMissingField accept empty containers would also change the default (switch-on) path — those inputs would go from error to success — which is outside this PR's guarantee that the default leaves parsing unchanged.
I'd prefer to keep this PR scoped to the switch and address general empty-container tolerance in handleMissingField as a separate change. Does that sound reasonable?
| @Test | ||
| public void testConfigBindingRespectsExplicitValue() { | ||
| Config off = ConfigFactory.parseString("node.allowNumericFieldAlias = false") | ||
| .withFallback(ConfigFactory.load(TestConstants.TEST_CONF)); |
There was a problem hiding this comment.
[NIT] Documentation & test backlog (1 item rolled up)
Grouped as one comment since this is a test-only request.
- Cover the live config path (
JsonFormatNumericAliasSwitchTest.java:53). The binding test stops atNodeConfig.fromConfig, while OFF-mode parsing tests set theArgssingleton directly. A future omission inArgs.applyNodeConfigwould leave the configured switch ineffective while all these tests pass. Add one test that initializesArgsfrom an explicitfalseconfiguration and then parses a numeric alias throughJsonFormat.merge.
Suggestion: Exercise the config-to-Args-to-parser path in one regression test.
There was a problem hiding this comment.
Good catch. The binding test stopped at NodeConfig.fromConfig, and the parsing tests set the Args flag directly, so the Args.applyNodeConfig assignment itself wasn't covered.
Added testConfigFileDisablesAliasEndToEnd: it boots Args from a config file with node.allowNumericFieldAlias = false (via -c), asserts the resolved flag is false, then confirms through JsonFormat.merge that a numeric alias is ignored while a named field still parses. I verified it fails if the Args.applyNodeConfig assignment is removed, and it does not call setNodeAllowNumericFieldAlias directly.
9e5fc37 to
392177e
Compare
| # HTTP API protobuf input and JSON-RPC embedded ABI JSON. | ||
| # false: numeric keys are handled by the existing unknown-field rules instead. | ||
| # Loaded at startup; restart required after changing this setting. | ||
| allowNumericFieldAlias = true |
There was a problem hiding this comment.
[SHOULD] The comment calls these "field-number aliases", but for message fields the digit key doesn't take the same JSON as the named key (an object is rejected, as testOnObjectAliasRejected pins). Please describe the accepted form accurately, and say that with false such keys are ignored like any other unrecognized key. The JsonFormat comment at L597-599 also uses "unknown field" for both outcomes.
There was a problem hiding this comment.
Agreed. The numeric key selects the same protobuf field, but message values retain the legacy hex-encoded protobuf representation; they do not accept the JSON object form used with field names.
Updated the configuration and parser comments to make that distinction explicit. Disabled or unresolved numeric keys follow the existing unknown-field handling, including its existing parse errors. Parsing behaviour is unchanged.
JsonFormat treats a single-digit JSON key as an alias for the protobuf field
with that number (e.g. {"2": ...} maps to field 2). Add a node-level switch,
node.allowNumericFieldAlias, to control this extension. It is loaded at startup
and defaults to true, so the default JSON parsing behaviour is unchanged.
Operators that do not want the extension can set it to false, after which a
digit key is no longer resolved to a field number and is handled by the
existing unknown-field rules instead.
The switch applies to JSON-to-protobuf parsing in the HTTP API and to embedded
ABI JSON parsing in JSON-RPC. Binary protobuf parsing in gRPC, broadcasthex and
P2P is unaffected.
Also move the GET request setup inside an outer try/finally so the existing
cleanup runs when that setup throws, preserving setup exception propagation and
the handler's existing exception handling.
392177e to
199efbe
Compare
SeriousCoding789
left a comment
There was a problem hiding this comment.
It is recommended to advise in the release notes that nodes exposing APIs to the outside node.allowNumericFieldAlias should be set to false.
What does this PR do?
Adds a node-level switch,
node.allowNumericFieldAlias(defaulttrue), that controls whetherJsonFormattreats a single-digit JSON key as an alias for the protobuf field with that number (for example{"2": ...}maps to field 2). It is loaded at startup; a restart is required after changing it.The default (
true) keeps the current JSON parsing behaviour unchanged. Setting it tofalsedisables the extension: digit keys are no longer resolved to field numbers and fall through to the existing unknown-field handling.For message fields, numeric keys use hex-encoded protobuf data rather than the JSON object representation accepted under field names.
Examples (parsing into
AccountPermissionUpdateContract; field 2 isowner, and withinPermissionfield 4 isthreshold):true(default)false{"2":"2003"}owner.thresholdto 3owneris unset{"owner":{"4":3}}owner.thresholdto 3owner, ignores"4";thresholdremains 0{"owner":{"threshold":3}}owner.thresholdto 3The switch applies to JSON-to-protobuf parsing in the HTTP API and to embedded ABI JSON parsing in JSON-RPC. Binary protobuf parsing in gRPC,
broadcasthexand P2P is unaffected, and it has no effect on consensus validation of already-constructed transactions.Why are these changes required?
Numeric field aliases are a java-tron extension to standard JSON. This adds a configuration option to disable the extension without a code change, while the default preserves full backward compatibility.
This PR has been tested by:
JsonFormatNumericAliasSwitchTest(9): default binding; explicit true/false binding viaNodeConfig.fromConfig; a config-file -> Args -> parser end-to-end check; ON behaviour unchanged; OFF unknown-field handling; named field still parsed while an inner numeric alias is ignored.RateLimiterServletTest(22, including 2 added regressions).UtilTestpass;checkstyleMainpasses.Follow up
None.
Extra details
This branch also moves the per-request GET setup in
RateLimiterServletinside an outertry/finallyso the existing rate-limiter cleanup runs if that setup throws; setup exception propagation and the handler's existing exception handling are preserved.Backward-compatible by default:
truepreserves the existing JsonFormat parsing behaviour; operators opt out by settingnode.allowNumericFieldAliastofalse.