Skip to content

Commit

Permalink
#2278 - Move null to empty json collections check to BeanPropertyJson…
Browse files Browse the repository at this point in the history
…Basic
  • Loading branch information
rbygrave committed Jul 30, 2021
1 parent 443e826 commit d601a88
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -625,27 +625,13 @@ public Object read(DataReader reader) throws SQLException {
return scalarType.read(reader);
}

protected Object checkForEmpty(EntityBean bean) {
final Object value = getValue(bean);
if (value instanceof Collection && ((Collection<?>) value).isEmpty()
|| value instanceof Map && ((Map<?, ?>) value).isEmpty()) {
return value;
}
return null;
}

public Object readSet(DataReader reader, EntityBean bean) throws SQLException {
try {
Object value = scalarType.read(reader);
if (value == null) {
value = checkForEmpty(bean);
}
if (bean != null) {
setValue(bean, value);
}
return value;
} catch (TextException e) {
throw e;
} catch (Exception e) {
throw new PersistenceException("Error readSet on " + descriptor + "." + name, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.ebeaninternal.server.deploy;

import io.ebean.bean.EntityBean;
import io.ebean.core.type.DataReader;
import io.ebean.text.TextException;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;

import javax.persistence.PersistenceException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;

/**
* A DbJson property that does not use Jackson ObjectMapper.
*/
public class BeanPropertyJsonBasic extends BeanProperty {

public BeanPropertyJsonBasic(BeanDescriptor<?> descriptor, DeployBeanProperty deploy) {
super(descriptor, deploy);
}

protected BeanPropertyJsonBasic(BeanProperty source, BeanPropertyOverride override) {
super(source, override);
}

@Override
public BeanProperty override(BeanPropertyOverride override) {
return new BeanPropertyJsonBasic(this, override);
}

protected Object checkForEmpty(EntityBean bean) {
final Object value = getValue(bean);
if (value instanceof Collection && ((Collection<?>) value).isEmpty()
|| value instanceof Map && ((Map<?, ?>) value).isEmpty()) {
return value;
}
return null;
}

@Override
public Object readSet(DataReader reader, EntityBean bean) throws SQLException {
try {
Object value = scalarType.read(reader);
if (value == null) {
value = checkForEmpty(bean);
}
if (bean != null) {
setValue(bean, value);
}
return value;
} catch (TextException e) {
throw e;
} catch (Exception e) {
throw new PersistenceException("Error readSet on " + descriptor + "." + name, e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Handle json property with MutationDetection of SOURCE or HASH only.
*/
public class BeanPropertyJsonMapper extends BeanProperty {
public class BeanPropertyJsonMapper extends BeanPropertyJsonBasic {

private final boolean sourceDetection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ public void setJsonDeserialize(boolean jsonDeserialize) {
}

public MutationDetection getMutationDetection() {
if (mutationDetection == null) {
mutationDetection = MutationDetection.DEFAULT;
}
return mutationDetection;
}

Expand Down Expand Up @@ -1204,4 +1201,7 @@ boolean isJsonMapper() {
return scalarType != null && scalarType.isJsonMapper();
}

boolean isJsonType() {
return mutationDetection != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ private BeanProperty createBeanProperty(BeanDescriptorMap owner, DeployBeanPrope
if (deployProp.isJsonMapper()) {
return new BeanPropertyJsonMapper(desc, deployProp);
}
if (deployProp.isJsonType()) {
return new BeanPropertyJsonBasic(desc, deployProp);
}
return new BeanProperty(desc, deployProp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,6 @@ void setDbArray(DeployBeanProperty prop, DbArray dbArray) {
}
}

/**
* This property is marked as a Lob object.
*/
void setDbJsonType(DeployBeanProperty prop, DbJson dbJsonType) {
int dbType = getDbJsonStorage(dbJsonType.storage());
setDbJsonType(prop, dbType, dbJsonType.length(), dbJsonType.mutationDetection());
Expand Down
12 changes: 12 additions & 0 deletions ebean-core/src/test/java/org/tests/model/embedded/EAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.util.Map;

@Embeddable
public class EAddress {
Expand All @@ -24,6 +25,9 @@ public class EAddress {
@DbJson
PlainBean jbean;

@DbJson
Map<String, Object> jraw;

public String getStreet() {
return street;
}
Expand Down Expand Up @@ -56,6 +60,14 @@ public void setJbean(PlainBean jbean) {
this.jbean = jbean;
}

public Map<String, Object> getJraw() {
return jraw;
}

public void setJraw(Map<String, Object> jraw) {
this.jraw = jraw;
}

public EAddressStatus getStatus() {
return status;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class EBasicJsonMapVarchar {
String name;

@DbJson(storage = DbJsonType.VARCHAR)//, length = 2200)
Map<String, Object> content;
Map<String, Object> content;

public Long getId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.tests.model.embedded.EPerson;
import org.tests.model.json.PlainBean;

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

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

Expand All @@ -18,17 +20,20 @@ public class TestNativeWithEmbedded extends BaseTestCase {
@Test
public void test() {

Map<String, Object> rawMap = new LinkedHashMap<>();
rawMap.put("a","1");
EPerson person = new EPerson();
person.setName("Frank");
EAddress address = new EAddress();
address.setStreet("1 foo st");
address.setCity("barv");
address.setJbean(new PlainBean("hi", 3));
address.setJraw(rawMap);
person.setAddress(address);

DB.save(person);

String sql = "select id, name, street, suburb, addr_city, addr_status, addr_jbean from eperson where id = ?";
String sql = "select id, name, street, suburb, addr_city, addr_status, addr_jbean, jraw from eperson where id = ?";

LoggedSqlCollector.start();

Expand All @@ -39,6 +44,7 @@ public void test() {
assertThat(one.getName()).isEqualTo("Frank");
assertThat(one.getAddress().getStreet()).isEqualTo("1 foo st");
assertThat(one.getAddress().getJbean().getName()).isEqualTo("hi");
assertThat(one.getAddress().getJraw().get("a")).isEqualTo("1");

List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(1);
Expand Down

0 comments on commit d601a88

Please sign in to comment.