Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
fix: Build routing header params map with the last entry if multiple …
Browse files Browse the repository at this point in the history
…entries have the same key. (#1729)

fixes: #1728
Guava ImmutableMap.Builder.build() will error out instead of replacing with the last entry if there are multiple entries with the same key, use `buildKeepingLast` instead of `build` to fix this issue.
  • Loading branch information
blakeli0 authored Jul 8, 2022
1 parent bd8ee04 commit 878bcf2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public void add(String fieldValue, String headerKey, PathTemplate pathTemplate)
}

public Map<String, String> build() {
return paramsBuilder.build();
return paramsBuilder.buildKeepingLast();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,61 @@ public void setUp() {

@Test
public void add_happyPath() {
String headerKey = "table_location";
Map<String, String> actual =
getRoutingHeaders(
headerKey,
"projects/**/{table_location=instances/*}",
"projects/my_cozy_home/instances/living_room");
assertThat(actual).containsExactly(headerKey, "instances/living_room");
assertThat(actual).containsExactly("table_location", "instances/living_room");
}

@Test
public void build_shouldKeepLastEntryIfMultipleEntriesHaveTheSameKeyRatherThanErrorOut() {
requestParamsBuilder.add(
"projects/my_cozy_home/instances/living_room",
"table_location",
PathTemplate.create("projects/**/{table_location=instances/*}"));
requestParamsBuilder.add(
"projects/my_cozy_home/instances/living_room",
"table_location",
PathTemplate.create("{table_location=**}"));
requestParamsBuilder.add(
"projects/my_cozy_home/instances/living_room",
"routing_id",
PathTemplate.create("{routing_id=**}"));
Map<String, String> actual = requestParamsBuilder.build();

// Should contain two entries instead of three, also should only keep the last entry if there
// are multiple entries with the same key
assertThat(actual)
.containsExactly(
"table_location",
"projects/my_cozy_home/instances/living_room",
"routing_id",
"projects/my_cozy_home/instances/living_room");
}

@Test
public void add_matchedValuesWithNoRoutingHeaderKey() {
Map<String, String> actual =
getRoutingHeaders("table_location", "projects/**", "projects/my_cozy_home/");
Map<String, String> actual = getRoutingHeaders("projects/**", "projects/my_cozy_home/");
assertThat(actual).isEmpty();
}

@Test
public void add_emptyMatchedValues() {
Map<String, String> actual =
getRoutingHeaders(
"table_location",
"projects/**/{table_location=instances/*}",
"projects/does_not_matter");
getRoutingHeaders("projects/**/{table_location=instances/*}", "projects/does_not_matter");
assertThat(actual).isEmpty();
}

@Test
public void add_nullFieldValue() {
Map<String, String> actual = getRoutingHeaders("table_location", "projects/**", null);
Map<String, String> actual = getRoutingHeaders("projects/**", null);
assertThat(actual).isEmpty();
}

private Map<String, String> getRoutingHeaders(
String headerKey, String patternString, String fieldValue) {
private Map<String, String> getRoutingHeaders(String patternString, String fieldValue) {
PathTemplate pathTemplate = PathTemplate.create(patternString);
requestParamsBuilder.add(fieldValue, headerKey, pathTemplate);
requestParamsBuilder.add(fieldValue, "table_location", pathTemplate);
return requestParamsBuilder.build();
}
}

0 comments on commit 878bcf2

Please sign in to comment.