You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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.
Activity
matheuslealpa commentedon Oct 29, 2026
I reproduced this on
main(211b2be) with a@ProcessorTest: withADDER_PREFERRED, mapping the list from a source property generates the expectedforloop callingaddList(item), while@Mapping(target = "list", expression = ...)generates the code from the description and fails to compile.constantis affected the same way:@Mapping(target = "list", constant = "c")generatestarget.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 inGetterWrapperForCollectionsAndMaps. WithADDER_PREFERREDthe write accessor is the adder, andtargetTypeis the element type (String), which explainstarget.addList()with no argument,String string = <list>and theputAllfrom the map branch.ConstantMappingBuilderhas the same fallback.A possible fix is to handle
AccessorType.ADDERexplicitly in both builders, iterating over the expression and calling the adder for each element, asAdderWrapperdoes for source properties. ForconstantI'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.
msvticket commentedon Nov 4, 2026
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.