Skip to content

Support dynamic default uri variables for RestClient #34190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -42,6 +44,7 @@
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @author Yanming Zhou
* @since 3.1
* @see UriComponentsBuilder
*/
Expand Down Expand Up @@ -144,7 +147,9 @@ public final UriComponents encode() {
/**
* Replace all URI template variables with the values from a given map.
* <p>The given map keys represent variable names; the corresponding values
* represent variable values. The order of variables is not significant.
* represent variable values, if the variable value is {@link Supplier} or
* {@link Function}, then the value to use is applied base on the variable name.
* The order of variables is not significant.
* @param uriVariables the map of URI variables
* @return the expanded URI components
*/
Expand Down Expand Up @@ -233,6 +238,7 @@ public final String toString() {
return expandUriComponent(source, uriVariables, null);
}

@SuppressWarnings({"unchecked", "rawtypes"})
static @Nullable String expandUriComponent(@Nullable String source, UriTemplateVariables uriVariables,
@Nullable UnaryOperator<String> encoder) {

Expand All @@ -254,6 +260,12 @@ public final String toString() {
if (UriTemplateVariables.SKIP_VALUE.equals(varValue)) {
continue;
}
if (varValue instanceof Supplier supplier) {
varValue = supplier.get();
}
else if (varValue instanceof Function function) {
varValue = function.apply(varName);
}
String formatted = getVariableValueAsString(varValue);
formatted = encoder != null ? encoder.apply(formatted) : Matcher.quoteReplacement(formatted);
matcher.appendReplacement(sb, formatted);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,10 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.jspecify.annotations.Nullable;
Expand All @@ -44,6 +46,7 @@
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Nicklas Wiegandt
* @author Yanming Zhou
*/
public class RestClientBuilderTests {

Expand Down Expand Up @@ -110,6 +113,23 @@ void defaultUri() {
assertThat(fieldValue("baseUrl", defaultBuilder)).isEqualTo(baseUrl.toString());
}

@Test
void baseUriWithDynamicDefaultUriVariables() {
String key = "partition";
String baseUrl = "http://{" + key + "}.example.com";
Map<String, String> holder = new HashMap<>();
RestClient restClient = RestClient.builder().baseUrl(baseUrl)
.defaultUriVariables(Map.of(key, (Function<String, String>) holder::get)).build();

holder.put(key, "p0");
assertThat(fieldValue("uri", restClient.get().uri("/{foo}.html", Map.of("foo", "index")))
.toString()).isEqualTo("http://p0.example.com/index.html");

holder.put(key, "p1");
assertThat(fieldValue("uri", restClient.get().uri("/{bar}.html", Map.of("bar", "index")))
.toString()).isEqualTo("http://p1.example.com/index.html");
}

@Test
void messageConvertersList() {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
Expand Down Expand Up @@ -281,4 +301,17 @@ void buildCopiesDefaultCookiesImmutable() {
return null;
}
}

private static @Nullable Object fieldValue(String name, Object instance) {
try {
Field field = instance.getClass().getDeclaredField(name);
field.setAccessible(true);

return field.get(instance);
}
catch (NoSuchFieldException | IllegalAccessException ex) {
fail(ex.getMessage(), ex);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,9 @@
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -42,6 +45,7 @@
* @author Arjen Poutsma
* @author Phillip Webb
* @author Rossen Stoyanchev
* @author Yanming Zhou
*/
class UriComponentsTests {

Expand Down Expand Up @@ -266,4 +270,24 @@ void equalsOpaqueUriComponents(ParserType parserType) {
assertThat(uric1).isNotEqualTo(uric3);
}

@Test
void expandSupplier() {
String key = "partition";
Map<String, String> holder = Map.of(key, "p0");
UriComponents uri = UriComponentsBuilder.fromUriString("http://{" + key + "}.example.com")
.uriVariables(Map.of(key, (Supplier<String>) () -> holder.get(key))).build();

assertThat(uri.toString()).isEqualTo("http://p0.example.com");
}

@Test
void expandFunction() {
String key = "partition";
Map<String, String> holder = Map.of(key, "p0");
UriComponents uri = UriComponentsBuilder.fromUriString("http://{" + key + "}.example.com")
.uriVariables(Map.of(key, (Function<String, String>) holder::get)).build();

assertThat(uri.toString()).isEqualTo("http://p0.example.com");
}

}
Loading