Skip to content

Adder not working together with expression #4145

Open

Description

@msvticket

Expected behaviour

Mapping should work when mapping is configured using annotations like

@Mapper( collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
@Mapping(target = "list", expression = "java(java.util.Arrays.asList( \"test2\" ))")

and the following method exist:

    public void addList(String item) {
        list.add(item);
    }

Actual behaviour

The code for setting list becomes

        if ( targetList.addList() != null ) {
            String string = java.util.Arrays.asList( "test2" );
            if ( string != null ) {
                targetList.addList().putAll( string );
            }
        }

giving an error like:

SourceTargetListMapperImpl.java:20 method addList in class org.mapstruct.ap.test.source.expressions.java.TargetList cannot be applied to given types;
  required: java.lang.String
  found:    no arguments
  reason: actual and formal argument lists differ in length, DiagnosticDescriptor: ERROR SourceTargetListMapperImpl.java:21 incompatible types: no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to java.lang.String, DiagnosticDescriptor: ERROR SourceTargetListMapperImpl.java:23 method addList in class org.mapstruct.ap.test.source.expressions.java.TargetList cannot be applied to given types;
  required: java.lang.String
  found:    no arguments
  reason: actual and formal argument lists differ in lengt

Steps to reproduce the problem

Mapping configuration is given under expected behaviour. I can create an MCVE if needed.

The actual use case is setting a collection in a protobuf object using an expression. This is the reason ADDER_PREFERRED is used, since that is the only way supported by mapstruct to set a repeated field in a protobuf object: https://protobuf.dev/reference/java/java-generated/#repeated

MapStruct Version

Verified in current head

Activity

  1. matheuslealpa commented on Oct 29, 2026

    @matheuslealpa

    I reproduced this on main (211b2be) with a @ProcessorTest: with ADDER_PREFERRED, mapping the list from a source property generates the expected for loop calling addList(item), while @Mapping(target = "list", expression = ...) generates the code from the description and fails to compile.

    constant is affected the same way: @Mapping(target = "list", constant = "c") generates target.addList().putAll( "c" ).

    The cause seems to be in PropertyMapping: JavaExpressionMappingBuilder.build() only distinguishes a setter (or field) from everything else, and treats everything else as a getter, wrapping the assignment in GetterWrapperForCollectionsAndMaps. With ADDER_PREFERRED the write accessor is the adder, and targetType is the element type (String), which explains target.addList() with no argument, String string = <list> and the putAll from the map branch. ConstantMappingBuilder has the same fallback.

    A possible fix is to handle AccessorType.ADDER explicitly in both builders, iterating over the expression and calling the adder for each element, as AdderWrapper does for source properties. For constant I'd add the value as a single element (target.addList( "c" )). Does that match what you'd expect?

    I'd be glad to send a PR with tests for both cases.

  2. msvticket commented on Nov 4, 2026

    @msvticket
    Author

    A possible fix is to handle AccessorType.ADDER explicitly in both builders, iterating over the expression and calling the adder for each element, as AdderWrapper does for source properties. For constant I'd add the value as a single element (target.addList( "c" )). Does that match what you'd expect?

    Yes, I'd say so. This might also solve another issue: when a builder is used to create the target a getter for the property might not exist. Now this make mapstruct fail with an error saying that the property is missing in the target. But fixing that would only be a bonus and not really important to me; I just stumbled upon it when creating test cases for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions