This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 8cac775  JUNEAU-236
8cac775 is described below

commit 8cac7752add3e554be5e4077ca053bd33b073df2
Author: JamesBognar <[email protected]>
AuthorDate: Mon May 25 18:40:27 2020 -0400

    JUNEAU-236
    
    BEAN_addRootType doesn't work without BEAN_addBeanTypes
---
 .../apache/juneau/jena/RdfSerializerSession.java   |   2 +-
 .../org/apache/juneau/BeanTraverseSession.java     |  14 +-
 .../apache/juneau/json/JsonSerializerSession.java  |   2 +-
 .../juneau/msgpack/MsgPackSerializerSession.java   |   2 +-
 .../juneau/serializer/SerializerSession.java       |   5 +-
 .../apache/juneau/uon/UonSerializerSession.java    |   2 +-
 .../urlencoding/UrlEncodingSerializerSession.java  |   2 +-
 .../apache/juneau/rest/client2/RestClientTest.java | 308 ++++++++++++++-------
 8 files changed, 231 insertions(+), 106 deletions(-)

diff --git 
a/juneau-core/juneau-marshall-rdf/src/main/java/org/apache/juneau/jena/RdfSerializerSession.java
 
b/juneau-core/juneau-marshall-rdf/src/main/java/org/apache/juneau/jena/RdfSerializerSession.java
index b808b73..10a971f 100644
--- 
a/juneau-core/juneau-marshall-rdf/src/main/java/org/apache/juneau/jena/RdfSerializerSession.java
+++ 
b/juneau-core/juneau-marshall-rdf/src/main/java/org/apache/juneau/jena/RdfSerializerSession.java
@@ -190,7 +190,7 @@ public final class RdfSerializerSession extends 
WriterSerializerSession {
                        sType = eType.getSerializedClassMeta(this);
                }
 
-               String typeName = getBeanTypeName(eType, aType, bpm);
+               String typeName = getBeanTypeName(this, eType, aType, bpm);
 
                RDFNode n = null;
 
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseSession.java
index 2f03183..236b256 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanTraverseSession.java
@@ -131,6 +131,18 @@ public class BeanTraverseSession extends BeanSession {
        }
 
        /**
+        * Returns <jk>true</jk> if we're processing the root node.
+        *
+        * <p>
+        * Must be called after {@link #push(String, Object, ClassMeta)} and 
before {@link #pop()}.
+        *
+        * @return <jk>true</jk> if we're processing the root node.
+        */
+       protected final boolean isRoot() {
+               return depth == 1;
+       }
+
+       /**
         * Returns <jk>true</jk> if {@link 
BeanTraverseContext#BEANTRAVERSE_detectRecursions} is enabled, and the specified
         * object is already higher up in the traversal chain.
         *
@@ -154,7 +166,7 @@ public class BeanTraverseSession extends BeanSession {
 
        /**
         * Returns <jk>true</jk> if we're about to exceed the max depth for the 
document.
-        * 
+        *
         * @return <jk>true</jk> if we're about to exceed the max depth for the 
document.
         */
        protected final boolean willExceedDepth() {
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonSerializerSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonSerializerSession.java
index c24a5cc..dd58819 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonSerializerSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonSerializerSession.java
@@ -114,7 +114,7 @@ public class JsonSerializerSession extends 
WriterSerializerSession {
                }
 
                sType = aType;
-               String typeName = getBeanTypeName(eType, aType, pMeta);
+               String typeName = getBeanTypeName(this, eType, aType, pMeta);
 
                // Swap if necessary
                PojoSwap swap = aType.getSwap(this);
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackSerializerSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackSerializerSession.java
index 88ffd7c..cf01f44 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackSerializerSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackSerializerSession.java
@@ -97,7 +97,7 @@ public final class MsgPackSerializerSession extends 
OutputStreamSerializerSessio
                }
 
                sType = aType;
-               String typeName = getBeanTypeName(eType, aType, pMeta);
+               String typeName = getBeanTypeName(this, eType, aType, pMeta);
 
                // Swap if necessary
                PojoSwap swap = aType.getSwap(this);
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
index 6bc7628..e24f347 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerSession.java
@@ -531,16 +531,17 @@ public abstract class SerializerSession extends 
BeanTraverseSession {
        /**
         * Resolves the dictionary name for the actual type.
         *
+        * @param session The current serializer session.
         * @param eType The expected type of the bean property.
         * @param aType The actual type of the bean property.
         * @param pMeta The current bean property being serialized.
         * @return The bean dictionary name, or <jk>null</jk> if a name could 
not be found.
         */
-       protected final String getBeanTypeName(ClassMeta<?> eType, ClassMeta<?> 
aType, BeanPropertyMeta pMeta) {
+       protected final String getBeanTypeName(SerializerSession session, 
ClassMeta<?> eType, ClassMeta<?> aType, BeanPropertyMeta pMeta) {
                if (eType == aType)
                        return null;
 
-               if (! isAddBeanTypes())
+               if (! (isAddBeanTypes() || (session.isRoot() && 
isAddRootType())))
                        return null;
 
                String eTypeTn = eType.getDictionaryName();
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonSerializerSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonSerializerSession.java
index fd42b4b..570dfdf 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonSerializerSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonSerializerSession.java
@@ -124,7 +124,7 @@ public class UonSerializerSession extends 
WriterSerializerSession implements Htt
                }
 
                sType = aType;
-               String typeName = getBeanTypeName(eType, aType, pMeta);
+               String typeName = getBeanTypeName(this, eType, aType, pMeta);
 
                // Swap if necessary
                PojoSwap swap = aType.getSwap(this);
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingSerializerSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingSerializerSession.java
index 29fa799..44de4bd 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingSerializerSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingSerializerSession.java
@@ -103,7 +103,7 @@ public class UrlEncodingSerializerSession extends 
UonSerializerSession {
                        aType = object();
 
                sType = aType;
-               String typeName = getBeanTypeName(eType, aType, null);
+               String typeName = getBeanTypeName(this, eType, aType, null);
 
                // Swap if necessary
                PojoSwap swap = aType.getSwap(this);
diff --git 
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
 
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
index d4e0966..5b96fc6 100644
--- 
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
+++ 
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
@@ -3771,11 +3771,11 @@ public class RestClientTest {
        }
 
        public static class O48 {
-               private int foo;
-               public int getFoo() { return foo; }
-               public void setFoo(int foo) { this.foo = foo; }
+               private String foo;
+               public String getFoo() { return foo; }
+               public void setFoo(String foo) { this.foo = foo; }
                public O48 init() {
-                       foo = 1;
+                       foo = "foo";
                        return this;
                }
        }
@@ -3785,12 +3785,12 @@ public class RestClientTest {
                @Override
                public Object readProperty(O48 bean, String name, Object value) 
{
                        getterCalled = true;
-                       return value;
+                       return "x" + value;
                }
                @Override
                public Object writeProperty(O48 bean, String name, Object 
value) {
                        setterCalled = true;
-                       return value;
+                       return value.toString().substring(1);
                }
        }
 
@@ -3804,104 +3804,216 @@ public class RestClientTest {
                        .post("/echoBody", new O48().init())
                        .run()
                        .cacheBody()
-                       .assertBody().is("{foo:1}")
+                       .assertBody().is("{foo:'xfoo'}")
                        .getBody().as(O48.class)
                ;
-               assertEquals(1, x.foo);
+               assertEquals("foo", x.foo);
                assertTrue(O48Interceptor.getterCalled);
                assertTrue(O48Interceptor.setterCalled);
        }
 
-       //      @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient propertyFilter(Class<?> on, Class<? extends 
org.apache.juneau.transform.PropertyFilter> value) {
-//             super.propertyFilter(on, value);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient propertyNamer(Class<? extends 
org.apache.juneau.PropertyNamer> value) {
-//             super.propertyNamer(value);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient propertyNamer(Class<?> on, Class<? extends 
org.apache.juneau.PropertyNamer> value) {
-//             super.propertyNamer(on, value);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient sortProperties() {
-//             super.sortProperties();
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient sortProperties(java.lang.Class<?>...on) {
-//             super.sortProperties(on);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient stopClass(Class<?> on, Class<?> value) {
-//             super.stopClass(on, value);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient swaps(Object...values) {
-//             super.swaps(values);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient swapsRemove(Object...values) {
-//             super.swapsRemove(values);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient swapsReplace(Object...values) {
-//             super.swapsReplace(values);
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient timeZone(TimeZone value) {
-//             super.timeZone(value);
-//             return this;
-//     }
-//
+       public static class O49 {
+               private String fooBar;
+               public String getFooBar() { return fooBar; }
+               public void setFooBar(String fooBar) { this.fooBar = fooBar; }
+               public O49 init() {
+                       fooBar = "fooBar";
+                       return this;
+               }
+       }
+
+       @Test
+       public void o049_beanContext_propertyNamer() throws Exception {
+               O49 x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .propertyNamer(PropertyNamerDLC.class)
+                       .build()
+                       .post("/echoBody", new O49().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{'foo-bar':'fooBar'}")
+                       .getBody().as(O49.class)
+               ;
+               assertEquals("fooBar", x.fooBar);
+
+               x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .propertyNamer(O49.class, PropertyNamerDLC.class)
+                       .build()
+                       .post("/echoBody", new O49().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{'foo-bar':'fooBar'}")
+                       .getBody().as(O49.class)
+               ;
+               assertEquals("fooBar", x.fooBar);
+       }
+
+       public static class O50 {
+               public int foo, bar, baz;
+               public O50 init() {
+                       foo = 1;
+                       bar = 2;
+                       baz = 3;
+                       return this;
+               }
+       }
+
+       @Test
+       public void o050_beanContext_sortProperties() throws Exception {
+               O50 x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .sortProperties()
+                       .build()
+                       .post("/echoBody", new O50().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{bar:2,baz:3,foo:1}")
+                       .getBody().as(O50.class)
+               ;
+               assertEquals(1, x.foo);
+
+               x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .sortProperties(O50.class)
+                       .build()
+                       .post("/echoBody", new O50().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{bar:2,baz:3,foo:1}")
+                       .getBody().as(O50.class)
+               ;
+               assertEquals(1, x.foo);
+       }
+
+       public static class O51a {
+               public int foo;
+       }
+
+       public static class O51b extends O51a {
+               public int bar;
+               public O51b init() {
+                       foo = 1;
+                       bar = 2;
+                       return this;
+               }
+       }
+
+       @Test
+       public void o051_beanContext_stopClass() throws Exception {
+               O51b x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .stopClass(O51b.class, O51a.class)
+                       .build()
+                       .post("/echoBody", new O51b().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{bar:2}")
+                       .getBody().as(O51b.class)
+               ;
+               assertEquals(0, x.foo);
+               assertEquals(2, x.bar);
+       }
+
+       public static class O52 {
+               public int foo;
+               public O52 init() {
+                       this.foo = 1;
+                       return this;
+               }
+       }
+
+       public static class O52Swap extends PojoSwap<O52,Integer> {
+               @Override
+               public Integer swap(BeanSession session, O52 o) { return o.foo; 
}
+               @Override
+               public O52 unswap(BeanSession session, Integer f, ClassMeta<?> 
hint) {return new O52().init(); }
+       }
+
+       @Test
+       public void o052_beanContext_swaps() throws Exception {
+               O52 x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .swaps(O52Swap.class)
+                       .build()
+                       .post("/echoBody", new O52().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("1")
+                       .getBody().as(O52.class)
+               ;
+               assertEquals(1, x.foo);
+       }
+
+       public static class O53 {
+               public int foo;
+               public O53 init() {
+                       foo = 1;
+                       return this;
+               }
+       }
+
+       public static class O53Swap extends StringSwap<O53> {
+               @Override
+               public String swap(BeanSession session, O53 o) throws Exception 
{
+                       assertEquals(TimeZone.getTimeZone("Z"), 
session.getTimeZone());
+                       return super.swap(session, o);
+               }
+
+               @Override
+               public O53 unswap(BeanSession session, String f, ClassMeta<?> 
hint) throws Exception {
+                       assertEquals(TimeZone.getTimeZone("Z"), 
session.getTimeZone());
+                       return super.unswap(session, f, hint);
+               }
+       }
+
+       @Test
+       public void o053_beanContext_timeZone() throws Exception {
+               O53 x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .timeZone(TimeZone.getTimeZone("Z"))
+                       .build()
+                       .post("/echoBody", new O53().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{foo:1}")
+                       .getBody().as(O53.class)
+               ;
+               assertEquals(1, x.foo);
+       }
+
+       public static class O54 {
+               public int foo;
+               public O54 init() {
+                       this.foo = 1;
+                       return this;
+               }
+       }
+
+       @Test
+       public void o054_beanContext_typeName() throws Exception {
+               O54 x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .typeName(O54.class, "foo")
+                       .addRootType()
+                       .build()
+                       .post("/echoBody", new O54().init())
+                       .run()
+                       .cacheBody()
+                       .assertBody().is("{_type:'foo',foo:1}")
+                       .getBody().as(O54.class)
+               ;
+               assertEquals(1, x.foo);
+       }
+
 //     @Test
 //     public void o0_beanContext_() throws Exception {
 //     }

Reply via email to