Skip to content

Commit 5173014

Browse files
lqiu96blakeli0
andauthored
fix: Validate paths and check additionalPathTemplates (#1522)
* fix: Validate paths and check additionalPathTemplates * chore: Update for PR comments * Update gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java Co-authored-by: Blake Li <[email protected]> * chore: Fix format issues --------- Co-authored-by: Blake Li <[email protected]>
1 parent ecfd4ff commit 5173014

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,30 @@ public String getRequestBody(RequestT apiMessage) {
9595
return requestBodyExtractor.extract(apiMessage);
9696
}
9797

98-
/* {@inheritDoc} */
98+
/**
99+
* Returns the relative URL path created from the path parameters from the given message. Attempts
100+
* to match the with the default PathTemplate. If there is not match, it attempts to match with
101+
* the templates in the additionalPathTemplates.
102+
*
103+
* @param apiMessage Request object to extract fields from
104+
* @return Path of a matching valid URL or the default Path URL
105+
*/
99106
@Override
100107
public String getPath(RequestT apiMessage) {
101-
return pathTemplate.instantiate(pathVarsExtractor.extract(apiMessage));
108+
Map<String, String> pathVarsMap = pathVarsExtractor.extract(apiMessage);
109+
String path = pathTemplate.instantiate(pathVarsMap);
110+
if (pathTemplate.matches(path)) {
111+
return path;
112+
}
113+
for (PathTemplate additionalPathTemplate : additionalPathTemplates) {
114+
String additionalPath = additionalPathTemplate.instantiate(pathVarsMap);
115+
if (additionalPathTemplate.matches(additionalPath)) {
116+
return additionalPath;
117+
}
118+
}
119+
// If there are no matches, we return the default path, this is for backwards compatibility.
120+
// TODO: Log this scenario once we implemented the Cloud SDK logging.
121+
return path;
102122
}
103123

104124
@BetaApi

gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatterTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ public void getPath() {
131131
Truth.assertThat(path).isEqualTo("api/v1/names/field_name1/aggregated");
132132
}
133133

134+
@Test
135+
public void getPath_additionalPaths() {
136+
Field fieldWithLongerName = field.toBuilder().setName("field_name1/random_text").build();
137+
String path = formatter.getPath(fieldWithLongerName);
138+
Truth.assertThat(path).isEqualTo("api/v1/names/field_name1/random_text/aggregated");
139+
140+
Field fieldWithRandomValues =
141+
field.toBuilder().setName("field_name1/random_text/random_text1").build();
142+
path = formatter.getPath(fieldWithRandomValues);
143+
Truth.assertThat(path)
144+
.isEqualTo("api/v1/names/field_name1/random_text/random_text1/aggregated");
145+
}
146+
147+
@Test
148+
public void getPath_noMatches() {
149+
// If there are no valid matches, it will return with the default path's url
150+
Field fieldNotMatching = field.toBuilder().setName("name_does_not_match").build();
151+
String path = formatter.getPath(fieldNotMatching);
152+
Truth.assertThat(path).isEqualTo("api/v1/names/name_does_not_match/aggregated");
153+
}
154+
134155
@Test
135156
public void getPathTemplate() {
136157
String path =

0 commit comments

Comments
 (0)