File tree 2 files changed +43
-2
lines changed
gax-java/gax-httpjson/src
main/java/com/google/api/gax/httpjson
test/java/com/google/api/gax/httpjson
2 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -95,10 +95,30 @@ public String getRequestBody(RequestT apiMessage) {
95
95
return requestBodyExtractor .extract (apiMessage );
96
96
}
97
97
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
+ */
99
106
@ Override
100
107
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 ;
102
122
}
103
123
104
124
@ BetaApi
Original file line number Diff line number Diff line change @@ -131,6 +131,27 @@ public void getPath() {
131
131
Truth .assertThat (path ).isEqualTo ("api/v1/names/field_name1/aggregated" );
132
132
}
133
133
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
+
134
155
@ Test
135
156
public void getPathTemplate () {
136
157
String path =
You can’t perform that action at this time.
0 commit comments