Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate paths and check additionalPathTemplates #1522

Merged
merged 4 commits into from Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -95,10 +95,29 @@ 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
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
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