Skip to content

Non-null iterable is not updated to NULL #3390

Description

@daniel-frak

Expected behavior

Given:

  • Object A contains a non-null iterable property
  • Object B's value of that property is NULL

When:

  • Object A's (@MappingTarget) values are updated using Object B

Then:

  • Object A's property should be NULL

Actual behavior

Currently, iterable properties are not set to NULL if they were not already NULL, when updating them using @MappingTarget.

It seems adding a custom mapping acts as a workaround to this issue:

@Mapping(target="property1", defaultExpression="java( null )")

Steps to reproduce the problem

Here's an example with a failing test:

import lombok.AllArgsConstructor;
import lombok.Data;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.NullValueMappingStrategy;

import java.util.List;

@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
        nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_NULL)
public interface TestMapper {

    void map(@MappingTarget ObjToUpdate objToUpdate, UpdateData updateData);

    @AllArgsConstructor
    @Data
    final class ObjToUpdate {

        private List<String> property1;
    }

    record UpdateData(List<String> property1) {
    }
}
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

class TestMapperTest {

    @Test
    void shouldMapToNull() {
        TestMapper mapper = Mappers.getMapper(TestMapper.class);
        TestMapper.ObjToUpdate objToUpdate = new TestMapper.ObjToUpdate(singletonList("Test value"));
        TestMapper.UpdateData updateData = new TestMapper.UpdateData(null);

        mapper.map(objToUpdate, updateData);

        assertThat(objToUpdate.getProperty1()).isEmpty();
    }
}

It seems to me like this is connected to #2843 but I might be misunderstanding that issue, as I was instructed to post this as a separate issue.

MapStruct Version

1.5.5

Activity

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions