Skip to content

Clarify that Mapping#ignoreByDefault is inherited in nested mappings in documentation #3577

@Zegveld

Description

@Zegveld

Expected behavior

given the situation in the reproduction area I would expect the following result, since the mapping @Mapping(source="sub", target="target") is explicitly present.

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2024-04-25T13:10:57+0200",
    comments = "version: 1.6.0-SNAPSHOT, compiler: javac, environment: Java 17.0.4.1 (Azul Systems, Inc.)"
)
class MyMapperImpl implements MyMapper {

    @Override
    public Target map(Source source) {
        if ( source == null ) {
            return null;
        }

        Target target = new Target();

        target.setTarget( subSourceToSubTarget( source.getSub() ) );

        return target;
    }

    protected SubTarget subSourceToSubTarget(SubSource subSource) {
        if ( subSource == null ) {
            return null;
        }

        SubTarget subTarget = new SubTarget();

        subTarget.setChildren( sourceChildListToTargetChildList( subSource.getChildren() ) );

        return subTarget;
    }

    protected TargetChild sourceChildToTargetChild(SourceChild sourceChild) {
        if ( sourceChild == null ) {
            return null;
        }

        TargetChild targetChild = new TargetChild();

        targetChild.setField( sourceChild.getField() );

        return targetChild;
    }

    protected List<TargetChild> sourceChildListToTargetChildList(List<SourceChild> list) {
        if ( list == null ) {
            return null;
        }

        List<TargetChild> list1 = new ArrayList<TargetChild>( list.size() );
        for ( SourceChild sourceChild : list ) {
            list1.add( sourceChildToTargetChild( sourceChild ) );
        }

        return list1;
    }
}

Actual behavior

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2024-04-25T13:09:22+0200",
    comments = "version: 1.6.0-SNAPSHOT, compiler: javac, environment: Java 17.0.4.1 (Azul Systems, Inc.)"
)
class MyMapperImpl implements MyMapper {

    @Override
    public Target map(Source source) {
        if ( source == null ) {
            return null;
        }

        Target target = new Target();

        target.setTarget( subSourceToSubTarget( source.getSub() ) );

        return target;
    }

    protected SubTarget subSourceToSubTarget(SubSource subSource) {
        if ( subSource == null ) {
            return null;
        }

        SubTarget subTarget = new SubTarget();

        return subTarget;
    }
}

Steps to reproduce the problem

// leaving out getters/setters to keep it compact.
class Source {
  SubSource sub;
  // lot of other fields
}

class SubSource {
  List<SourceChild> children;
}

class SourceChild {
  String field;
}

class Target {
  SubTarget target;
}

class SubTarget {
  List<TargetChild> children;
}

class TargetChild {
  String field;
}

@Mapper
interface MyMapper {
  @BeanMapping(ignoreByDefault=true)
  @Mapping(source="sub", target="target")
  Target map(Source source);
}

MapStruct Version

Mapstruct 1.6.0.Beta1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions