Skip to content

Mapping from java.util.Map<String, ?> to Bean is broken (looks like regression) #3697

@hextriclosan

Description

@hextriclosan

Expected behavior

To map java.util.Map values to bean fields by corresponding String Map-key

Actual behavior

Since v1.6.0 Mapstruct doesn't generate proper code

Steps to reproduce the problem

Sample code

record Entity(long id, String firstName, String lastName) {
}

@Mapper
public interface MyMapper {
    @Mapping(target = "id", source = "id")
    Entity fromMap(long id, Map<String, String> fields);
}

for v1.5.5.Final gives

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2024-09-06T12:01:59+0300",
    comments = "version: 1.5.5.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-8.4.jar, environment: Java 17.0.10 (Amazon.com Inc.)"
)
public class MyMapperImpl implements MyMapper {

    @Override
    public Entity fromMap(long id, Map<String, String> fields) {
        if ( fields == null ) {
            return null;
        }

        String firstName = null;
        String lastName = null;
        if ( fields != null ) {
            if ( fields.containsKey( "firstName" ) ) {
                firstName = fields.get( "firstName" );
            }
            if ( fields.containsKey( "lastName" ) ) {
                lastName = fields.get( "lastName" );
            }
        }
        long id1 = 0L;
        id1 = id;

        Entity entity = new Entity( id1, firstName, lastName );

        return entity;
    }
}

for v1.6.0 gives

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2024-09-06T12:03:23+0300",
    comments = "version: 1.6.0, compiler: IncrementalProcessingEnvironment from gradle-language-java-8.4.jar, environment: Java 17.0.10 (Amazon.com Inc.)"
)
public class MyMapperImpl implements MyMapper {

    @Override
    public Entity fromMap(long id, Map<String, String> fields) {
        if ( fields == null ) {
            return null;
        }

        long id1 = 0L;
        id1 = id;

        String firstName = null;
        String lastName = null;

        Entity entity = new Entity( id1, firstName, lastName );

        return entity;
    }
}

with warning

MyMapper.java:14: warning: Unmapped target properties: "firstName, lastName".
    Entity fromMap(long id, Map<String, String> fields);
           ^
1 warning

MapStruct Version

1.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions