Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/main/java/feign/template/Expressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import feign.Util;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -154,6 +155,17 @@ protected String expand(Object variable, boolean encode) {
expanded.append(this.expandIterable((Iterable<?>) variable));
} else if (Map.class.isAssignableFrom(variable.getClass())) {
expanded.append(this.expandMap((Map<String, ?>) variable));
} else if (Optional.class.isAssignableFrom(variable.getClass())) {
Optional<?> optional = (Optional) variable;
if (optional.isPresent()) {
expanded.append(this.expand(optional.get(), encode));
} else {
if (!this.nameRequired) {
return null;
}
expanded.append(this.encode(this.getName()))
.append("=");
}
} else {
if (this.nameRequired) {
expanded.append(this.encode(this.getName()))
Expand Down
34 changes: 34 additions & 0 deletions spring4/src/test/java/feign/spring/SpringContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public void setup() throws IOException {
mockClient = new MockClient()
.noContent(HttpMethod.GET, "/health")
.noContent(HttpMethod.GET, "/health/1")
.noContent(HttpMethod.GET, "/health/optional")
.noContent(HttpMethod.GET, "/health/optional?param=value")
.noContent(HttpMethod.GET, "/health/optional?param")
.noContent(HttpMethod.GET, "/health/1?deep=true")
.noContent(HttpMethod.GET, "/health/1?deep=true&dryRun=true")
.noContent(HttpMethod.GET, "/health/name?deep=true&dryRun=true")
Expand Down Expand Up @@ -115,6 +118,34 @@ public void testWithName() {
mockClient.verifyOne(HttpMethod.GET, "/health/name?deep=true&dryRun=true");
}

@Test
public void testOptionalPresent() {
resource.checkWithOptional(Optional.of("value"));

mockClient.verifyOne(HttpMethod.GET, "/health/optional?param=value");
}

@Test
public void testOptionalNotPresent() {
resource.checkWithOptional(Optional.empty());

mockClient.verifyOne(HttpMethod.GET, "/health/optional");
}

@Test
public void testOptionalEmptyValue() {
resource.checkWithOptional(Optional.of(""));

mockClient.verifyOne(HttpMethod.GET, "/health/optional?param");
}

@Test
public void testOptionalNullable() {
resource.checkWithOptional(Optional.ofNullable(null));

mockClient.verifyOne(HttpMethod.GET, "/health/optional");
}

@Test
public void testRequestPart() {
resource.checkRequestPart("1", "hello", "6");
Expand Down Expand Up @@ -253,6 +284,9 @@ void checkWithName(
@RequestParam(name = "deep", defaultValue = "false") boolean deepCheck,
@RequestParam(name = "dryRun", defaultValue = "false") boolean dryRun);

@RequestMapping(value = "/optional", method = RequestMethod.GET)
void checkWithOptional(@RequestParam(name = "param") Optional<String> param);

@RequestMapping(value = "/part/{id}", method = RequestMethod.POST)
void checkRequestPart(@PathVariable(name = "id") String campaignId,
@RequestPart(name = "name1") String name,
Expand Down