Skip to content

feat(http): add node.allowNumericFieldAlias switch for JSON aliases - #7028

Merged
kuny0707 merged 1 commit into
tronprotocol:release_v4.8.3from
waynercheung:feature/numeric_field_alias_switch
Oct 14, 2026
Merged

kuny0707 merged 1 commit into
tronprotocol:release_v4.8.3from
waynercheung:feature/numeric_field_alias_switch

Conversation

@waynercheung

@waynercheung waynercheung commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds a node-level switch, node.allowNumericFieldAlias (default true), that controls whether JsonFormat treats 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 to false disables 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 is owner, and within Permission field 4 is threshold):

Input true (default) false
{"2":"2003"} sets owner.threshold to 3 ignores the numeric key; owner is unset
{"owner":{"4":3}} sets owner.threshold to 3 parses owner, ignores "4"; threshold remains 0
{"owner":{"threshold":3}} sets owner.threshold to 3 same

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, 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:

  • Unit Tests
    • JsonFormatNumericAliasSwitchTest (9): default binding; explicit true/false binding via NodeConfig.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).
    • JsonFormat regression tests and UtilTest pass; checkstyleMain passes.
  • Manual Testing: not performed; behaviour is covered by the unit tests above.

Follow up

None.

Extra details

This branch also moves the per-request GET setup in RateLimiterServlet inside an outer try/finally so 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: true preserves the existing JsonFormat parsing behaviour; operators opt out by setting node.allowNumericFieldAlias to false.

// 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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[NIT] Documentation & test backlog (1 item rolled up)

Grouped as one comment since this is a test-only request.

  1. Cover the live config path (JsonFormatNumericAliasSwitchTest.java:53). The binding test stops at NodeConfig.fromConfig, while OFF-mode parsing tests set the Args singleton directly. A future omission in Args.applyNodeConfig would leave the configured switch ineffective while all these tests pass. Add one test that initializes Args from an explicit false configuration and then parses a numeric alias through JsonFormat.merge.

Suggestion: Exercise the config-to-Args-to-parser path in one regression test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@waynercheung
waynercheung force-pushed the feature/numeric_field_alias_switch branch from 9e5fc37 to 392177e Compare October 13, 2026 10:39
# 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.
@waynercheung
waynercheung force-pushed the feature/numeric_field_alias_switch branch from 392177e to 199efbe Compare October 13, 2026 11:09

@SeriousCoding789 SeriousCoding789 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is recommended to advise in the release notes that nodes exposing APIs to the outside node.allowNumericFieldAlias should be set to false.

@kuny0707
kuny0707 merged commit 5f9b426 into tronprotocol:release_v4.8.3 Oct 14, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants