Skip to content

Commit f821a93

Browse files
authored
Refactored Header and Query parameter JAXRS Contract Parsing (#896)
As part of 10.x, the `headers()` and `queries()` collections on the `RequestTemplate` were made read only. The `JAXRSContract` was still attempting to manipulate those directly. THis was missed due to a use case not accounted for in the tests. I've added the appropriate use case and corrected the usage of the template.
1 parent 4a5d1c2 commit f821a93

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

jaxrs/src/main/java/feign/jaxrs/JAXRSContract.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data,
154154
String name = QueryParam.class.cast(parameterAnnotation).value();
155155
checkState(emptyToNull(name) != null, "QueryParam.value() was empty on parameter %s",
156156
paramIndex);
157-
Collection<String> query = addTemplatedParam(data.template().queries().get(name), name);
157+
String query = addTemplatedParam(name);
158158
data.template().query(name, query);
159159
nameParam(data, name, paramIndex);
160160
isHttpParam = true;
161161
} else if (annotationType == HeaderParam.class) {
162162
String name = HeaderParam.class.cast(parameterAnnotation).value();
163163
checkState(emptyToNull(name) != null, "HeaderParam.value() was empty on parameter %s",
164164
paramIndex);
165-
Collection<String> header = addTemplatedParam(data.template().headers().get(name), name);
165+
String header = addTemplatedParam(name);
166166
data.template().header(name, header);
167167
nameParam(data, name, paramIndex);
168168
isHttpParam = true;
@@ -179,11 +179,7 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data,
179179
}
180180

181181
// Not using override as the super-type's method is deprecated and will be removed.
182-
protected Collection<String> addTemplatedParam(Collection<String> possiblyNull, String name) {
183-
if (possiblyNull == null) {
184-
possiblyNull = new ArrayList<String>();
185-
}
186-
possiblyNull.add(String.format("{%s}", name));
187-
return possiblyNull;
182+
private String addTemplatedParam(String name) {
183+
return String.format("{%s}", name);
188184
}
189185
}

jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package feign.jaxrs;
1515

1616
import java.util.ArrayList;
17+
import java.util.Arrays;
1718
import java.util.Collections;
1819
import org.junit.Rule;
1920
import org.junit.Test;
@@ -394,6 +395,18 @@ public void methodPathWithoutLeadingSlashParsesCorrectly() throws Exception {
394395
.hasUrl("/base/specific");
395396
}
396397

398+
399+
@Test
400+
public void producesWithHeaderParamContainAllHeaders() throws Exception {
401+
assertThat(parseAndValidateMetadata(MixedAnnotations.class, "getWithHeaders",
402+
String.class, String.class, String.class)
403+
.template())
404+
.hasHeaders(entry("Accept", Arrays.asList("{Accept}", "application/json")))
405+
.hasQueries(
406+
entry("multiple", Arrays.asList("stuff", "{multiple}")),
407+
entry("another", Collections.singletonList("{another}")));
408+
}
409+
397410
interface Methods {
398411

399412
@POST
@@ -638,4 +651,14 @@ private MethodMetadata parseAndValidateMetadata(Class<?> targetType,
638651
return contract.parseAndValidateMetadata(targetType,
639652
targetType.getMethod(method, parameterTypes));
640653
}
654+
655+
interface MixedAnnotations {
656+
657+
@GET
658+
@Path("/api/stuff?multiple=stuff")
659+
@Produces("application/json")
660+
Response getWithHeaders(@HeaderParam("Accept") String accept,
661+
@QueryParam("multiple") String multiple,
662+
@QueryParam("another") String another);
663+
}
641664
}

0 commit comments

Comments
 (0)