Skip to content

Commit

Permalink
fix: Validate paths and check additionalPathTemplates (#1522)
Browse files Browse the repository at this point in the history
* 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 <blakeli@google.com>

* chore: Fix format issues

---------

Co-authored-by: Blake Li <blakeli@google.com>
  • Loading branch information
lqiu96 and blakeli0 committed Mar 27, 2023
1 parent ecfd4ff commit 5173014
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Expand Up @@ -95,10 +95,30 @@ public String getRequestBody(RequestT apiMessage) {
return requestBodyExtractor.extract(apiMessage);
}

/* {@inheritDoc} */
/**
* Returns the relative URL path created from the path parameters from the given message. Attempts
* to match the with the default PathTemplate. If there is not match, it attempts to match with
* the templates in the additionalPathTemplates.
*
* @param apiMessage Request object to extract fields from
* @return Path of a matching valid URL or the default Path URL
*/
@Override
public String getPath(RequestT apiMessage) {
return pathTemplate.instantiate(pathVarsExtractor.extract(apiMessage));
Map<String, String> pathVarsMap = pathVarsExtractor.extract(apiMessage);
String path = pathTemplate.instantiate(pathVarsMap);
if (pathTemplate.matches(path)) {
return path;
}
for (PathTemplate additionalPathTemplate : additionalPathTemplates) {
String additionalPath = additionalPathTemplate.instantiate(pathVarsMap);
if (additionalPathTemplate.matches(additionalPath)) {
return additionalPath;
}
}
// If there are no matches, we return the default path, this is for backwards compatibility.
// TODO: Log this scenario once we implemented the Cloud SDK logging.
return path;
}

@BetaApi
Expand Down
Expand Up @@ -131,6 +131,27 @@ public void getPath() {
Truth.assertThat(path).isEqualTo("api/v1/names/field_name1/aggregated");
}

@Test
public void getPath_additionalPaths() {
Field fieldWithLongerName = field.toBuilder().setName("field_name1/random_text").build();
String path = formatter.getPath(fieldWithLongerName);
Truth.assertThat(path).isEqualTo("api/v1/names/field_name1/random_text/aggregated");

Field fieldWithRandomValues =
field.toBuilder().setName("field_name1/random_text/random_text1").build();
path = formatter.getPath(fieldWithRandomValues);
Truth.assertThat(path)
.isEqualTo("api/v1/names/field_name1/random_text/random_text1/aggregated");
}

@Test
public void getPath_noMatches() {
// If there are no valid matches, it will return with the default path's url
Field fieldNotMatching = field.toBuilder().setName("name_does_not_match").build();
String path = formatter.getPath(fieldNotMatching);
Truth.assertThat(path).isEqualTo("api/v1/names/name_does_not_match/aggregated");
}

@Test
public void getPathTemplate() {
String path =
Expand Down

0 comments on commit 5173014

Please sign in to comment.