Skip to content

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

@filiphr

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.

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