Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for private fields in @BeanParam #5525

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}