Skip to content

Commit 1b9113a

Browse files
committed
Handle next links as-is to avoid double encoding
Fixes gh-111
1 parent b082301 commit 1b9113a

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Diff for: src/main/java/io/spring/githubchangeloggenerator/github/service/GitHubService.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package io.spring.githubchangeloggenerator.github.service;
1818

1919
import java.lang.reflect.Array;
20+
import java.net.URI;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.List;
@@ -103,7 +104,17 @@ private <T> Page<T> getPage(Class<T> type, String url, Object... uriVariables) {
103104
if (!StringUtils.hasText(url)) {
104105
return null;
105106
}
106-
ResponseEntity<T[]> response = this.restTemplate.getForEntity(url, arrayType(type), uriVariables);
107+
return pageFrom(this.restTemplate.getForEntity(url, arrayType(type), uriVariables), type);
108+
}
109+
110+
private <T> Page<T> getPage(Class<T> type, URI uri) {
111+
if (uri == null) {
112+
return null;
113+
}
114+
return pageFrom(this.restTemplate.getForEntity(uri, arrayType(type)), type);
115+
}
116+
117+
private <T> Page<T> pageFrom(ResponseEntity<T[]> response, Class<T> type) {
107118
return new Page<>(Arrays.asList(response.getBody()), () -> getPage(type, getNextUrl(response.getHeaders())));
108119
}
109120

@@ -112,12 +123,12 @@ private <T> Class<T[]> arrayType(Class<T> elementType) {
112123
return (Class<T[]>) Array.newInstance(elementType, 0).getClass();
113124
}
114125

115-
private String getNextUrl(HttpHeaders headers) {
126+
private URI getNextUrl(HttpHeaders headers) {
116127
String links = headers.getFirst("Link");
117128
for (String link : StringUtils.commaDelimitedListToStringArray(links)) {
118129
Matcher matcher = LINK_PATTERN.matcher(link.trim());
119130
if (matcher.matches() && "next".equals(matcher.group(2))) {
120-
return matcher.group(1);
131+
return URI.create(matcher.group(1));
121132
}
122133
}
123134
return null;

Diff for: src/test/java/io/spring/githubchangeloggenerator/github/service/GitHubServiceTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -107,10 +107,10 @@ void getIssuesWhenSinglePageOfIssuesPresent() {
107107
@Test
108108
void getIssuesWhenMultiplePagesOfIssuesPresent() {
109109
HttpHeaders headers = new HttpHeaders();
110-
headers.set("Link", "<page-two>; rel=\"next\"");
110+
headers.set("Link", "</page-two%3D>; rel=\"next\"");
111111
expectGet(ISSUES_URL + "23&state=closed")
112112
.andRespond(withJsonFrom("closed-issues-for-milestone-page-1.json").headers(headers));
113-
expectGet("/page-two").andRespond(withJsonFrom("closed-issues-for-milestone-page-2.json"));
113+
expectGet("/page-two%3D").andRespond(withJsonFrom("closed-issues-for-milestone-page-2.json"));
114114
List<Issue> issues = this.service.getIssuesForMilestone(23, Repository.of("org/repo"));
115115
assertThat(issues.size()).isEqualTo(60);
116116
}

0 commit comments

Comments
 (0)