FIX: do not override empty (json) collections with null

Original: https://github.com/ebean-orm/ebean/commit/5551c35ec2496a9d62ab0afcaecbed739f17aa7c
Signed-off-by: Jonas Pöhler (JPo) <jonas.poehler@foconis.de>
This commit is contained in:
Jonas Pöhler (JPo)
2021-07-29 15:53:44 +02:00
committed by Jonas Pöhler
parent 4aef0ad459
commit 1fad6e910c
2 changed files with 26 additions and 6 deletions
@@ -6,6 +6,9 @@ import io.ebeaninternal.api.SpiQuery.Mode;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.DbReadContext;
import java.util.Collection;
import java.util.Map;
/**
* Controls the loading of property data into a bean.
* <p>
@@ -70,12 +73,7 @@ public class SqlBeanLoad {
try {
Object dbVal = prop.read(ctx);
if (!refreshLoading) {
prop.setValue(bean, dbVal);
} else {
prop.setValueIntercept(bean, dbVal);
}
load(prop, dbVal);
return dbVal;
} catch (Exception e) {
@@ -89,6 +87,14 @@ public class SqlBeanLoad {
* Load the given value into the property.
*/
public void load(BeanProperty target, Object dbVal) {
if (dbVal == null) {
Object current = target.getValue(bean);
if ((current instanceof Collection && ((Collection<?>) current).isEmpty())
|| current instanceof Map && ((Map<?, ?>) current).isEmpty()) {
dbVal = current; // do not modify
}
}
if (!refreshLoading) {
target.setValue(bean, dbVal);
} else {
@@ -221,4 +221,18 @@ public class TestDbJson_List extends BaseTestCase {
DB.delete(bean);
}
@Test
public void testNullToEmpty() {
EBasicJsonList bean = new EBasicJsonList();
bean.setFlags(null);
bean.setTags(null);
bean.setBeanMap(null);
DB.save(bean);
bean = DB.find(EBasicJsonList.class)
.setId(bean.getId()).findOne();
assertThat(bean.getFlags()).isEmpty();
}
}