Skip to content

Private collection field with getter gives incorrect compilation error #2952

@andye2004

Description

@andye2004

Erroneous Unmapped target property compilation error when private collection field has a get method but no set method.

Versions: Java 11, Mapstruct 1.5.2-Final.

Given the following classes:

public class Source {
    private String sourceField;

    public Source() {
        // no-op
    }

    public String getSourceField() {
        return sourceField;
    }

    public void setSourceField(String sourceField) {
        this.sourceField = sourceField;
    }
}

public class Target {
    private final Map<String, String> attributes = new HashMap<>();
    private String targetField;

    public Target() {
        // no-op
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public String getTargetField() {
        return targetField;
    }

    public void setTargetField(String targetField) {
        this.targetField = targetField;
    }
}

@Mapper
public interface S2TMapper {
    @Mapping(target = "targetField", source = "sourceField")
    Target map(Source source);
}

When the compiler is run we get the following compilation error.

image

If the getAttributes method is ommitted, everything compiles fine, so it seems Mapstruct mistakenly believes the field should be writable on the basis that it has an accessor.

NOTE: I haven't tested this with other collection type classes.

Reporoducible project here

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions