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

Commit 878bcf2

Browse files
authored
fix: Build routing header params map with the last entry if multiple 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.
1 parent bd8ee04 commit 878bcf2

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

gax/src/main/java/com/google/api/gax/rpc/RequestParamsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public void add(String fieldValue, String headerKey, PathTemplate pathTemplate)
7373
}
7474

7575
public Map<String, String> build() {
76-
return paramsBuilder.build();
76+
return paramsBuilder.buildKeepingLast();
7777
}
7878
}

gax/src/test/java/com/google/api/gax/rpc/RequestParamsBuilderTest.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,42 +51,61 @@ public void setUp() {
5151

5252
@Test
5353
public void add_happyPath() {
54-
String headerKey = "table_location";
5554
Map<String, String> actual =
5655
getRoutingHeaders(
57-
headerKey,
5856
"projects/**/{table_location=instances/*}",
5957
"projects/my_cozy_home/instances/living_room");
60-
assertThat(actual).containsExactly(headerKey, "instances/living_room");
58+
assertThat(actual).containsExactly("table_location", "instances/living_room");
59+
}
60+
61+
@Test
62+
public void build_shouldKeepLastEntryIfMultipleEntriesHaveTheSameKeyRatherThanErrorOut() {
63+
requestParamsBuilder.add(
64+
"projects/my_cozy_home/instances/living_room",
65+
"table_location",
66+
PathTemplate.create("projects/**/{table_location=instances/*}"));
67+
requestParamsBuilder.add(
68+
"projects/my_cozy_home/instances/living_room",
69+
"table_location",
70+
PathTemplate.create("{table_location=**}"));
71+
requestParamsBuilder.add(
72+
"projects/my_cozy_home/instances/living_room",
73+
"routing_id",
74+
PathTemplate.create("{routing_id=**}"));
75+
Map<String, String> actual = requestParamsBuilder.build();
76+
77+
// Should contain two entries instead of three, also should only keep the last entry if there
78+
// are multiple entries with the same key
79+
assertThat(actual)
80+
.containsExactly(
81+
"table_location",
82+
"projects/my_cozy_home/instances/living_room",
83+
"routing_id",
84+
"projects/my_cozy_home/instances/living_room");
6185
}
6286

6387
@Test
6488
public void add_matchedValuesWithNoRoutingHeaderKey() {
65-
Map<String, String> actual =
66-
getRoutingHeaders("table_location", "projects/**", "projects/my_cozy_home/");
89+
Map<String, String> actual = getRoutingHeaders("projects/**", "projects/my_cozy_home/");
6790
assertThat(actual).isEmpty();
6891
}
6992

7093
@Test
7194
public void add_emptyMatchedValues() {
7295
Map<String, String> actual =
73-
getRoutingHeaders(
74-
"table_location",
75-
"projects/**/{table_location=instances/*}",
76-
"projects/does_not_matter");
96+
getRoutingHeaders("projects/**/{table_location=instances/*}", "projects/does_not_matter");
7797
assertThat(actual).isEmpty();
7898
}
7999

80100
@Test
81101
public void add_nullFieldValue() {
82-
Map<String, String> actual = getRoutingHeaders("table_location", "projects/**", null);
102+
Map<String, String> actual = getRoutingHeaders("projects/**", null);
83103
assertThat(actual).isEmpty();
84104
}
85105

86-
private Map<String, String> getRoutingHeaders(
87-
String headerKey, String patternString, String fieldValue) {
106+
private Map<String, String> getRoutingHeaders(String patternString, String fieldValue) {
88107
PathTemplate pathTemplate = PathTemplate.create(patternString);
89-
requestParamsBuilder.add(fieldValue, headerKey, pathTemplate);
108+
requestParamsBuilder.add(fieldValue, "table_location", pathTemplate);
90109
return requestParamsBuilder.build();
91110
}
92111
}

0 commit comments

Comments
 (0)