Skip to content

Commit

Permalink
Fix page metadata not being present on collections
Browse files Browse the repository at this point in the history
  • Loading branch information
vierbergenlars committed Aug 21, 2023
1 parent bf8ce7d commit b6f4e5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ public CollectionModel<?> process(CollectionModel<?> model) {
return wrappers.wrap(content, IanaLinkRelations.ITEM);
});

return createModelCopy(model, List.of(wrapper))
.add(model.getLinks())
.withFallbackType(model.getResolvableType());
return createModelCopy(model, List.of(wrapper));
}

private CollectionModel<?> createModelCopy(CollectionModel<?> model, Iterable<?> content) {
private CollectionModel<?> createModelCopy(CollectionModel<?> model, Collection<?> content) {
if (model instanceof PagedModel<?> pagedModel) {
return new PageModel<>(content, pagedModel.getMetadata());
return PagedModel.of(content, pagedModel.getMetadata(), model.getLinks());
} else {
return CollectionModel.of(content);
return CollectionModel.of(content, model.getLinks());
}
}

Expand All @@ -79,45 +77,11 @@ private Optional<EmbeddedWrapper> findEmptyWrapper(Collection<?> collection) {
return Optional.empty();
}

private Collection<EmbeddedWrapper> ensureWrapped(Collection<?> content) {
List<EmbeddedWrapper> allWrappers = new ArrayList<>();
List<Object> unwrapped = new ArrayList<>();
for (Object item : content) {
if (item instanceof EmbeddedWrapper wrapper) {
allWrappers.add(wrapper);
} else {
unwrapped.add(item);
}
}

if (!unwrapped.isEmpty()) {
allWrappers.add(wrappers.wrap(unwrapped, IanaLinkRelations.ITEM));
}

return allWrappers;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

private static class PageModel<T> extends CollectionModel<T> {

private final PageMetadata pageMetadata;

private PageModel(Iterable<T> content, PageMetadata pageMetadata) {
super(content);
this.pageMetadata = pageMetadata;
}

@JsonProperty("page")
@Nullable
public PageMetadata getMetadata() {
return pageMetadata;
}
}

@RequiredArgsConstructor
private static class ForcedLinkRelEmbeddedWrapper implements EmbeddedWrapper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ void listInvoices_returns_http200_ok() throws Exception {
mockMvc.perform(get("/invoices")
.contentType("application/json"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page.size").value(20))
.andExpect(jsonPath("$.page.totalElements").value(2))
.andExpect(jsonPath("$.page.totalPages").value(1))
.andExpect(jsonPath("$.page.number").value(0))
.andExpect(jsonPath("$._embedded.['item'].length()").value(2))
.andExpect(jsonPath("$._embedded.['item'][0].number").exists())
.andExpect(jsonPath("$._links.self.href").value("http://localhost/invoices?page=0&size=20"));
Expand All @@ -152,6 +156,10 @@ void listInvoices_returns_http200_ok() throws Exception {
void listRefunds_returns_http200_ok() throws Exception {
mockMvc.perform(get("/refunds").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page.size").value(20))
.andExpect(jsonPath("$.page.totalElements").value(0))
.andExpect(jsonPath("$.page.totalPages").value(0))
.andExpect(jsonPath("$.page.number").value(0))
.andExpect(jsonPath("$._embedded.['item'].length()").value(0))
.andExpect(jsonPath("$._links.self.href").value("http://localhost/refunds?page=0&size=20"));
}
Expand Down

0 comments on commit b6f4e5c

Please sign in to comment.