Skip to content

Commit

Permalink
Handle null support policy values from Contentful
Browse files Browse the repository at this point in the history
Fixes gh-18
  • Loading branch information
mbhave committed Nov 17, 2023
1 parent 93e3f0a commit 6d39a52
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.projectapi.contentful;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

Expand Down Expand Up @@ -76,7 +77,13 @@ private ClientGraphQlResponse executeForSingleProject(String documentName, Strin
}

private <T> List<T> fieldToEntityList(ClientGraphQlResponse response, String path, Class<T> type) {
return field(response, path, (field) -> field.toEntityList(type));
return field(response, path, (field) -> getList(type, response, path));
}

private static <T> List<T> getList(Class<T> type, ClientGraphQlResponse response, String path) {
ClientResponseField field = response.field(path);
return (!field.hasValue() && response.getErrors().isEmpty()) ? Collections.emptyList()
: field.toEntityList(type);
}

private <T> T fieldToEntity(ClientGraphQlResponse response, String path, Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ void getProjectSupportsWhenNoProjectMatchThrowsException() throws IOException {
.satisfies((ex) -> assertThat(ex.getProjectSlug()).isEqualTo("spring-xd"));
}

@Test
void getProjectSupportsWhenNullReturnsEmptyList() throws IOException {
setupResponse("query-null-support.json");
List<ProjectSupport> supports = this.contentfulQueries.getProjectSupports("spring-xd");
assertThat(supports).isEmpty();
}

private void setupResponse(String name) throws IOException {
setupResponse(new ClassPathResource(name, getClass()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{
"data": {
}
},
"errors": [
{
"message": "Cannot query field \"a\" on type \"Project\".",
"locations": [
{
"line": 4,
"column": 4
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"projectCollection": {
"items": [
{
"support": null
}
]
}
}
}

0 comments on commit 6d39a52

Please sign in to comment.