GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Redundant if condition in Java record mapping with RETURN_DEFAULT strategy · Issue #3747 · mapstruct/mapstruct

Redundant if condition in Java record mapping with RETURN_DEFAULT strategy #3747

New issue
Copy link
New issue
Copy link
Closed
#3758
Closed
Redundant if condition in Java record mapping with RETURN_DEFAULT strategy#3747
#3758
Copy link
Assignees
filiphr
Labels
bug
Milestone
1.6.3

Description

@primavistalab
primavistalab
opened on Oct 19, 2024
Issue body actions

Expected behavior

The generated code should avoid unnecessary empty if blocks. It should generate something like these:

    @Override
    public ApiCategoryRecord mapRecord(Category category) {
        Long id = null;
        String title = null;
        if ( category != null ) {
            id = category.getId();
            title = category.getTitle();
        }

        ApiCategoryRecord apiCategoryRecord = new ApiCategoryRecord( id, title );

        return apiCategoryRecord;
    }

OR

    @Override
    public ApiCategoryRecord mapRecord(Category category) {
        if ( category != null ) {
            return new ApiCategoryRecord( category.getId(), category.getTitle() );
        }
        else {
            return new ApiCategoryRecord( null, null );
        }
    }

Actual behavior

MapStruct generates an empty and unnecessary if block after constructing the record:

    @Override
    public ApiCategoryRecord mapRecord(Category category) {
        Long id = null;
        String title = null;
        if ( category != null ) {
            id = category.getId();
            title = category.getTitle();
        }
        ApiCategoryRecord apiCategoryRecord = new ApiCategoryRecord( id, title );
        if ( category != null ) {
        }
        return apiCategoryRecord;
    }

Literally this is not a bug but a minor inefficiency in the generated code.
It does not affect functionality, removing the redundant if condition would improve code clarity and doesn’t suggest anything went wrong during generation.

Steps to reproduce the problem

Use the following setup

public class Category {
    private Long id;
    private String title;
    public Long getId() { return id; }
    public String getTitle() { return title; }
}

public record ApiCategoryRecord(Long id, String title) {}

@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface ICategoryMapper {
    ApiCategoryRecord mapRecord(Category category);
}

MapStruct Version

1.6.2

Activity

  1. primavistalab
    added
    bug
    on Oct 19, 2024
  2. primavistalab commented on Oct 19, 2024

    @primavistalab
    primavistalab
    on Oct 19, 2024
    Author
    More actions

    version: 1.6.2, compiler: javac, environment: Java 21.0.5 (BellSoft)

  3. Hypnagokali
    added a commit that references this issue on Oct 19, 2024

    mapstruct#3747 Fix empty if-block when NullValueMappingStrategy is RE…

    f566ce9
  4. filiphr
    added this to the 1.6.3 milestone on Nov 3, 2024
  5. filiphr
    self-assigned this
    on Nov 3, 2024
  6. filiphr commented on Nov 3, 2024

    @filiphr
    filiphr
    on Nov 3, 2024
    Member
    More actions

    Thanks for reporting this @primavistalab.

    Literally this is not a bug but a minor inefficiency in the generated code.
    It does not affect functionality, removing the redundant if condition would improve code clarity and doesn’t suggest anything went wrong during generation.

    We strive to generate code as it has been written by a human. The fact that we generate

    if ( category != null ) {
    }

    is a bug for us.

    The fix should be relatively easy. We'll look into it

  7. filiphr
    added 2 commits that reference this issue on Nov 3, 2024

    mapstruct#3747 Do not generate redundant if condition with constructo…

    a9aaf8a

    #3747 Do not generate redundant if condition with constructor mapping…

    21fdaa0
  8. filiphr
    closed this as completedin #3758on Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @filiphr
    filiphr

Labels

bug

Type

No type

Projects

No projects

    Milestone

    • 1.6.3

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.