Skip to content

Commit

Permalink
Handle no project fields are selected on list
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Feb 25, 2016
1 parent 5fea7ce commit 926241a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class LocalResourceManagerHelper {
ImmutableSet.of("gzip", "x-gzip");
private static final Pattern LIST_FIELDS_PATTERN =
Pattern.compile("(.*?)projects\\((.*?)\\)(.*?)");
private static final String[] NO_FIELDS = {};

static {
try {
Expand Down Expand Up @@ -245,6 +246,9 @@ private static Map<String, Object> parseListOptions(String query) throws IOExcep
if (matcher.matches()) {
options.put("projectFields", matcher.group(2).split(","));
options.put("listFields", (matcher.group(1) + matcher.group(3)).split(","));
} else {
options.put("projectFields", NO_FIELDS);
options.put("listFields", argEntry[1].split(","));
}
break;
case "filter":
Expand Down Expand Up @@ -393,14 +397,23 @@ Response list(Map<String, Object> options) {
}
}
}
StringBuilder responseBody = new StringBuilder();
responseBody.append("{\"projects\": [");
Joiner.on(",").appendTo(responseBody, projectsSerialized);
responseBody.append(']');
String[] listFields = (String[]) options.get("listFields");
StringBuilder responseBody = new StringBuilder();
responseBody.append('{');
boolean commaNeeded = false;
// If fields parameter is set but no project field is selected we must return no projects.
if (!(projectFields != null && projectFields.length == 0)) {
responseBody.append("\"projects\": [");
Joiner.on(",").appendTo(responseBody, projectsSerialized);
responseBody.append(']');
commaNeeded = true;
}
if (nextPageToken != null && (listFields == null
|| ImmutableSet.copyOf(listFields).contains("nextPageToken"))) {
responseBody.append(", \"nextPageToken\": \"");
if (commaNeeded) {
responseBody.append(",");
}
responseBody.append("\"nextPageToken\": \"");
responseBody.append(nextPageToken);
responseBody.append('"');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,23 @@ public void testListNoPageTokenFieldOptions() {
assertFalse(iterator.hasNext());
}

@Test
public void testListPageTokenNoFieldsOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "nextPageToken");
rpc.create(PARTIAL_PROJECT);
rpc.create(COMPLETE_PROJECT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
assertNotNull(projects.x());
assertNull(projects.y());
rpcOptions.put(ResourceManagerRpc.Option.PAGE_TOKEN, projects.x());
projects = rpc.list(rpcOptions);
assertNull(projects.x());
assertNull(projects.y());
}

@Test
public void testListFilterOptions() {
Map<ResourceManagerRpc.Option, Object> rpcFilterOptions = new HashMap<>();
Expand Down

0 comments on commit 926241a

Please sign in to comment.