Skip to content

fix(codegen): nested arrays and static-array struct fields in generated wrappers - #2335

Open
Saurav1375 wants to merge 1 commit into
LFDT-web3j:mainfrom
Saurav1375:fix/codegen-nested-array-wrappers
Open

Saurav1375 wants to merge 1 commit into
LFDT-web3j:mainfrom
Saurav1375:fix/codegen-nested-array-wrappers

Conversation

@Saurav1375

@Saurav1375 Saurav1375 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

required

Generated contract wrappers declare native Java types: List<List<BigInteger>> for uint256[][], and List<byte[]> for a bytes[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 in core, abi (Array) and codegen.

1. Call results of type uint256[][] hold Uint256, not BigInteger

  • Symptom:
    • List<List<BigInteger>> grid = contract.gridAndCount().send().component1(); compiles.
    • But grid.get(0).get(0) is a Uint256.
    • Using it as a BigInteger throws ClassCastException: class org.web3j.abi.datatypes.generated.Uint256 cannot be cast to class java.math.BigInteger.
  • Root cause: Contract.convertToNative unwraps only the outer list: Contract.java#L928.
  • Fix: nested Array values are converted recursively. Struct elements are still returned as structs.

2. Event fields of type uint256[][] / string[][] have the same problem

  • Symptom: FilledEventResponse.grid is declared List<List<BigInteger>> but holds Uint256 objects.
  • Root cause: generated event code uses Array.getNativeValueCopy(), which converts one level only: Array.java#L71.
  • Fix: getNativeValueCopy() converts nested arrays recursively.

3. Structs with bytes[2] / string[2] fields are generated as StaticStruct

  • Symptom: struct Pair { bytes[2] blobs; uint256 n; } is generated as class Pair extends StaticStruct. The struct is encoded and decoded as if it were static.
  • Root cause: AbiDefinition.NamedType.isDynamic() only treats bytes, string and T[] as dynamic: AbiDefinition.java#L292. Under the ABI spec, T[k] is dynamic when T is dynamic.
  • Fix: the base type of a static array is checked as well.

4. Structs with uint256[][2] / uint256[2][2] fields generate code that does not compile

  • Symptom:
    error: incompatible types: inference variable T has incompatible bounds
        this.groups = groups.getValue().stream().map(v -> v.getValue()).collect(Collectors.toList());
        equality constraints: List<BigInteger>
        lower bounds: List<Uint256>
    
  • Root cause: the generated native struct constructor converts one array level only: SolidityFunctionWrapper.java#L806.
  • Fix:
    • One nested map(...) is generated per array dimension.
    • The output for one-dimensional arrays is unchanged.

5. Code generation crashes for nested arrays once the contract has structs

  • Symptom: generating a wrapper for a contract that declares two structs, one with a uint256[2][2] field, fails with:
    class com.squareup.javapoet.ParameterizedTypeName cannot be cast to class com.squareup.javapoet.ClassName
    
    The same crash happens for an event with a uint256[][] parameter in a contract that has any struct.
  • Root cause: createMappedParameterTypes and isNotArrayOfStructs cast the array's element type to ClassName. For a nested array the element type is a ParameterizedTypeName: SolidityFunctionWrapper.java#L1338, #L2297.
  • Fix: the struct lookup only applies when the element type is a ClassName.

Where should the reviewer start?

required

  1. core/src/main/java/org/web3j/tx/Contract.java (convertToNative) and abi/src/main/java/org/web3j/abi/datatypes/Array.java (getNativeValueCopy): the runtime conversions.
  2. core/src/main/java/org/web3j/protocol/core/methods/response/AbiDefinition.java (NamedType.isDynamic).
  3. codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java:
    • adjustToNativeTypeIfNecessary / nativeArrayElementMapper;
    • createMappedParameterTypes;
    • isNotArrayOfStructs.
  4. Tests:
    • SolidityFunctionWrapperGeneratorTest.testStaticArraysInStructGeneration generates and compiles a wrapper for the new staticarraysinstruct test contract. The contract has three structs with bytes[2], string[2], uint256[][2] and uint256[2][2] fields, plus uint256[][] outputs and event fields. The test also checks DynamicStruct / StaticStruct.
    • ContractTest.testConvertToNativeConvertsNestedArrays
    • DynamicArrayTest.testNativeValueCopyOfNestedArrays
    • AbiDefinitionTest.testNamedTypeIsDynamic

Why is it needed?

required

On main:

  • A wrapper that returns uint256[][] compiles, but fails at runtime as soon as an element is used.
  • Contracts with structs that hold fixed-size arrays of 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:

  • generateWrappers runs web3j codegen (from the checked-out branch) on five test contracts.
  • wrapperChecks deploys 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 generated StructLabels / StructSquare wrappers do not compile.

D1-wrappers-main

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:

D2-wrappers-branch

After (this branch + #2334 merged locally): 10 / 10 pass.

D3-wrappers-with-B

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.

D4-tests-main D5-tests-branch

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 with JAVA_TOOL_OPTIONS=-Dapi.version=1.44 before 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)
  • The generated output for one-dimensional arrays is unchanged: all existing SolidityFunctionWrapperGeneratorTest comparisons against the checked-in expected Java files still pass.

Checklist

  • I've read the contribution guidelines.
  • I've added tests (if applicable).
  • I've added a changelog entry if necessary.

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>

This branch has not been deployed

No deployments
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.

1 participant