diff --git a/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/links/SpringDataEmbeddedItemResourceProcessor.java b/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/links/SpringDataEmbeddedItemResourceProcessor.java index 1bf692c2..a5f9d487 100644 --- a/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/links/SpringDataEmbeddedItemResourceProcessor.java +++ b/contentgrid-spring-data-rest/src/main/java/com/contentgrid/spring/data/rest/links/SpringDataEmbeddedItemResourceProcessor.java @@ -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()); } } @@ -79,45 +77,11 @@ private Optional findEmptyWrapper(Collection collection) { return Optional.empty(); } - private Collection ensureWrapped(Collection content) { - List allWrappers = new ArrayList<>(); - List 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 extends CollectionModel { - - private final PageMetadata pageMetadata; - - private PageModel(Iterable content, PageMetadata pageMetadata) { - super(content); - this.pageMetadata = pageMetadata; - } - - @JsonProperty("page") - @Nullable - public PageMetadata getMetadata() { - return pageMetadata; - } - } - @RequiredArgsConstructor private static class ForcedLinkRelEmbeddedWrapper implements EmbeddedWrapper { diff --git a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java index 33fb91fd..e8cf62d5 100644 --- a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java +++ b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java @@ -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")); @@ -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")); }