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

Add Optional BeanParam support for JAXRS2Contract #1935

Merged
merged 13 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 2 additions & 1 deletion jaxrs/src/main/java/feign/jaxrs/JAXRSContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ protected void registerParamAnnotations() {
}

// Not using override as the super-type's method is deprecated and will be removed.
private String addTemplatedParam(String name) {
// Protected so JAXRS2Contract can make use of this
protected String addTemplatedParam(String name) {
return String.format("{%s}", name);
}
}
2 changes: 2 additions & 0 deletions jaxrs2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ Links the value of the corresponding parameter to a query parameter. When invok
Links the value of the corresponding parameter to a header.
#### `@FormParam`
Links the value of the corresponding parameter to a key passed to `Encoder.Text<Map<String, Object>>.encode()`.
#### `@BeanParm`
Aggregates the above supported parameter annotations under a single value object.
18 changes: 12 additions & 6 deletions jaxrs2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<artifactId>feign-core</artifactId>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-jaxrs</artifactId>
Expand All @@ -53,12 +59,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>

<!-- for example -->
<dependency>
<groupId>${project.groupId}</groupId>
Expand All @@ -77,6 +77,12 @@
<artifactId>feign-jaxrs</artifactId>
<type>test-jar</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
69 changes: 66 additions & 3 deletions jaxrs2/src/main/java/feign/jaxrs2/JAXRS2Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package feign.jaxrs2;

import javax.ws.rs.BeanParam;
import feign.jaxrs.JAXRSContract;
import javax.ws.rs.*;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import feign.jaxrs.JAXRSContract;
import java.lang.reflect.Field;
import static feign.Util.checkState;
import static feign.Util.emptyToNull;

/**
* Please refer to the <a href="https://github.com/Netflix/feign/tree/master/feign-jaxrs2">Feign
Expand All @@ -31,7 +34,67 @@ public JAXRS2Contract() {
// https://github.com/OpenFeign/feign/issues/669
super.registerParameterAnnotation(Suspended.class, (ann, data, i) -> data.ignoreParamater(i));
super.registerParameterAnnotation(Context.class, (ann, data, i) -> data.ignoreParamater(i));
super.registerParameterAnnotation(BeanParam.class, (ann, data, i) -> data.ignoreParamater(i));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, before made sense to ignore any @BeanParam cause it was not implemented.

Now that you fixed that shortcoming, I think it's fine to have it enabled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you would like me to make this change or if you plan to make it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is a nice to have, but your call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed this change. Jdk8 appears to be failing again in CI. Not able to duplicate on my local


}

@Override
protected void registerParamAnnotations() {
super.registerParamAnnotations();

registerParameterAnnotation(BeanParam.class, (param, data, paramIndex) -> {
final Field[] aggregatedParams = data.method()
.getParameters()[paramIndex]
.getType()
.getDeclaredFields();

for (Field aggregatedParam : aggregatedParams) {

if (aggregatedParam.isAnnotationPresent(PathParam.class)) {
final String name = aggregatedParam.getAnnotation(PathParam.class).value();
JKomoroski marked this conversation as resolved.
Show resolved Hide resolved
checkState(
emptyToNull(name) != null,
"BeanParam parameter %s contains PathParam with empty .value() on field %s",
paramIndex,
aggregatedParam.getName());
nameParam(data, name, paramIndex);
}

if (aggregatedParam.isAnnotationPresent(QueryParam.class)) {
final String name = aggregatedParam.getAnnotation(QueryParam.class).value();
checkState(
emptyToNull(name) != null,
"BeanParam parameter %s contains QueryParam with empty .value() on field %s",
paramIndex,
aggregatedParam.getName());
final String query = addTemplatedParam(name);
data.template().query(name, query);
nameParam(data, name, paramIndex);
}

if (aggregatedParam.isAnnotationPresent(HeaderParam.class)) {
final String name = aggregatedParam.getAnnotation(HeaderParam.class).value();
checkState(
emptyToNull(name) != null,
"BeanParam parameter %s contains HeaderParam with empty .value() on field %s",
paramIndex,
aggregatedParam.getName());
final String header = addTemplatedParam(name);
data.template().header(name, header);
nameParam(data, name, paramIndex);
}

if (aggregatedParam.isAnnotationPresent(FormParam.class)) {
final String name = aggregatedParam.getAnnotation(FormParam.class).value();
checkState(
emptyToNull(name) != null,
"BeanParam parameter %s contains FormParam with empty .value() on field %s",
paramIndex,
aggregatedParam.getName());
data.formParams().add(name);
nameParam(data, name, paramIndex);
}
}
});

}
}
60 changes: 53 additions & 7 deletions jaxrs2/src/test/java/feign/jaxrs2/JAXRS2ContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
*/
package feign.jaxrs2;

import static feign.assertj.FeignAssertions.assertThat;
import org.assertj.core.data.MapEntry;
import feign.MethodMetadata;
import feign.jaxrs.JAXRSContract;
import feign.jaxrs.JAXRSContractTest;
import feign.jaxrs2.JAXRS2ContractTest.Jaxrs2BeanParamInternals.BeanParamInput;
import feign.jaxrs2.JAXRS2ContractTest.Jaxrs2Internals.Input;
import org.junit.Test;
import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import feign.MethodMetadata;
import feign.jaxrs.JAXRSContract;
import feign.jaxrs.JAXRSContractTest;
import feign.jaxrs2.JAXRS2ContractTest.Jaxrs2Internals.Input;
import static feign.assertj.FeignAssertions.assertThat;
import static java.util.Arrays.asList;
import static org.assertj.core.data.MapEntry.entry;

/**
* Tests interfaces defined per {@link JAXRS2Contract} are interpreted into expected
Expand All @@ -47,13 +49,57 @@ public void injectJaxrsInternals() throws Exception {
}

@Test
public void injectBeanParam() throws Exception {
public void injectWithoutBeanParam() throws Exception {
final MethodMetadata methodMetadata =
parseAndValidateMetadata(Jaxrs2Internals.class, "beanParameters", Input.class);
assertThat(methodMetadata.template())
.noRequestBody();
}

@Test
public void injectBeanParam() throws Exception {
final MethodMetadata methodMetadata =
parseAndValidateMetadata(Jaxrs2BeanParamInternals.class, "beanParameters",
BeanParamInput.class);
assertThat(methodMetadata.template())
.noRequestBody();

assertThat(methodMetadata.template())
.hasHeaders(entry("X-Custom-Header", asList("{X-Custom-Header}")));
assertThat(methodMetadata.template())
.hasQueries(entry("query", asList("{query}")));
assertThat(methodMetadata.formParams())
.isNotEmpty()
.containsExactly("form");

}

public interface Jaxrs2BeanParamInternals {
@GET
@Path("/")
void inject(@Suspended AsyncResponse ar, @Context UriInfo info);

@Path("/{path}")
@POST
void beanParameters(@BeanParam BeanParamInput beanParam);

public class BeanParamInput {

@PathParam("path")
String path;

@QueryParam("query")
String query;

@FormParam("form")
String form;

@HeaderParam("X-Custom-Header")
String header;

}
}


@Path("/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, the compile time error moved to this line.... probably some jaxrs 1 leaking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to install jdk 8 again to check for these issues

public interface Jaxrs2Internals {
Expand Down