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

fix inconsistent bug #1137

Merged
merged 1 commit into from
Dec 30, 2019
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
18 changes: 11 additions & 7 deletions core/src/main/java/feign/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method me

if (parameterTypes[i] == URI.class) {
data.urlIndex(i);
} else if (!isHttpAnnotation && parameterTypes[i] != Request.Options.class
&& !data.isAlreadyProcessed(i)) {
checkState(data.formParams().isEmpty(),
"Body parameters cannot be used with form parameters.");
checkState(data.bodyIndex() == null, "Method has too many Body parameters: %s", method);
data.bodyIndex(i);
data.bodyType(Types.resolve(targetType, targetType, genericParameterTypes[i]));
} else if (!isHttpAnnotation && parameterTypes[i] != Request.Options.class) {
if (data.isAlreadyProcessed(i)) {
checkState(data.formParams().isEmpty() || data.bodyIndex() == null,
"Body parameters cannot be used with form parameters.");
} else {
checkState(data.formParams().isEmpty(),
"Body parameters cannot be used with form parameters.");
checkState(data.bodyIndex() == null, "Method has too many Body parameters: %s", method);
data.bodyIndex(i);
data.bodyType(Types.resolve(targetType, targetType, genericParameterTypes[i]));
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions core/src/test/java/feign/DefaultContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,42 @@ public void formParamsParseIntoIndexToName() throws Exception {
entry(2, asList("password")));
}

@Test
public void formParamAndBodyParams() throws Exception {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Body parameters cannot be used with form parameters.");

parseAndValidateMetadata(FormParams.class,
"formParamAndBodyParams", String.class, String.class);
Fail.failBecauseExceptionWasNotThrown(IllegalStateException.class);
}

@Test
public void bodyParamsAndformParam() throws Exception {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Body parameters cannot be used with form parameters.");

parseAndValidateMetadata(FormParams.class,
"bodyParamsAndformParam", String.class, String.class);
Fail.failBecauseExceptionWasNotThrown(IllegalStateException.class);
}

@Test
public void formParamParseIntoFormParams() throws Exception {

MethodMetadata md = parseAndValidateMetadata(FormParams.class,
"loginNoBodyTemplate", String.class, String.class, String.class);

assertThat(md.formParams())
.containsExactly("customer_name", "user_name", "password");

assertThat(md.indexToName()).containsExactly(
entry(0, asList("customer_name")),
entry(1, asList("user_name")),
entry(2, asList("password")));
}


/**
* Body type is only for the body param.
*/
Expand Down Expand Up @@ -490,6 +526,22 @@ void login(
@Param("customer_name") String customer,
@Param("user_name") String user,
@Param("password") String password);

@RequestLine("POST /")
void loginNoBodyTemplate(
@Param("customer_name") String customer,
@Param("user_name") String user,
@Param("password") String password);

@RequestLine("POST /")
void formParamAndBodyParams(
@Param("customer_name") String customer,
String body);

@RequestLine("POST /")
void bodyParamsAndformParam(
String body,
@Param("customer_name") String customer);
}

interface HeaderMapInterface {
Expand Down