Skip to content

Commit 5b19f62

Browse files
committed
Refactoring in ApiVersionInserter
Refine naming of static factory methods, and update them to be shortcuts for instance creation. See gh-34919
1 parent f4f0e52 commit 5b19f62

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ApiVersionTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public class ApiVersionTests {
4141

4242
@Test
4343
void header() {
44-
Map<String, String> result = performRequest(ApiVersionInserter.fromHeader("X-API-Version").build());
44+
Map<String, String> result = performRequest(ApiVersionInserter.useHeader("X-API-Version"));
4545
assertThat(result.get(HEADER_NAME)).isEqualTo("1.2");
4646
}
4747

4848
@Test
4949
void queryParam() {
50-
Map<String, String> result = performRequest(ApiVersionInserter.fromQueryParam("api-version").build());
50+
Map<String, String> result = performRequest(ApiVersionInserter.useQueryParam("api-version"));
5151
assertThat(result.get("query")).isEqualTo("api-version=1.2");
5252
}
5353

5454
@Test
5555
void pathSegment() {
56-
Map<String, String> result = performRequest(ApiVersionInserter.fromPathSegment(0).build());
56+
Map<String, String> result = performRequest(ApiVersionInserter.usePathSegment(0));
5757
assertThat(result.get("path")).isEqualTo("/1.2/path");
5858
}
5959

spring-web/src/main/java/org/springframework/web/client/ApiVersionInserter.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,34 @@ default void insertVersion(Object version, HttpHeaders headers) {
5353

5454

5555
/**
56-
* Create a builder for an inserter that sets a header.
56+
* Create an inserter that sets a header.
5757
* @param header the name of a header to hold the version
5858
*/
59-
static Builder fromHeader(@Nullable String header) {
60-
return new DefaultApiVersionInserterBuilder(header, null, null);
59+
static ApiVersionInserter useHeader(@Nullable String header) {
60+
return new DefaultApiVersionInserterBuilder(header, null, null).build();
6161
}
6262

6363
/**
64-
* Create a builder for an inserter that sets a query parameter.
64+
* Create an inserter that sets a query parameter.
6565
* @param queryParam the name of a query parameter to hold the version
6666
*/
67-
static Builder fromQueryParam(@Nullable String queryParam) {
68-
return new DefaultApiVersionInserterBuilder(null, queryParam, null);
67+
static ApiVersionInserter useQueryParam(@Nullable String queryParam) {
68+
return new DefaultApiVersionInserterBuilder(null, queryParam, null).build();
6969
}
7070

7171
/**
72-
* Create a builder for an inserter that inserts a path segment.
72+
* Create an inserter that inserts a path segment.
7373
* @param pathSegmentIndex the index of the path segment to hold the version
7474
*/
75-
static Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
76-
return new DefaultApiVersionInserterBuilder(null, null, pathSegmentIndex);
75+
static ApiVersionInserter usePathSegment(@Nullable Integer pathSegmentIndex) {
76+
return new DefaultApiVersionInserterBuilder(null, null, pathSegmentIndex).build();
77+
}
78+
79+
/**
80+
* Create a builder for an {@link ApiVersionInserter}.
81+
*/
82+
static Builder builder() {
83+
return new DefaultApiVersionInserterBuilder(null, null, null);
7784
}
7885

7986

@@ -86,19 +93,19 @@ interface Builder {
8693
* Configure the inserter to set a header.
8794
* @param header the name of the header to hold the version
8895
*/
89-
Builder fromHeader(@Nullable String header);
96+
Builder useHeader(@Nullable String header);
9097

9198
/**
9299
* Configure the inserter to set a query parameter.
93100
* @param queryParam the name of the query parameter to hold the version
94101
*/
95-
Builder fromQueryParam(@Nullable String queryParam);
102+
Builder useQueryParam(@Nullable String queryParam);
96103

97104
/**
98105
* Configure the inserter to insert a path segment.
99106
* @param pathSegmentIndex the index of the path segment to hold the version
100107
*/
101-
Builder fromPathSegment(@Nullable Integer pathSegmentIndex);
108+
Builder usePathSegment(@Nullable Integer pathSegmentIndex);
102109

103110
/**
104111
* Format the version Object into a String using the given {@link ApiVersionFormatter}.

spring-web/src/main/java/org/springframework/web/client/DefaultApiVersionInserterBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*
2424
* @author Rossen Stoyanchev
2525
* @since 7.0
26-
* @see ApiVersionInserter#fromHeader(String)
27-
* @see ApiVersionInserter#fromQueryParam(String)
28-
* @see ApiVersionInserter#fromPathSegment(Integer)
26+
* @see ApiVersionInserter#useHeader(String)
27+
* @see ApiVersionInserter#useQueryParam(String)
28+
* @see ApiVersionInserter#usePathSegment(Integer)
2929
*/
3030
final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Builder {
3131

@@ -50,7 +50,7 @@ final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Build
5050
* Configure the inserter to set a header.
5151
* @param header the name of the header to hold the version
5252
*/
53-
public ApiVersionInserter.Builder fromHeader(@Nullable String header) {
53+
public ApiVersionInserter.Builder useHeader(@Nullable String header) {
5454
this.header = header;
5555
return this;
5656
}
@@ -59,7 +59,7 @@ public ApiVersionInserter.Builder fromHeader(@Nullable String header) {
5959
* Configure the inserter to set a query parameter.
6060
* @param queryParam the name of the query parameter to hold the version
6161
*/
62-
public ApiVersionInserter.Builder fromQueryParam(@Nullable String queryParam) {
62+
public ApiVersionInserter.Builder useQueryParam(@Nullable String queryParam) {
6363
this.queryParam = queryParam;
6464
return this;
6565
}
@@ -68,7 +68,7 @@ public ApiVersionInserter.Builder fromQueryParam(@Nullable String queryParam) {
6868
* Configure the inserter to insert a path segment.
6969
* @param pathSegmentIndex the index of the path segment to hold the version
7070
*/
71-
public ApiVersionInserter.Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
71+
public ApiVersionInserter.Builder usePathSegment(@Nullable Integer pathSegmentIndex) {
7272
this.pathSegmentIndex = pathSegmentIndex;
7373
return this;
7474
}

spring-web/src/test/java/org/springframework/web/client/RestClientVersionTests.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,50 +59,47 @@ void shutdown() throws IOException {
5959

6060
@Test
6161
void header() {
62-
performRequest(ApiVersionInserter.fromHeader("X-API-Version"));
62+
performRequest(ApiVersionInserter.useHeader("X-API-Version"));
6363
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
6464
}
6565

6666
@Test
6767
void queryParam() {
68-
performRequest(ApiVersionInserter.fromQueryParam("api-version"));
68+
performRequest(ApiVersionInserter.useQueryParam("api-version"));
6969
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path?api-version=1.2"));
7070
}
7171

7272
@Test
7373
void pathSegmentIndexLessThanSize() {
74-
performRequest(ApiVersionInserter.fromPathSegment(0).withVersionFormatter(v -> "v" + v));
74+
performRequest(ApiVersionInserter.builder().usePathSegment(0).withVersionFormatter(v -> "v" + v).build());
7575
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/v1.2/path"));
7676
}
7777

7878
@Test
7979
void pathSegmentIndexEqualToSize() {
80-
performRequest(ApiVersionInserter.fromPathSegment(1).withVersionFormatter(v -> "v" + v));
80+
performRequest(ApiVersionInserter.builder().usePathSegment(1).withVersionFormatter(v -> "v" + v).build());
8181
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path/v1.2"));
8282
}
8383

8484
@Test
8585
void pathSegmentIndexGreaterThanSize() {
8686
assertThatIllegalStateException()
87-
.isThrownBy(() -> performRequest(ApiVersionInserter.fromPathSegment(2)))
87+
.isThrownBy(() -> performRequest(ApiVersionInserter.usePathSegment(2)))
8888
.withMessage("Cannot insert version into '/path' at path segment index 2");
8989
}
9090

9191
@Test
9292
void defaultVersion() {
93-
ApiVersionInserter inserter = ApiVersionInserter.fromHeader("X-API-Version").build();
93+
ApiVersionInserter inserter = ApiVersionInserter.useHeader("X-API-Version");
9494
RestClient restClient = restClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
9595
restClient.get().uri("/path").retrieve().body(String.class);
9696

9797
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
9898
}
9999

100-
private void performRequest(ApiVersionInserter.Builder builder) {
101-
ApiVersionInserter versionInserter = builder.build();
102-
RestClient restClient = restClientBuilder.apiVersionInserter(versionInserter).build();
103-
104-
restClient.get()
105-
.uri("/path")
100+
private void performRequest(ApiVersionInserter versionInserter) {
101+
restClientBuilder.apiVersionInserter(versionInserter).build()
102+
.get().uri("/path")
106103
.apiVersion(1.2)
107104
.retrieve()
108105
.body(String.class);

spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void getWithIgnoredUriBuilderFactory(MockWebServer server, Service service) thro
274274
void apiVersion() throws Exception {
275275
RestClient restClient = RestClient.builder()
276276
.baseUrl(anotherServer.url("/").toString())
277-
.apiVersionInserter(ApiVersionInserter.fromHeader("X-API-Version").build())
277+
.apiVersionInserter(ApiVersionInserter.useHeader("X-API-Version"))
278278
.build();
279279

280280
RestClientAdapter adapter = RestClientAdapter.create(restClient);

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientVersionTests.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,46 +58,45 @@ void shutdown() throws IOException {
5858

5959
@Test
6060
void header() {
61-
performRequest(ApiVersionInserter.fromHeader("X-API-Version"));
61+
performRequest(ApiVersionInserter.useHeader("X-API-Version"));
6262
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
6363
}
6464

6565
@Test
6666
void queryParam() {
67-
performRequest(ApiVersionInserter.fromQueryParam("api-version"));
67+
performRequest(ApiVersionInserter.useQueryParam("api-version"));
6868
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path?api-version=1.2"));
6969
}
7070

7171
@Test
7272
void pathSegmentIndexLessThanSize() {
73-
performRequest(ApiVersionInserter.fromPathSegment(0).withVersionFormatter(v -> "v" + v));
73+
performRequest(ApiVersionInserter.builder().usePathSegment(0).withVersionFormatter(v -> "v" + v).build());
7474
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/v1.2/path"));
7575
}
7676

7777
@Test
7878
void pathSegmentIndexEqualToSize() {
79-
performRequest(ApiVersionInserter.fromPathSegment(1).withVersionFormatter(v -> "v" + v));
79+
performRequest(ApiVersionInserter.builder().usePathSegment(1).withVersionFormatter(v -> "v" + v).build());
8080
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path/v1.2"));
8181
}
8282

8383
@Test
8484
void pathSegmentIndexGreaterThanSize() {
8585
assertThatIllegalStateException()
86-
.isThrownBy(() -> performRequest(ApiVersionInserter.fromPathSegment(2)))
86+
.isThrownBy(() -> performRequest(ApiVersionInserter.usePathSegment(2)))
8787
.withMessage("Cannot insert version into '/path' at path segment index 2");
8888
}
8989

9090
@Test
9191
void defaultVersion() {
92-
ApiVersionInserter inserter = ApiVersionInserter.fromHeader("X-API-Version").build();
92+
ApiVersionInserter inserter = ApiVersionInserter.useHeader("X-API-Version");
9393
WebClient webClient = webClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
9494
webClient.get().uri("/path").retrieve().bodyToMono(String.class).block();
9595

9696
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
9797
}
9898

99-
private void performRequest(ApiVersionInserter.Builder builder) {
100-
ApiVersionInserter versionInserter = builder.build();
99+
private void performRequest(ApiVersionInserter versionInserter) {
101100
WebClient webClient = webClientBuilder.apiVersionInserter(versionInserter).build();
102101
webClient.get().uri("/path").apiVersion(1.2).retrieve().bodyToMono(String.class).block();
103102
}

0 commit comments

Comments
 (0)