Skip to content

Commit

Permalink
Fix pagination bug by replacing params if they already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
tdonohue committed Apr 17, 2020
1 parent c4ab6e7 commit fbd8757
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public EmbeddedPageHeader(UriComponentsBuilder self, Page page) {
this(self, page, true);
}

/**
* Build the "page" element with all valid pagination information (number, size, totalPages, totalElements)
* @return Map that will be used to build the JSON of the "page" element
*/
@JsonProperty(value = "page")
public Map<String, Long> getPageInfo() {
Map<String, Long> pageInfo = new HashMap<String, Long>();
Expand All @@ -53,6 +57,10 @@ public Map<String, Long> getPageInfo() {
return pageInfo;
}

/**
* Build the "_links" element with all valid pagination links (first, next, prev, last)
* @return Map that will be used to build the JSON of the "_links" element
*/
@JsonProperty(value = "_links")
public Map<String, Object> getLinks() {
Map<String, Object> links = new HashMap<>();
Expand All @@ -74,23 +82,34 @@ public Map<String, Object> getLinks() {
return links;
}

private Href _link(final Sort sort, Integer i, int size) {
/**
* Builds a single HREF link element within the "_links" section
* <P>
* (e.g. "next" : { "href": "[next-link]" } )
* @param sort current sort
* @param page page param for this link
* @param size size param for this link
* @return Href representing the link
*/
private Href _link(final Sort sort, Integer page, int size) {
UriComponentsBuilder uriComp = self.cloneBuilder();
if (sort != null) {
for (Sort.Order order : sort) {
uriComp = uriComp.queryParam("sort", order.getProperty() + "," + order.getDirection());
// replace existing sort param (if exists), otherwise append it
uriComp = uriComp.replaceQueryParam("sort", order.getProperty() + "," + order.getDirection());
}
}
if (i != null) {
uriComp = uriComp.queryParam("page", i);
uriComp = uriComp.queryParam("size", size);
if (page != null) {
// replace existing page & size params (if exist), otherwise append them
uriComp = uriComp.replaceQueryParam("page", page);
uriComp = uriComp.replaceQueryParam("size", size);
}
return new Href(uriComp.build().toUriString());
}

/**
* Represents an individual "_link", including its HREF property.
* (e.g. "next" : { "href": "[next-link]" } )
* Represents a single HREF property for an single link
* (e.g. { "href": "[full-link-url]" } )
* <P>
* NOTE: This inner class is protected to allow for easier unit testing
*/
Expand Down

0 comments on commit fbd8757

Please sign in to comment.