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 4247594  JUNEAU-233
4247594 is described below

commit 4247594ecc3e9561a6480ca7601e8505334b3325
Author: JamesBognar <[email protected]>
AuthorDate: Sat May 23 15:34:19 2020 -0400

    JUNEAU-233
    
    JsonParser is not instantiating interface proxies.
---
 .../test/java/org/apache/juneau/ProxyBeanTest.java | 54 +++++++++++++++++++++
 .../org/apache/juneau/html/HtmlParserSession.java  |  3 ++
 .../org/apache/juneau/json/JsonParserSession.java  |  2 +
 .../juneau/msgpack/MsgPackParserSession.java       |  2 +
 .../org/apache/juneau/uon/UonParserSession.java    |  2 +
 .../urlencoding/UrlEncodingParserSession.java      |  4 +-
 .../org/apache/juneau/xml/XmlParserSession.java    |  6 +++
 .../apache/juneau/rest/client2/RestClientTest.java | 55 ++++++++++++++--------
 8 files changed, 108 insertions(+), 20 deletions(-)

diff --git 
a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ProxyBeanTest.java
 
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ProxyBeanTest.java
new file mode 100644
index 0000000..69c5e4a
--- /dev/null
+++ 
b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/ProxyBeanTest.java
@@ -0,0 +1,54 @@
+// 
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright 
ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                
                                              *
+// *                                                                           
                                              *
+// *  http://www.apache.org/licenses/LICENSE-2.0                               
                                              *
+// *                                                                           
                                              *
+// * Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the 
License.                                              *
+// 
***************************************************************************************************************************
+package org.apache.juneau;
+
+import static org.junit.Assert.*;
+
+import org.apache.juneau.html.*;
+import org.apache.juneau.json.*;
+import org.apache.juneau.msgpack.*;
+import org.apache.juneau.oapi.*;
+import org.apache.juneau.uon.*;
+import org.apache.juneau.urlencoding.*;
+import org.apache.juneau.xml.*;
+import org.junit.*;
+
+public class ProxyBeanTest {
+
+       
//====================================================================================================
+       // testBasic
+       
//====================================================================================================
+
+       public static interface A {
+               void setFoo(int foo);
+               int getFoo();
+       }
+
+       @Test
+       public void testBasic() throws Exception {
+               A a = JsonParser.DEFAULT.parse("{foo:1}", A.class);
+               assertEquals(1, a.getFoo());
+               a = XmlParser.DEFAULT.parse("<object><foo>1</foo></object>", 
A.class);
+               assertEquals(1, a.getFoo());
+               a = UonParser.DEFAULT.parse("(foo=1)", A.class);
+               assertEquals(1, a.getFoo());
+               a = UrlEncodingParser.DEFAULT.parse("foo=1", A.class);
+               assertEquals(1, a.getFoo());
+               a = MsgPackParser.DEFAULT.parse("81A3666F6F01", A.class);
+               assertEquals(1, a.getFoo());
+               a = 
HtmlParser.DEFAULT.parse("<table><tr><td>foo</td><td>1</td></tr></table>", 
A.class);
+               assertEquals(1, a.getFoo());
+               a = OpenApiParser.DEFAULT.parse("foo=1", A.class);
+               assertEquals(1, a.getFoo());
+       }
+}
\ No newline at end of file
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
index bdd0e7f..72d70c5 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlParserSession.java
@@ -224,6 +224,9 @@ public final class HtmlParserSession extends 
XmlParserSession {
                                } else if (sType.canCreateNewBean(outer)) {
                                        BeanMap m = newBeanMap(outer, 
sType.getInnerClass());
                                        o = parseIntoBean(r, m).getBean();
+                               } else if (sType.getProxyInvocationHandler() != 
null) {
+                                       BeanMap m = newBeanMap(outer, 
sType.getInnerClass());
+                                       o = parseIntoBean(r, m).getBean();
                                } else {
                                        isValid = false;
                                }
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonParserSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonParserSession.java
index d4d85d9..96dcbd1 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonParserSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/json/JsonParserSession.java
@@ -211,6 +211,8 @@ public final class JsonParserSession extends 
ReaderParserSession {
                        parseIntoMap2(r, m, sType.getKeyType(), 
sType.getValueType(), pMeta);
                        if (m.containsKey(getBeanTypePropertyName(eType)))
                                o = cast((OMap)m, pMeta, eType);
+                       else if (sType.getProxyInvocationHandler() != null)
+                               o = newBeanMap(outer, 
sType.getInnerClass()).load(m).getBean();
                        else
                                throw new ParseException(this, "Class ''{0}'' 
could not be instantiated.  Reason: ''{1}''",
                                                
sType.getInnerClass().getName(), sType.getNotABeanReason());
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackParserSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackParserSession.java
index c9c619b..cd96799 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackParserSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/msgpack/MsgPackParserSession.java
@@ -194,6 +194,8 @@ public final class MsgPackParserSession extends 
InputStreamParserSession {
                                        m.put((String)parseAnything(string(), 
is, outer, pMeta), parseAnything(object(), is, m, pMeta));
                                if 
(m.containsKey(getBeanTypePropertyName(eType)))
                                        o = cast(m, pMeta, eType);
+                               else if (sType.getProxyInvocationHandler() != 
null)
+                                       o = newBeanMap(outer, 
sType.getInnerClass()).load(m).getBean();
                                else
                                        throw new ParseException(this, "Class 
''{0}'' could not be instantiated.  Reason: ''{1}''",
                                                
sType.getInnerClass().getName(), sType.getNotABeanReason());
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonParserSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonParserSession.java
index f432e31..6aad9f8 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonParserSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/uon/UonParserSession.java
@@ -288,6 +288,8 @@ public class UonParserSession extends ReaderParserSession 
implements HttpPartPar
                        parseIntoMap(r, m, string(), object(), pMeta);
                        if (m.containsKey(getBeanTypePropertyName(sType)))
                                o = cast(m, pMeta, eType);
+                       else if (sType.getProxyInvocationHandler() != null)
+                               o = newBeanMap(outer, 
sType.getInnerClass()).load(m).getBean();
                        else
                                throw new ParseException(this, "Class ''{0}'' 
could not be instantiated.  Reason: ''{1}''",
                                        sType.getInnerClass().getName(), 
sType.getNotABeanReason());
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingParserSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingParserSession.java
index 72ed9c9..5e72eed 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingParserSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/urlencoding/UrlEncodingParserSession.java
@@ -142,8 +142,10 @@ public class UrlEncodingParserSession extends 
UonParserSession {
                        parseIntoMap2(r, m, getClassMeta(Map.class, 
String.class, Object.class), outer);
                        if (m.containsKey(getBeanTypePropertyName(eType)))
                                o = cast(m, null, eType);
-                       else if (m.containsKey("_value")) {
+                       else if (m.containsKey("_value"))
                                o = convertToType(m.get("_value"), sType);
+                       else if (sType.getProxyInvocationHandler() != null) {
+                               o = newBeanMap(outer, 
sType.getInnerClass()).load(m).getBean();
                        } else {
                                if (sType.getNotABeanReason() != null)
                                        throw new ParseException(this, "Class 
''{0}'' could not be instantiated as application/x-www-form-urlencoded.  
Reason: ''{1}''", sType, sType.getNotABeanReason());
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlParserSession.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlParserSession.java
index 3a623c1..3571059 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlParserSession.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/xml/XmlParserSession.java
@@ -385,6 +385,12 @@ public class XmlParserSession extends ReaderParserSession {
                        o = toArray(sType, l);
                } else if (sType.canCreateNewInstanceFromString(outer)) {
                        o = sType.newInstanceFromString(outer, 
getElementText(r));
+               } else if (sType.getProxyInvocationHandler() != null) {
+                       OMap m = new OMap(this);
+                       parseIntoMap(r, m, string(), object(), pMeta);
+                       if (wrapperAttr != null)
+                               m = new OMap(this).a(wrapperAttr, m);
+                       o = newBeanMap(outer, 
sType.getInnerClass()).load(m).getBean();
                } else {
                        throw new ParseException(this,
                                "Class ''{0}'' could not be instantiated.  
Reason: ''{1}'', property: ''{2}''",
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 1f15acb..6b50ead 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
@@ -3344,30 +3344,47 @@ public class RestClientTest {
                                .run()
                                .cacheBody()
                                .getBody().assertContains("{foo:'1',bar:null}")
-                               .getBody().as(O34.class);
+                               .getBody().as(O36.class);
                } catch (RestCallException e) {
                        
assertTrue(e.getCause(ParseException.class).getMessage().contains("Unknown 
property 'bar'"));
                }
        }
 
-       //      @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient dontIgnoreUnknownNullBeanProperties() {
-//             super.dontIgnoreUnknownNullBeanProperties();
-//             return this;
-//     }
-//
-//     @Test
-//     public void o0_beanContext_() throws Exception {
-//     }
-//     @Override /* GENERATED - BeanContextBuilder */
-//     public MockRestClient dontUseInterfaceProxies() {
-//             super.dontUseInterfaceProxies();
-//             return this;
-//     }
-//
+       public static interface O37 {
+               public String getFoo();
+               public void setFoo(String foo);
+       }
+
+       @Test
+       public void o037_beanContext_dontUseInterfaceProxies() throws Exception 
{
+               O37 x = MockRestClient
+                       .create(A.class)
+                       .simpleJson()
+                       .build()
+                       .post("/echoBody", new StringReader("{foo:'1'}"))
+                       .run()
+                       .cacheBody()
+                       .getBody().assertContains("{foo:'1'}")
+                       .getBody().as(O37.class);
+               ;
+               assertEquals("1", x.getFoo());
+
+               try {
+                       MockRestClient
+                               .create(A.class)
+                               .simpleJson()
+                               .dontUseInterfaceProxies()
+                               .build()
+                               .post("/echoBody", new 
StringReader("{foo:'1'}"))
+                               .run()
+                               .cacheBody()
+                               .getBody().assertContains("{foo:'1'}")
+                               .getBody().as(O37.class);
+               } catch (RestCallException e) {
+                       
assertTrue(e.getCause(ParseException.class).getMessage().contains("could not be 
instantiated"));
+               }
+       }
+
 //     @Test
 //     public void o0_beanContext_() throws Exception {
 //     }

Reply via email to