Content-Length: 590327 | pFad | http://github.com/mapstruct/mapstruct/pull/3871/files

63 fix: Ensure NullValuePropertyMappingStrategy.SET_TO_DEFAULT initializes empty collection/map when target is null by tangyang9464 · Pull Request #3871 · mapstruct/mapstruct · GitHub
Skip to content

fix: Ensure NullValuePropertyMappingStrategy.SET_TO_DEFAULT initializes empty collection/map when target is null #3871

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<@lib.handleLocalVarNullCheck needs_explicit_local_var=directAssignment>
${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWrite><#if directAssignment><@wrapLocalVarInCollectionInitializer/><#else><@lib.handleWithAssignmentOrNullCheckVar/></#if></@lib.handleWrite>;
</@lib.handleLocalVarNullCheck>
<#if !ext.defaultValueAssignment?? && !sourcePresenceCheckerReference?? && mapNullToDefault>else {
${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWrite><@lib.initTargetObject/></@lib.handleWrite>;
}
</#if>
</#macro>
<#--
wraps the local variable in a collection initializer (new collection, or EnumSet.copyOf)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3865;

import java.util.List;
import java.util.Map;

/**
* Destination type interface for testing null value property mapping strategy with Map properties.
*/
public interface DestinationType {
Map<String, String> getAttributes();

void setAttributes(Map<String, String> attributes);

Map<String, String> getInitializedAttributes();

void setInitializedAttributes(Map<String, String> attributes);

List<String> getItems();

void setItems(List<String> items);

List<String> getInitializedItems();

void setInitializedItems(List<String> items);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3865;

import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.factory.Mappers;

/**
* Mapper for testing null value property mapping strategy with Map properties.
*/
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT)
public interface Issue3865Mapper {
Issue3865Mapper INSTANCE = Mappers.getMapper( Issue3865Mapper.class );

void update(@MappingTarget DestinationType destination, SourceType source);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3865;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;

import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test for issue 3865: NullValuePropertyMappingStrategy.SET_TO_DEFAULT should set target Map/Collection to default
* when source and target are all null.
*/
@IssueKey("3865")
@WithClasses({
DestinationType.class,
SourceType.class,
Issue3865Mapper.class
})
public class Issue3865Test {

@ProcessorTest
public void shouldSetTargetToDefaultWhenBothSourceAndTargetAreNull() {
DestinationType target = new SourceType();
SourceType source = new SourceType();

assertThat( source.getAttributes() ).isNull();
assertThat( target.getAttributes() ).isNull();
assertThat( source.getItems() ).isNull();
assertThat( target.getItems() ).isNull();

Issue3865Mapper.INSTANCE.update( target, source );

assertThat( target.getAttributes() ).isNotNull();
assertThat( target.getAttributes() ).isEmpty();
assertThat( target.getItems() ).isNotNull();
assertThat( target.getItems() ).isEmpty();

assertThat( target.getInitializedAttributes() ).containsKey( "key1" );
assertThat( target.getInitializedItems() ).contains( "item1" );
}

@ProcessorTest
public void shouldClearTargetWhenSourceIsNullAndTargetIsInitialized() {
DestinationType target = new SourceType();
SourceType source = new SourceType();

Map<String, String> targetAttributes = new HashMap<>();
targetAttributes.put( "targetKey", "targetValue" );
target.setAttributes( targetAttributes );

List<String> targetItems = new ArrayList<>();
targetItems.add( "targetItem" );
target.setItems( targetItems );

assertThat( source.getAttributes() ).isNull();
assertThat( target.getAttributes() ).isNotNull();
assertThat( source.getItems() ).isNull();
assertThat( target.getItems() ).isNotNull();

Issue3865Mapper.INSTANCE.update( target, source );

assertThat( target.getAttributes() ).isNotNull();
assertThat( target.getAttributes() ).isEmpty();
assertThat( target.getItems() ).isNotNull();
assertThat( target.getItems() ).isEmpty();
}

@ProcessorTest
public void shouldCopySourceToTargetWhenSourceIsInitializedAndTargetIsNull() {
DestinationType target = new SourceType();
SourceType source = new SourceType();

Map<String, String> sourceAttributes = new HashMap<>();
sourceAttributes.put( "sourceKey", "sourceValue" );
source.setAttributes( sourceAttributes );

List<String> sourceItems = new ArrayList<>();
sourceItems.add( "sourceItem" );
source.setItems( sourceItems );

assertThat( source.getAttributes() ).isNotNull();
assertThat( target.getAttributes() ).isNull();
assertThat( source.getItems() ).isNotNull();
assertThat( target.getItems() ).isNull();

Issue3865Mapper.INSTANCE.update( target, source );

assertThat( target.getAttributes() ).isNotNull();
assertThat( target.getAttributes() ).containsEntry( "sourceKey", "sourceValue" );
assertThat( target.getItems() ).isNotNull();
assertThat( target.getItems() ).contains( "sourceItem" );
}

@ProcessorTest
public void shouldCopySourceToTargetWhenBothSourceAndTargetAreInitialized() {
DestinationType target = new SourceType();
SourceType source = new SourceType();

Map<String, String> sourceAttributes = new HashMap<>();
sourceAttributes.put( "sourceKey", "sourceValue" );
source.setAttributes( sourceAttributes );

Map<String, String> targetAttributes = new HashMap<>();
targetAttributes.put( "targetKey", "targetValue" );
target.setAttributes( targetAttributes );

List<String> sourceItems = new ArrayList<>();
sourceItems.add( "sourceItem" );
source.setItems( sourceItems );

List<String> targetItems = new ArrayList<>();
targetItems.add( "targetItem" );
target.setItems( targetItems );

assertThat( source.getAttributes() ).isNotNull();
assertThat( target.getAttributes() ).isNotNull();
assertThat( source.getItems() ).isNotNull();
assertThat( target.getItems() ).isNotNull();

Issue3865Mapper.INSTANCE.update( target, source );

assertThat( target.getAttributes() ).isNotNull();
assertThat( target.getAttributes() ).containsEntry( "sourceKey", "sourceValue" );
assertThat( target.getAttributes() ).doesNotContainKey( "targetKey" );

assertThat( target.getItems() ).isNotNull();
assertThat( target.getItems() ).contains( "sourceItem" );
assertThat( target.getItems() ).doesNotContain( "targetItem" );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3865;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Source type class implementing DestinationType for testing null value property mapping strategy with Map properties.
*/
public class SourceType implements DestinationType {
private Map<String, String> attributes;
private List<String> items;

private Map<String, String> initializedAttributes = new HashMap<>();
private List<String> initializedItems = new ArrayList<>();

public SourceType() {
initializedAttributes.put( "key1", "value1" );
initializedItems.add( "item1" );
}

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

@Override
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}

@Override
public Map<String, String> getInitializedAttributes() {
return initializedAttributes;
}

@Override
public void setInitializedAttributes(Map<String, String> attributes) {
this.initializedAttributes = attributes;
}

@Override
public List<String> getItems() {
return items;
}

@Override
public void setItems(List<String> items) {
this.items = items;
}

@Override
public List<String> getInitializedItems() {
return initializedItems;
}

@Override
public void setInitializedItems(List<String> items) {
this.initializedItems = items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public void update(Dto source, Domain target) {
if ( list != null ) {
target.setStrings( new LinkedHashSet<String>( list ) );
}
else {
target.setStrings( new LinkedHashSet<String>() );
}
}
if ( target.getLongs() != null ) {
Set<Long> set = stringListToLongSet( source.getStrings() );
Expand All @@ -83,6 +86,9 @@ public void update(Dto source, Domain target) {
if ( set != null ) {
target.setLongs( set );
}
else {
target.setLongs( new LinkedHashSet<Long>() );
}
}
if ( target.getStringsInitialized() != null ) {
List<String> list1 = source.getStringsInitialized();
Expand All @@ -99,6 +105,9 @@ public void update(Dto source, Domain target) {
if ( list1 != null ) {
target.setStringsInitialized( new LinkedHashSet<String>( list1 ) );
}
else {
target.setStringsInitialized( new LinkedHashSet<String>() );
}
}
if ( target.getLongsInitialized() != null ) {
Set<Long> set1 = stringListToLongSet( source.getStringsInitialized() );
Expand All @@ -115,6 +124,9 @@ public void update(Dto source, Domain target) {
if ( set1 != null ) {
target.setLongsInitialized( set1 );
}
else {
target.setLongsInitialized( new LinkedHashSet<Long>() );
}
}
if ( target.getStringsWithDefault() != null ) {
List<String> list2 = source.getStringsWithDefault();
Expand Down Expand Up @@ -157,6 +169,9 @@ public Domain updateWithReturn(Dto source, Domain target) {
if ( list != null ) {
target.setStrings( new LinkedHashSet<String>( list ) );
}
else {
target.setStrings( new LinkedHashSet<String>() );
}
}
if ( target.getLongs() != null ) {
Set<Long> set = stringListToLongSet( source.getStrings() );
Expand All @@ -173,6 +188,9 @@ public Domain updateWithReturn(Dto source, Domain target) {
if ( set != null ) {
target.setLongs( set );
}
else {
target.setLongs( new LinkedHashSet<Long>() );
}
}
if ( target.getStringsInitialized() != null ) {
List<String> list1 = source.getStringsInitialized();
Expand All @@ -189,6 +207,9 @@ public Domain updateWithReturn(Dto source, Domain target) {
if ( list1 != null ) {
target.setStringsInitialized( new LinkedHashSet<String>( list1 ) );
}
else {
target.setStringsInitialized( new LinkedHashSet<String>() );
}
}
if ( target.getLongsInitialized() != null ) {
Set<Long> set1 = stringListToLongSet( source.getStringsInitialized() );
Expand All @@ -205,6 +226,9 @@ public Domain updateWithReturn(Dto source, Domain target) {
if ( set1 != null ) {
target.setLongsInitialized( set1 );
}
else {
target.setLongsInitialized( new LinkedHashSet<Long>() );
}
}
if ( target.getStringsWithDefault() != null ) {
List<String> list2 = source.getStringsWithDefault();
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/mapstruct/mapstruct/pull/3871/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy