PushJson/PopJson did not work correctly, if null beans were involved

This commit is contained in:
Roland Praml
2021-08-10 13:24:13 +02:00
parent 7091e972fc
commit 75fa7cb7fd
3 changed files with 99 additions and 2 deletions
@@ -80,11 +80,11 @@ class ScalarTypeJsonObjectMapper {
@Override
public Object read(DataReader reader) throws SQLException {
String json = reader.getString();
// pushJson such that we MD5 and store on EntityBeanIntercept later
reader.pushJson(json);
if (json == null || json.isEmpty()) {
return null;
}
// pushJson such that we MD5 and store on EntityBeanIntercept later
reader.pushJson(json);
try {
return objectReader.readValue(json, deserType);
} catch (IOException e) {
@@ -10,6 +10,7 @@ import io.ebeantest.LoggedSql;
import org.junit.Test;
import org.tests.model.json.EBasicJsonJackson3;
import org.tests.model.json.EBasicJsonList;
import org.tests.model.json.EBasicJsonMulti;
import org.tests.model.json.PlainBean;
import org.tests.model.json.PlainBeanDirtyAware;
@@ -221,6 +222,21 @@ public class TestDbJson_Jackson3 extends BaseTestCase {
LoggedSql.stop();
}
@Test
public void push_pop_test() {
EBasicJsonMulti bean = new EBasicJsonMulti();
bean.setPlainValue2(new PlainBeanDirtyAware("x", 42));
bean.save();
bean = DB.find(EBasicJsonMulti.class, bean.getId());
bean.setPlainValue1(null); // already null
bean.setPlainValue2(null);
bean.setPlainValue3(null); // already null
BeanState state = DB.getBeanState(bean);
assertThat(state.getDirtyValues()).hasSize(1).containsKey("plainValue2");
}
private void expectedSql(int i, String s) {
assertThat(LoggedSql.collect().get(i)).contains(s);
@@ -0,0 +1,81 @@
package org.tests.model.json;
import io.ebean.Model;
import io.ebean.annotation.DbJson;
import io.ebean.annotation.MutationDetection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import static io.ebean.annotation.MutationDetection.NONE;
import static io.ebean.annotation.MutationDetection.SOURCE;
@Entity
public class EBasicJsonMulti extends Model {
@Id
Long id;
String name;
@DbJson(length = 500, mutationDetection = SOURCE)
PlainBeanDirtyAware plainValue1;
@DbJson(length = 500, mutationDetection = SOURCE)
PlainBeanDirtyAware plainValue2;
@DbJson(length = 500, mutationDetection = SOURCE)
PlainBeanDirtyAware plainValue3;
@Version
long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PlainBeanDirtyAware getPlainValue1() {
return plainValue1;
}
public void setPlainValue1(PlainBeanDirtyAware plainValue1) {
this.plainValue1 = plainValue1;
}
public PlainBeanDirtyAware getPlainValue2() {
return plainValue2;
}
public void setPlainValue2(PlainBeanDirtyAware plainValue2) {
this.plainValue2 = plainValue2;
}
public PlainBeanDirtyAware getPlainValue3() {
return plainValue3;
}
public void setPlainValue3(PlainBeanDirtyAware plainValue3) {
this.plainValue3 = plainValue3;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}