From fc53e281e88154088a4a44d1795bd758e0645f11 Mon Sep 17 00:00:00 2001 From: rbygrave Date: Fri, 24 Apr 2015 23:16:10 +1200 Subject: [PATCH] #276 - WriteJson not write Transient properties (without using PathProperties) --- .../server/text/json/WriteJson.java | 12 +++- .../tests/text/json/TestTextJsonSimple.java | 67 +++++++++++++++++-- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/text/json/WriteJson.java b/src/main/java/com/avaje/ebeaninternal/server/text/json/WriteJson.java index 3c3dc9914..c4beef5ea 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/text/json/WriteJson.java +++ b/src/main/java/com/avaje/ebeaninternal/server/text/json/WriteJson.java @@ -108,6 +108,16 @@ public class WriteJson { } } + private boolean isIncludeTransientProperty(BeanProperty prop) { + if (!explicitAllProps && currentIncludeProps != null) { + // explicitly controlled by pathProperties + return currentIncludeProps.contains(prop.getName()); + } else { + // by default include transient properties + return true; + } + } + public void write(WriteJson writeJson) throws IOException { BeanProperty beanProp = desc.getIdProperty(); @@ -127,7 +137,7 @@ public class WriteJson { } props = desc.propertiesTransient(); for (int j = 0; j < props.length; j++) { - if (isIncludeProperty(props[j])) { + if (isIncludeTransientProperty(props[j])) { props[j].jsonWrite(writeJson, currentBean); } } diff --git a/src/test/java/com/avaje/tests/text/json/TestTextJsonSimple.java b/src/test/java/com/avaje/tests/text/json/TestTextJsonSimple.java index ab2041638..82c18b3bb 100644 --- a/src/test/java/com/avaje/tests/text/json/TestTextJsonSimple.java +++ b/src/test/java/com/avaje/tests/text/json/TestTextJsonSimple.java @@ -3,6 +3,7 @@ package com.avaje.tests.text.json; import com.avaje.ebean.BaseTestCase; import com.avaje.ebean.Ebean; import com.avaje.ebean.EbeanServer; +import com.avaje.ebean.text.PathProperties; import com.avaje.ebean.text.json.JsonContext; import com.avaje.tests.model.basic.Customer; import com.avaje.tests.model.basic.ResetBasicData; @@ -19,10 +20,11 @@ public class TestTextJsonSimple extends BaseTestCase { ResetBasicData.reset(); - List list = Ebean.find(Customer.class).select("id, name, status, shippingAddress") - .fetch("billingAddress", "line1, city").fetch("billingAddress.country", "*") - .fetch("contacts", "firstName,email") - .order().desc("id").findList(); + List list = Ebean.find(Customer.class) + .select("id, name, status, shippingAddress") + .fetch("billingAddress", "line1, city").fetch("billingAddress.country", "*") + .fetch("contacts", "firstName,email") + .order().desc("id").findList(); EbeanServer server = Ebean.getServer(null); @@ -31,8 +33,63 @@ public class TestTextJsonSimple extends BaseTestCase { String jsonOutput = json.toJson(list); System.out.println(jsonOutput); - List mList = json.toList(Customer.class, jsonOutput); + // check that transient fields are included by default in the JSON output + Assert.assertTrue(jsonOutput.contains("\"selected\":")); + List mList = json.toList(Customer.class, jsonOutput); Assert.assertEquals(list.size(), mList.size()); } + + @Test + public void testTransientIncludedByDefault() { + + ResetBasicData.reset(); + + List list = Ebean.find(Customer.class) + .select("id, name") + .order().desc("id").findList(); + + EbeanServer server = Ebean.getServer(null); + + JsonContext json = server.json(); + + // not using pathProperties - includes transient fields by default + String jsonOutput = json.toJson(list); + System.out.println(jsonOutput); + + // check that transient fields are included by default in the JSON output + Assert.assertTrue(jsonOutput.contains("\"selected\":")); + + List mList = json.toList(Customer.class, jsonOutput); + Assert.assertEquals(list.size(), mList.size()); + } + + @Test + public void testTransientIncludedExplicitly() { + + ResetBasicData.reset(); + + List list = Ebean.find(Customer.class) + .select("id, name") + .order().desc("id").findList(); + + EbeanServer server = Ebean.getServer(null); + + JsonContext json = server.json(); + + PathProperties pathProperties = PathProperties.parse("(id,name,selected)"); + String jsonOutput = json.toJson(list, pathProperties); + System.out.println(jsonOutput); + + // check that transient fields are included by explicit pathProperties + Assert.assertTrue(jsonOutput.contains("\"selected\":")); + + pathProperties = PathProperties.parse("(id,name)"); + jsonOutput = json.toJson(list, pathProperties); + System.out.println(jsonOutput); + + // check that transient fields are NOT included when explicitly excluded by pathProperties + Assert.assertFalse(jsonOutput.contains("\"selected\":")); + } + }