diff --git a/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java b/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java index fa3480ad23..c9153e4b26 100644 --- a/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java +++ b/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java @@ -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 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 diff --git a/gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatterTest.java b/gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatterTest.java index 8f43231c1b..cc4c9e4a40 100644 --- a/gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatterTest.java +++ b/gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatterTest.java @@ -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 =