Skip to content
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
10 changes: 10 additions & 0 deletions src/main/java/io/beanmapper/config/BeanMapperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ public BeanMapperBuilder setUseNullValue() {
return this;
}

public BeanMapperBuilder setUseNullValue(boolean useNullValue) {
this.configuration.setUseNullValue(useNullValue);
return this;
}

public BeanMapperBuilder setUseCollectionNullValue(boolean useCollectionNullValue) {
this.configuration.setUseCollectionNullValue(useCollectionNullValue);
return this;
}

public BeanMapper build() {
BeanMapper beanMapper = new BeanMapper(configuration);
// Custom collection handlers must be registered before default ones
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/beanmapper/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public interface Configuration {
*/
Boolean getUseNullValue();

/**
* Property that determines if null values for the source collection must be skipped or not
* @return determines if null values must be skipped or not
*/
Boolean getUseCollectionNullValue();

/**
* The RoleSecuredCheck is responsible for checking if a Principal may access
* a field or method annotated with @BeanRoleSecured. Returns the RoleSecuredCheck,
Expand Down Expand Up @@ -419,4 +425,9 @@ public interface Configuration {
*/
void setUseNullValue(Boolean useNullValue);

/**
* Property that determines if null values for the source collections must be skipped or not
* @param useNullValue determines if null values must be skipped or not
*/
void setUseCollectionNullValue(Boolean useNullValue);
}
15 changes: 15 additions & 0 deletions src/main/java/io/beanmapper/config/CoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public class CoreConfiguration implements Configuration {
*/
private Boolean useNullValue = false;

/**
* Property that determines if null collections should be mapped to an empty collection or a null value. Normal
* behaviour is to create an empty list if a source value is null.
*/
private Boolean useCollectionNullValues = true;

@Override
public List<String> getDownsizeTarget() { return null; }

Expand Down Expand Up @@ -244,6 +250,11 @@ public Boolean getUseNullValue() {
return this.useNullValue;
}

@Override
public Boolean getUseCollectionNullValue() {
return this.useCollectionNullValues;
}

@Override
public RoleSecuredCheck getRoleSecuredCheck() {
return this.roleSecuredCheck;
Expand Down Expand Up @@ -414,4 +425,8 @@ public void setUseNullValue(Boolean useNullValue) {
this.useNullValue = useNullValue;
}

@Override
public void setUseCollectionNullValue(Boolean useNullValue) {
this.useCollectionNullValues = useNullValue;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/config/OverrideConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class OverrideConfiguration implements Configuration {

private OverrideField<Boolean> useNullValue;

private OverrideField<Boolean> useCollectionNullValue;

private OverrideField<Boolean> flushAfterClear;

private OverrideField<Boolean> flushEnabled;
Expand All @@ -64,6 +66,7 @@ public OverrideConfiguration(Configuration configuration) {
this.flushAfterClear = new OverrideField<>(configuration::isFlushAfterClear);
this.flushEnabled = new OverrideField<>(configuration::isFlushEnabled);
this.useNullValue = new OverrideField<>(configuration::getUseNullValue);
this.useCollectionNullValue = new OverrideField<>(configuration::getUseCollectionNullValue);
}

@Override
Expand Down Expand Up @@ -235,6 +238,11 @@ public Boolean getUseNullValue() {
return useNullValue.get();
}

@Override
public Boolean getUseCollectionNullValue() {
return useCollectionNullValue.get();
}

@Override
public RoleSecuredCheck getRoleSecuredCheck() {
return parentConfiguration.getRoleSecuredCheck();
Expand Down Expand Up @@ -397,4 +405,9 @@ public void setUseNullValue(Boolean useNullValue) {
this.useNullValue.set(useNullValue);
}

@Override
public void setUseCollectionNullValue(Boolean useNullValue) {
this.useCollectionNullValue.set(useNullValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public T convert(
.setFlushAfterClear(beanPropertyMatch.getCollectionInstructions().getFlushAfterClear())
.setTargetClass(beanPropertyMatch.getCollectionInstructions().getCollectionElementType().getType())
.setTarget(beanPropertyMatch.getTargetObject())
.setUseNullValue()
.setUseNullValue(beanMapper.getConfiguration().getUseCollectionNullValue())
.build()
.map(sourceCollection);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/io/beanmapper/BeanMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,17 @@ public void emptyListToExistingList() {
assertEquals(targetItems, target.getItems());
}

@Test
public void nullListToNullList() {
BeanMapper beanMapper = new BeanMapperBuilder()
.setUseCollectionNullValue(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

als useCollectionNullValue true is moet volgens mij de value null zijn. Dus als het false is juist niet.
Volgens mij werkt het nu precies verkeerd om...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default gebruikt useNullValue true en worden null waarden daarom niet genegeerd. Ik neem aan dat dit op dezelfde manier werkt, klopt dat of denk ik verkeerd?

.build();
CollectionListSource source = new CollectionListSource();
source.items = null;
CollectionListTarget target = beanMapper.map(source, CollectionListTarget.class);
assertNull(target.items);
}

@Test
public void useBeanPropertyPathToAccessGetterOnly() {
SourceWithPerson source = new SourceWithPerson();
Expand Down