Skip to content

Commit

Permalink
fix: Collection models without links now deserialise correctly
Browse files Browse the repository at this point in the history
fixes #37
  • Loading branch information
toedter committed Aug 20, 2021
1 parent f6a1cc8 commit f3c8ba7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ protected JsonApiCollectionModelDeserializer(JavaType contentType, JsonApiConfig
@Override
protected CollectionModel<?> convertToRepresentationModel(List<Object> resources, JsonApiDocument doc) {
Links links = doc.getLinks();
if(links == null) {
return CollectionModel.of(resources);
}
return CollectionModel.of(resources, links);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,26 @@ void should_deserialize_movies_collection_model() throws Exception {
assertThat(links.getLink("self").get().getHref()).isEqualTo("http://localhost/movies");
}

@Test
void should_deserialize_movies_collection_model_without_links() throws Exception {
JavaType moviesCollectionModelType =
mapper.getTypeFactory().constructParametricType(CollectionModel.class, Movie.class);
File file = new ClassPathResource("moviesCollectionModelWithoutLinks.json", getClass()).getFile();
CollectionModel<Movie> movieCollectionModel = mapper.readValue(file, moviesCollectionModelType);
Collection<Movie> movieCollection = movieCollectionModel.getContent();

final Iterator<Movie> iterator = movieCollection.iterator();
Movie movie1 = iterator.next();
assertThat(movie1.getId()).isEqualTo("1");
assertThat(movie1.getTitle()).isEqualTo("Star Wars");
Movie movie2 = iterator.next();
assertThat(movie2.getId()).isEqualTo("2");
assertThat(movie2.getTitle()).isEqualTo("Avengers");

Links links = movieCollectionModel.getLinks();
assertThat(links.isEmpty()).isTrue();
}

@Test
void should_deserialize_movies_paged_model() throws Exception {
JavaType moviesPagedModelType =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data": [
{
"id": "1",
"type": "movies",
"attributes": {
"title": "Star Wars"
},
"links": {
"self": "http://localhost/movies/1"
}
},
{
"id": "2",
"type": "movies",
"attributes": {
"title": "Avengers"
},
"links": {
"self": "http://localhost/movies/2"
}
}
]
}

0 comments on commit f3c8ba7

Please sign in to comment.