Skip to content

nullValueIterableMappingStrategy in 1.5.0.RC1 not working properly #2843

@marialenak

Description

@marialenak

I've played around with mapstruct and I have come across a bug about initializing arrays. The following is taken from your guide

However, MapStruct also offers a more dedicated way to control how collections / maps should be mapped. e.g. return default (empty) collections / maps, but return null for beans.
For collections (iterables) this can be controlled through:
MapperConfig#nullValueIterableMappingStrategy

so I figured that if nullValueIterableMappingStrategy is set to NullValueMappingStrategy.RETURN_NULL then this piece of code

public class TestVo {

  private String id;
  private List<String> list = new ArrayList<>();
}
public class TestEntity {

  private String id;
  private List<String> list = new ArrayList<>();
}
@Mapper(
    componentModel = "cdi",
    nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_NULL)
@ApplicationScoped
public interface TestMapper {

  TestEntity testVoToTest(TestVo testVo);
}

would conclude to the arraylist if null to initialize with empty collection. Instead this code is generated

@Override
public TestEntity testVoToTest(TestVo testVo) {
    if ( testVo == null ) {
        return null;
    }

    TestEntity.TestEntityBuilder testEntity = TestEntity.builder();

    testEntity.id( testVo.getId() );
    List<String> list = testVo.getList();
    if ( list != null ) {
        testEntity.list( new ArrayList<String>( list ) );
    }

    return testEntity.build();
}

Is this expected? Doesn't the generated code supposed to have an else after the if (list != null)???

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions