You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2019/06/21 18:20:07 UTC
[cxf] branch 3.2.x-fixes updated: CXF-8059: @FormParam inside
@BeanParam not working
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/3.2.x-fixes by this push:
new 78a9fb7 CXF-8059: @FormParam inside @BeanParam not working
78a9fb7 is described below
commit 78a9fb79de48750640d20637869880b3e92ae3b4
Author: reta <dr...@gmail.com>
AuthorDate: Fri Jun 21 11:27:13 2019 -0400
CXF-8059: @FormParam inside @BeanParam not working
---
.../apache/cxf/jaxrs/client/ClientProxyImpl.java | 18 ++++-
.../org/apache/cxf/systest/jaxrs/BookStore.java | 80 +++++++++++++++++++++-
.../systest/jaxrs/JAXRSClientServerBookTest.java | 36 ++++++++++
3 files changed, 132 insertions(+), 2 deletions(-)
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
index 6aaee1c..3b7a345 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
@@ -41,6 +41,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
+import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -286,8 +287,10 @@ public class ClientProxyImpl extends AbstractClient implements
body = handleForm(m, params, types, beanParamsList);
} else if (types.containsKey(ParameterType.REQUEST_BODY)) {
body = handleMultipart(types, ori, params);
+ } else if (hasFormParams(params, beanParamsList)) {
+ body = handleForm(m, params, types, beanParamsList);
}
-
+
setRequestHeaders(headers, ori, types.containsKey(ParameterType.FORM),
body == null ? null : body.getClass(), m.getReturnType());
@@ -996,6 +999,19 @@ public class ClientProxyImpl extends AbstractClient implements
return aMethod == null || bodyIndex == -1 ? new Annotation[0]
: aMethod.getParameterAnnotations()[bodyIndex];
}
+
+ /**
+ * Checks if @BeanParam object has at least one @FormParam declaration.
+ * @param params parameter values
+ * @param beanParams bean parameters
+ * @return "true" @BeanParam object has at least one @FormParam, "false" otherwise
+ */
+ private boolean hasFormParams(Object[] params, List<Parameter> beanParams) {
+ return beanParams
+ .stream()
+ .map(p -> getValuesFromBeanParam(params[p.getIndex()], FormParam.class))
+ .anyMatch(((Predicate<Map<String, BeanPair>>) Map::isEmpty).negate());
+ }
protected class BodyWriter extends AbstractBodyWriter {
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index f73df80..bfa5851 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -47,6 +47,7 @@ import javax.ws.rs.CookieParam;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Encoded;
+import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.HeaderParam;
@@ -399,11 +400,55 @@ public class BookStore {
long id = bean1.getId() + bean1.getId2() + bean1.getId3();
if (bean2.getId4() != id) {
- throw new RuntimeException();
+ throw new RuntimeException("id4 != id");
+ }
+ return books.get(id);
+ }
+
+ @POST
+ @Path("/formBeanParams/{id}")
+ @Produces("application/xml")
+ public Book postFormBeanParamsBook(@BeanParam BookBeanForm bean) {
+ long id = bean.getId() + bean.getId2();
+ if (bean.getId3() != id) {
+ throw new RuntimeException("id3 != id");
+ }
+ return books.get(id);
+ }
+
+ @POST
+ @Path("/formParams/{id}")
+ @Produces("application/xml")
+ public Book postFormParamsBook(@PathParam("id") long id, @QueryParam("id2") long id2, @FormParam("id3") long id3) {
+ long theBookId = id + id2;
+ if (id3 != theBookId) {
+ throw new RuntimeException("id3 != id");
+ }
+ return books.get(theBookId);
+ }
+
+ @GET
+ @Path("/formBeanParams/{id}")
+ @Produces("application/xml")
+ public Book getFormBeanParamsBook(@BeanParam BookBeanForm bean) {
+ long id = bean.getId() + bean.getId2();
+ if (bean.getId3() != 0) {
+ throw new RuntimeException("id3 != 0");
}
return books.get(id);
}
+ @GET
+ @Path("/formParams/{id}")
+ @Produces("application/xml")
+ public Book getFormParamsBook(@PathParam("id") long id, @QueryParam("id2") long id2, @FormParam("id3") long id3) {
+ long theBookId = id + id2;
+ if (id3 != 0) {
+ throw new RuntimeException("id3 != 0");
+ }
+ return books.get(theBookId);
+ }
+
@POST
@Path("/mapperonbus")
public void mapperOnBus() {
@@ -1933,11 +1978,44 @@ public class BookStore {
public long getId4() {
return id4;
}
+
@QueryParam("id4")
public void setId4(long id4) {
this.id4 = id4;
}
+ }
+
+ public static class BookBeanForm {
+ private long id;
+ private long id2;
+ private long id3;
+
+ public long getId() {
+ return id;
+ }
+
+ @PathParam("id")
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ @QueryParam("id2")
+ public void setId2(long id2) {
+ this.id2 = id2;
+ }
+
+ public long getId2() {
+ return id2;
+ }
+ @FormParam("id3")
+ public void setId3(long id3) {
+ this.id3 = id3;
+ }
+
+ public long getId3() {
+ return id3;
+ }
}
public static class BookBean2 {
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 663d187..6482046 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -507,6 +507,42 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase {
assertEquals(123L, book.getId());
}
+
+ @Test
+ public void testProxyBeanPostFormParam() throws Exception {
+ BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+ BookStore.BookBeanForm bean = new BookStore.BookBeanForm();
+ bean.setId(100L);
+ bean.setId2(23L);
+ bean.setId3(123);
+ Book book = store.postFormBeanParamsBook(bean);
+ assertEquals(123L, book.getId());
+ }
+
+ @Test
+ public void testProxyBeanGetFormParam() throws Exception {
+ BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+ BookStore.BookBeanForm bean = new BookStore.BookBeanForm();
+ bean.setId(100L);
+ bean.setId2(23L);
+ bean.setId3(123);
+ Book book = store.getFormBeanParamsBook(bean);
+ assertEquals(123L, book.getId());
+ }
+
+ @Test
+ public void testProxyPostFormParam() throws Exception {
+ BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+ Book book = store.postFormParamsBook(100L, 23L, 123L);
+ assertEquals(123L, book.getId());
+ }
+
+ @Test
+ public void testProxyGetFormParam() throws Exception {
+ BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+ Book book = store.getFormParamsBook(100L, 23L, 123L);
+ assertEquals(123L, book.getId());
+ }
@Test
public void testGetBookWithCustomHeader() throws Exception {