mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#502 - jsonContext.toJson(o, generator, path) can not apply path on @Transient field that is an entity bean
This commit is contained in:
@@ -2,12 +2,15 @@ package com.avaje.tests.json.include;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.tests.json.transientproperties.EJsonTransientEntityList;
|
||||
import com.avaje.tests.json.transientproperties.EJsonTransientList;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestJsonExcludeTransientEmptyList {
|
||||
@@ -45,4 +48,18 @@ public class TestJsonExcludeTransientEmptyList {
|
||||
|
||||
assertEquals(expectedJson, asJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJson_with_transientExcludeFromPathProperties() throws Exception {
|
||||
|
||||
EJsonTransientEntityList bean = new EJsonTransientEntityList();
|
||||
bean.setId(99L);
|
||||
bean.setName("John");
|
||||
|
||||
PathProperties pathProps = PathProperties.parse("id,name");
|
||||
|
||||
String asJson = Ebean.json().toJson(bean, pathProps);
|
||||
|
||||
assertThat(asJson).isEqualTo("{\"id\":99,\"name\":\"John\"}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.avaje.tests.json.transientproperties;
|
||||
|
||||
import com.avaje.ebean.annotation.Sql;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
import java.util.List;
|
||||
|
||||
@Sql
|
||||
@Entity
|
||||
public class EJsonTransientEntityList {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Transient
|
||||
private Boolean basic;
|
||||
|
||||
@Transient
|
||||
private List<Order> orders;
|
||||
|
||||
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 Boolean getBasic() {
|
||||
return basic;
|
||||
}
|
||||
|
||||
public void setBasic(Boolean basic) {
|
||||
this.basic = basic;
|
||||
}
|
||||
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.avaje.tests.json.transientproperties;
|
||||
|
||||
import com.avaje.ebean.annotation.Sql;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
import java.util.List;
|
||||
|
||||
@Sql
|
||||
@Entity
|
||||
public class ModelA {
|
||||
|
||||
@Id
|
||||
int id;
|
||||
|
||||
String a;
|
||||
|
||||
// transient mapping to an entity bean
|
||||
@Transient
|
||||
List<ModelB> list;
|
||||
|
||||
public String getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public void setA(String a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public List<ModelB> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<ModelB> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.avaje.tests.json.transientproperties;
|
||||
|
||||
|
||||
import com.avaje.ebean.annotation.Sql;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Sql
|
||||
@Entity
|
||||
public class ModelB {
|
||||
|
||||
Integer oneField;
|
||||
|
||||
Integer twoField;
|
||||
|
||||
public Integer getOneField() {
|
||||
return oneField;
|
||||
}
|
||||
|
||||
public void setOneField(Integer oneField) {
|
||||
this.oneField = oneField;
|
||||
}
|
||||
|
||||
public Integer getTwoField() {
|
||||
return twoField;
|
||||
}
|
||||
|
||||
public void setTwoField(Integer twoField) {
|
||||
this.twoField = twoField;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.tests.json.transientproperties;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class TestModelAJson {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ModelA a = new ModelA();
|
||||
a.setId(1);
|
||||
a.setA("a");
|
||||
|
||||
ModelB b = new ModelB();
|
||||
b.setOneField(1);
|
||||
b.setTwoField(1);
|
||||
|
||||
a.setList(new ArrayList<ModelB>());
|
||||
a.getList().add(b);
|
||||
|
||||
PathProperties pathProperties = PathProperties.parse("(a,list(oneField))");
|
||||
|
||||
String json = Ebean.json().toJson(a, pathProperties);
|
||||
|
||||
assertThat(json).isEqualTo("{\"a\":\"a\",\"list\":[{\"oneField\":1}]}");
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user