From 67c711d75fe680ff1e2d11453689b32bd03af0d8 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Wed, 28 Oct 2015 09:10:19 +1300 Subject: [PATCH] #442 - @JsonIgnore / @Expose ... not working on @OneToMany or @ManyToMany - was - @Transient field on @MappedSuperclass is not ignored by toJson() --- .../server/deploy/parse/AnnotationFields.java | 44 +++++++++++-------- .../com/avaje/tests/iud/TestInfoOneToOne.java | 22 +++++++--- .../avaje/tests/model/info/InfoCompany.java | 2 + .../avaje/tests/model/info/InfoCustomer.java | 6 ++- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java index 6169cfff0..5cb6ad912 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java @@ -76,6 +76,8 @@ public class AnnotationFields extends AnnotationParser { */ private void readAssocOne(DeployBeanProperty prop) { + readJsonAnnotations(prop); + Id id = get(prop, Id.class); if (id != null) { prop.setId(); @@ -117,25 +119,7 @@ public class AnnotationFields extends AnnotationParser { readColumn(column, prop); } - if (jacksonAnnotationsPresent) { - com.fasterxml.jackson.annotation.JsonIgnore jsonIgnore = get(prop, com.fasterxml.jackson.annotation.JsonIgnore.class); - if (jsonIgnore != null) { - prop.setJsonSerialize(!jsonIgnore.value()); - prop.setJsonDeserialize(!jsonIgnore.value()); - } - } - - Expose expose = get(prop, Expose.class); - if (expose != null) { - prop.setJsonSerialize(expose.serialize()); - prop.setJsonDeserialize(expose.deserialize()); - } - - JsonIgnore jsonIgnore = get(prop, JsonIgnore.class); - if (jsonIgnore != null) { - prop.setJsonSerialize(jsonIgnore.serialize()); - prop.setJsonDeserialize(jsonIgnore.deserialize()); - } + readJsonAnnotations(prop); if (prop.getDbColumn() == null) { // No @Column annotation or @Column.name() not set @@ -291,6 +275,28 @@ public class AnnotationFields extends AnnotationParser { } } + private void readJsonAnnotations(DeployBeanProperty prop) { + if (jacksonAnnotationsPresent) { + com.fasterxml.jackson.annotation.JsonIgnore jsonIgnore = get(prop, com.fasterxml.jackson.annotation.JsonIgnore.class); + if (jsonIgnore != null) { + prop.setJsonSerialize(!jsonIgnore.value()); + prop.setJsonDeserialize(!jsonIgnore.value()); + } + } + + Expose expose = get(prop, Expose.class); + if (expose != null) { + prop.setJsonSerialize(expose.serialize()); + prop.setJsonDeserialize(expose.deserialize()); + } + + JsonIgnore jsonIgnore = get(prop, JsonIgnore.class); + if (jsonIgnore != null) { + prop.setJsonSerialize(jsonIgnore.serialize()); + prop.setJsonDeserialize(jsonIgnore.deserialize()); + } + } + private boolean hasRelationshipItem(DeployBeanProperty prop) { return get(prop, OneToMany.class) != null || get(prop, ManyToOne.class) != null || diff --git a/src/test/java/com/avaje/tests/iud/TestInfoOneToOne.java b/src/test/java/com/avaje/tests/iud/TestInfoOneToOne.java index 08eca3425..66ab160c6 100644 --- a/src/test/java/com/avaje/tests/iud/TestInfoOneToOne.java +++ b/src/test/java/com/avaje/tests/iud/TestInfoOneToOne.java @@ -1,11 +1,15 @@ package com.avaje.tests.iud; import com.avaje.ebean.BaseTestCase; +import com.avaje.ebean.Ebean; import com.avaje.tests.model.info.InfoCompany; import com.avaje.tests.model.info.InfoCustomer; -import org.junit.Assert; import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + public class TestInfoOneToOne extends BaseTestCase { @Test @@ -20,13 +24,17 @@ public class TestInfoOneToOne extends BaseTestCase { customer.save(); + // assert JsonIgnore working as expected + String asJson = Ebean.json().toJson(company); + assertThat(asJson).doesNotContain("contacts"); + // assert both are inserted - Assert.assertNotNull(customer.getId()); - Assert.assertNotNull(company.getId()); + assertNotNull(customer.getId()); + assertNotNull(company.getId()); // both can be fetched - Assert.assertNotNull(InfoCustomer.find.byId(customer.getId())); - Assert.assertNotNull(InfoCompany.find.byId(company.getId())); + assertNotNull(InfoCustomer.find.byId(customer.getId())); + assertNotNull(InfoCompany.find.byId(company.getId())); // just update the customer @@ -47,8 +55,8 @@ public class TestInfoOneToOne extends BaseTestCase { // delete both customer and company fetchedCustomer.delete(); - Assert.assertNull(InfoCustomer.find.byId(customer.getId())); - Assert.assertNull(InfoCompany.find.byId(company.getId())); + assertNull(InfoCustomer.find.byId(customer.getId())); + assertNull(InfoCompany.find.byId(company.getId())); } diff --git a/src/test/java/com/avaje/tests/model/info/InfoCompany.java b/src/test/java/com/avaje/tests/model/info/InfoCompany.java index c8fa175e3..86ba42185 100644 --- a/src/test/java/com/avaje/tests/model/info/InfoCompany.java +++ b/src/test/java/com/avaje/tests/model/info/InfoCompany.java @@ -1,6 +1,7 @@ package com.avaje.tests.model.info; import com.avaje.ebean.Model; +import com.avaje.ebean.annotation.JsonIgnore; import javax.persistence.*; import java.util.ArrayList; @@ -19,6 +20,7 @@ public class InfoCompany extends Model { String name; + @JsonIgnore @OneToMany(mappedBy = "company", cascade = CascadeType.ALL) List contacts = new ArrayList(); diff --git a/src/test/java/com/avaje/tests/model/info/InfoCustomer.java b/src/test/java/com/avaje/tests/model/info/InfoCustomer.java index 033b2785d..f3928c82d 100644 --- a/src/test/java/com/avaje/tests/model/info/InfoCustomer.java +++ b/src/test/java/com/avaje/tests/model/info/InfoCustomer.java @@ -2,7 +2,11 @@ package com.avaje.tests.model.info; import com.avaje.ebean.Model; -import javax.persistence.*; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Version; @Entity public class InfoCustomer extends Model {