Skip to content

Commit

Permalink
Support Conditional Parameter Expansion (#2216)
Browse files Browse the repository at this point in the history
  • Loading branch information
gromspys authored Oct 26, 2023
1 parent b899595 commit 87179ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 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,16 @@ 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())).append("=");
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 @@ -55,6 +55,9 @@ public void setup() throws IOException {
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 @@ -117,6 +120,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 @@ -250,6 +281,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,
Expand Down

0 comments on commit 87179ca

Please sign in to comment.