Skip to content

Nested source null check calls method twice instead of using intermediate variable #2245

Description

@filiphr

The following mapper:

@Mapper
public interface TestMapper {
    class Tenant {
        public String id;
    }
    class Inner {
        public String id;
    }

    class TenantDTO {
        public Inner inner;
    }

    @Mapping(target="id", source = "inner.id", defaultValue = "test")
    Tenant map(TenantDTO tenant);

}

We generate the following code:

if ( tenantInnerId( tenant ) != null ) {
    tenant1.id = tenantInnerId( tenant );
}
else {
    tenant1.id = "";
}

We should instead generate:

String id = tenantInnerId( tenant );
if (  id != null ) {
    tenant1.id = id;
}
else {
    tenant1.id = "";
}

Note if we use nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS then the correct code is generated.

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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions