Skip to content

Commit

Permalink
Adding support for private fields in @BeanParam (#5525)
Browse files Browse the repository at this point in the history
* Check field accessibility before accessing bean param field value
Signed-off-by: Divyansh Shekhar Gaur <divyanshshekhar@users.noreply.github.com>
Co-authored-by: divsgaur <26884362+divyanshshekhar@users.noreply.github.com>
  • Loading branch information
divyanshshekhar authored Mar 7, 2024
1 parent ba671e0 commit c12c0dd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -156,7 +156,7 @@ private void addBeanParameter(final Object beanParam)
anns.put(ann.annotationType(), ann);
}

if (hasAnyParamAnnotation(anns)) {
if (field.canAccess(beanParam) && hasAnyParamAnnotation(anns)) {
value = field.get(beanParam);
} else {
// get getter annotations if there are no field annotations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.client.proxy;


import jakarta.ws.rs.QueryParam;

/**
* @author Divyansh Shekhar Gaur
*/
public class MyBeanParamWithPrivateField {

@QueryParam("privateFieldParam")
private String privateFieldParam;

public MyBeanParamWithPrivateField() {}

public String getPrivateFieldParam() {
return privateFieldParam;
}

public void setPrivateFieldParam(String privateFieldParam) {
this.privateFieldParam = privateFieldParam;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -53,6 +53,11 @@ public String echoSubBean(@BeanParam MyGetBeanParam bean) {
return bean.getSubBeanParam().getSubQueryParam().toString();
}

@Override
public String echoPrivateField(@BeanParam MyBeanParamWithPrivateField bean) {
return bean.getPrivateFieldParam();
}

@Override
public String echo(MyBeanParam bean) {
return ("HEADER=" + bean.getHeaderParam() + ",PATH=" + bean.getPathParam() + ",FORM="
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -59,6 +59,12 @@ public interface MyResourceWithBeanParamIfc {
@Produces("text/plain")
public String echoSubBean(@BeanParam MyGetBeanParam bean);

@POST
@Consumes("application/x-www-form-urlencoded")
@Path("getPrivateField")
@Produces("text/plain")
public String echoPrivateField(@BeanParam MyBeanParamWithPrivateField bean);

@POST
@Consumes("application/x-www-form-urlencoded")
@Path("all/{pathParam}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -138,4 +138,14 @@ public void testSubResource() {

assertEquals("query", response);
}

@Test
public void testBeanParamPrivateFieldQuery() {
MyBeanParamWithPrivateField myGetBeanParam = new MyBeanParamWithPrivateField();
myGetBeanParam.setPrivateFieldParam("query");

String response = resourceWithBeanParam.echoPrivateField(myGetBeanParam);

assertEquals("query", response);
}
}

0 comments on commit c12c0dd

Please sign in to comment.