Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant null checks for nested properties #3245

Closed
cpgerber opened this issue Apr 19, 2023 · 4 comments · Fixed by #3275
Closed

Redundant null checks for nested properties #3245

cpgerber opened this issue Apr 19, 2023 · 4 comments · Fixed by #3275
Labels
enhancement up-for-grabs Issues targeted for community contribution
Milestone

Comments

@cpgerber
Copy link

cpgerber commented Apr 19, 2023

Expected behavior

Consider this simple DTO and Entity classes:

@Data
public class PersonDTO {
    private String name;
    private String parentName;
}
@Data
public class Person {
    private String name;
    private Person parent;
}

And a simple mapper:

@Mapper
public interface PersonMapper {

    @Mapping(target = "parentName", source = "parent.name")
    PersonDTO entityToDTO(Person person);
}

Generated code should be:

public class PersonMapperImpl implements PersonMapper {

    @Override
    public PersonDTO entityToDTO(Person person) {
        if ( person == null ) {
            return null;
        }

        PersonDTO personDTO = new PersonDTO();

        personDTO.setParentName( personParentName( person ) );
        personDTO.setName( person.getName() );

        return personDTO;
    }

    private String personParentName(Person person) {
        Person parent = person.getParent();
        if ( parent == null ) {
            return null;
        }
        return parent.getName();
    }

Actual behavior

Generated code is this:

public class PersonMapperImpl implements PersonMapper {

    @Override
    public PersonDTO entityToDTO(Person person) {
        if ( person == null ) {
            return null;
        }

        PersonDTO personDTO = new PersonDTO();

        personDTO.setParentName( personParentName( person ) );
        personDTO.setName( person.getName() );

        return personDTO;
    }

    private String personParentName(Person person) {
        if ( person == null ) {       // Redundant null check, unreachable return statement
            return null;
        }
        Person parent = person.getParent();
        if ( parent == null ) {
            return null;
        }
        String name = parent.getName();
        if ( name == null ) {      // Redundant null check, just return the parent name
            return null;
        }
        return name;
    }

As marked, two additional redundant null checks were generated. The first one makes it impossible to reach full code coverage (unless using Reflection) and the second is just unnecessary code.

Steps to reproduce the problem

No special configuration needed to reproduce this.

MapStruct Version

MapStruct 1.5.4.Final

@cpgerber cpgerber added the bug label Apr 19, 2023
@filiphr filiphr added enhancement and removed bug labels Apr 22, 2023
@filiphr
Copy link
Member

filiphr commented Apr 22, 2023

I wouldn't treat this as a bug, but rather as a possible enhancement. I do agree with you that we can optimize this code a bit. The past check can indeed be removed (we need to make sure that it works properly with a presence check still.

We can consider this for some future release

@filiphr filiphr added the up-for-grabs Issues targeted for community contribution label Apr 22, 2023
@filiphr filiphr added this to the Future planning milestone Apr 22, 2023
@jccampanero
Copy link
Contributor

Hi @filiphr.

I provided an initial PR that could resolve the redundant nulls checks for nested properties described in the issue.

I tested, mostly "manually" the generated mappers and at first glande they look fine.

I will wait for the results of the tests suites in the repository Github actions to see if the change breaks something.

Please, feel free to make the changes you deem appropriate to the provided code.

@jccampanero
Copy link
Contributor

jccampanero commented May 13, 2023

No way @filiphr, the change breaks every fixture based test that is related to nested properties. I will try to fix them and update the PR.

@jccampanero
Copy link
Contributor

After adapting the fixtures everything seems to be fine again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement up-for-grabs Issues targeted for community contribution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants