Repository navigation
fix(codegen): nested arrays and static-array struct fields in generated wrappers - #2335
Open
Saurav1375 wants to merge 1 commit into
Open
Saurav1375 wants to merge 1 commit into
Saurav1375 wants to merge 1 commit into
Conversation
Saurav1375
requested review from
NickSneo,
conor10 and
gtebrean
as code owners
November 12, 2026 14:47
Generated wrappers declare native Java types such as List<List<BigInteger>> for uint256[][], but several code paths only converted one array level or mis-handled static arrays of dynamic types. - Contract.convertToNative and Array.getNativeValueCopy convert nested arrays at every level, so call results and event fields of type uint256[][] hold BigInteger values instead of Uint256 objects (which failed with a ClassCastException when used). - AbiDefinition.NamedType.isDynamic treats T[k] as dynamic when T is (bytes[2], string[2]), so structs with such fields are generated as DynamicStruct instead of StaticStruct. - The native struct constructor converts nested-array fields (uint256[][2], uint256[2][2]) at every level; the generated code previously did not compile. - Code generation no longer throws a ClassCastException for nested-array struct fields or event parameters when the contract also declares structs. Signed-off-by: Saurav1375 <sauravgupta1375@gmail.com>
Saurav1375
force-pushed
the
fix/codegen-nested-array-wrappers
branch
from
November 12, 2026 14:47
a5f5aea to
261a505
Compare
3 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
required
Generated contract wrappers declare native Java types:
List<List<BigInteger>>foruint256[][], andList<byte[]>for abytes[2]struct field. Several code paths only handled one array level, or treated static arrays of dynamic types as static. This PR fixes five such issues incore,abi(Array) andcodegen.1. Call results of type
uint256[][]holdUint256, notBigIntegerList<List<BigInteger>> grid = contract.gridAndCount().send().component1();compiles.grid.get(0).get(0)is aUint256.BigIntegerthrowsClassCastException: class org.web3j.abi.datatypes.generated.Uint256 cannot be cast to class java.math.BigInteger.Contract.convertToNativeunwraps only the outer list:Contract.java#L928.Arrayvalues are converted recursively. Struct elements are still returned as structs.2. Event fields of type
uint256[][]/string[][]have the same problemFilledEventResponse.gridis declaredList<List<BigInteger>>but holdsUint256objects.Array.getNativeValueCopy(), which converts one level only:Array.java#L71.getNativeValueCopy()converts nested arrays recursively.3. Structs with
bytes[2]/string[2]fields are generated asStaticStructstruct Pair { bytes[2] blobs; uint256 n; }is generated asclass Pair extends StaticStruct. The struct is encoded and decoded as if it were static.AbiDefinition.NamedType.isDynamic()only treatsbytes,stringandT[]as dynamic:AbiDefinition.java#L292. Under the ABI spec,T[k]is dynamic whenTis dynamic.4. Structs with
uint256[][2]/uint256[2][2]fields generate code that does not compileSolidityFunctionWrapper.java#L806.map(...)is generated per array dimension.5. Code generation crashes for nested arrays once the contract has structs
uint256[2][2]field, fails with:uint256[][]parameter in a contract that has any struct.createMappedParameterTypesandisNotArrayOfStructscast the array's element type toClassName. For a nested array the element type is aParameterizedTypeName:SolidityFunctionWrapper.java#L1338,#L2297.ClassName.Where should the reviewer start?
required
core/src/main/java/org/web3j/tx/Contract.java(convertToNative) andabi/src/main/java/org/web3j/abi/datatypes/Array.java(getNativeValueCopy): the runtime conversions.core/src/main/java/org/web3j/protocol/core/methods/response/AbiDefinition.java(NamedType.isDynamic).codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java:adjustToNativeTypeIfNecessary/nativeArrayElementMapper;createMappedParameterTypes;isNotArrayOfStructs.SolidityFunctionWrapperGeneratorTest.testStaticArraysInStructGenerationgenerates and compiles a wrapper for the newstaticarraysinstructtest contract. The contract has three structs withbytes[2],string[2],uint256[][2]anduint256[2][2]fields, plusuint256[][]outputs and event fields. The test also checksDynamicStruct/StaticStruct.ContractTest.testConvertToNativeConvertsNestedArraysDynamicArrayTest.testNativeValueCopyOfNestedArraysAbiDefinitionTest.testNamedTypeIsDynamicWhy is it needed?
required
On
main:uint256[][]compiles, but fails at runtime as soon as an element is used.bytes/string/ arrays either crash code generation or produce wrappers that don't compile.Relation to other PRs
Before / after
A small sample project (not part of this PR) depends on the local web3j checkout through a Gradle composite build (
includeBuild '../web3j'). It has two tasks:generateWrappersruns web3j codegen (from the checked-out branch) on five test contracts.wrapperChecksdeploys them to a local anvil chain and uses the generated wrappers: nested arrays in calls and events, and struct inputs, outputs and events.Before (
main): code generation crashes for the two-struct contract, and the generatedStructLabels/StructSquarewrappers do not compile.After (this branch): everything generates and compiles. Nested arrays in calls and events return
BigInteger/String, and struct inputs encode correctly. Struct outputs and events still fail on this branch alone, because they need the decoder fix in #2334:After (this branch + #2334 merged locally): 10 / 10 pass.
New unit tests against
main's code: 3 tests fail, and the codegen suite cannot even start, because generating the new test contract's wrapper crashes. All pass on this branch. The init script only prints one line per test result.Test results
./gradlew spotlessCheck: clean./gradlew check(all modules, without integration tests): 1186 tests, 0 failures, 3 skipped (4 new tests)./gradlew -Pintegration-tests=true :integration-tests:test: 87 tests, 0 failures, 18 skipped (run locally withJAVA_TOOL_OPTIONS=-Dapi.version=1.44before the Docker 29 fix in build: run integration tests on Docker 29 and fix -Pintegration-tests=false #2327 was merged; CI now runs them without it)SolidityFunctionWrapperGeneratorTestcomparisons against the checked-in expected Java files still pass.Checklist