-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Improve compatibility of the REST Client configuration #42932
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 0 additions & 87 deletions
87
...main/java/io/quarkus/restclient/config/RestClientNameFallbackConfigSourceInterceptor.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
...runtime/src/main/java/io/quarkus/restclient/config/RestClientNameFallbackInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import static io.quarkus.restclient.config.AbstractRestClientConfigBuilder.indexOfRestClient; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import jakarta.annotation.Priority; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import io.smallrye.config.FallbackConfigSourceInterceptor; | ||
import io.smallrye.config.Priorities; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
|
||
/** | ||
* Fallbacks REST Client FQN to Simple Name and quoted config keys to unquoted | ||
* <p> | ||
* Ideally, this shouldn't be required. The old custom implementation allowed us to mix both FQN and Simple Name in a | ||
* merged configuration to use in the REST Client. The standard Config system does not support such a feature. If a | ||
* configuration supports multiple names, the user has to use the same name across all configuration sources. No other | ||
* Quarkus extension behaves this way because only the REST Client extension provides the custom code to make it work. | ||
* <p> | ||
* In the case of {@link RegisterRestClient#configKey()}, users either use quoted or unquoted configuration names for | ||
* single config key segments. Again, the Config system does not support such a feature (but could be implemented), so | ||
* the interceptor also fallbacks to unquoted configuration names, due to the <code>force</code> property added by | ||
* {@link AbstractRestClientConfigBuilder#configBuilder(SmallRyeConfigBuilder)}. | ||
*/ | ||
@Priority(Priorities.LIBRARY + 610) | ||
public class RestClientNameFallbackInterceptor extends FallbackConfigSourceInterceptor { | ||
public RestClientNameFallbackInterceptor(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
super(fallback(restClients, ignoreNames)); | ||
} | ||
|
||
private static Function<String, String> fallback(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
return new Function<String, String>() { | ||
@Override | ||
public String apply(final String name) { | ||
int indexOfRestClient = indexOfRestClient(name); | ||
if (indexOfRestClient != -1) { | ||
if (ignoreNames.contains(name)) { | ||
return name; | ||
} | ||
|
||
int endOfRestClient = indexOfRestClient + 1; | ||
for (RegisteredRestClient restClient : restClients) { | ||
if (name.length() > indexOfRestClient && name.charAt(indexOfRestClient) == '"') { | ||
String interfaceName = restClient.getFullName(); | ||
if (name.regionMatches(endOfRestClient, interfaceName, 0, interfaceName.length())) { | ||
if (name.length() > endOfRestClient + interfaceName.length() | ||
&& name.charAt(endOfRestClient + interfaceName.length()) == '"') { | ||
return "quarkus.rest-client." + restClient.getSimpleName() | ||
+ name.substring(endOfRestClient + interfaceName.length() + 1); | ||
} | ||
} | ||
|
||
String configKey = restClient.getConfigKey(); | ||
if (configKey == null || configKey.isEmpty() || restClient.isConfigKeySegments()) { | ||
continue; | ||
} | ||
int endOfConfigKey = endOfRestClient + configKey.length(); | ||
if (name.regionMatches(endOfRestClient, configKey, 0, configKey.length())) { | ||
if (name.length() > endOfConfigKey && name.charAt(endOfConfigKey) == '"') { | ||
return "quarkus.rest-client." + configKey + name.substring(endOfConfigKey + 1); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return name; | ||
} | ||
}; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...src/main/java/io/quarkus/restclient/config/RestClientNameUnquotedFallbackInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.quarkus.restclient.config; | ||
|
||
import static io.quarkus.restclient.config.AbstractRestClientConfigBuilder.indexOfRestClient; | ||
import static io.smallrye.config.ProfileConfigSourceInterceptor.convertProfile; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import jakarta.annotation.Priority; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import io.smallrye.config.ConfigValue; | ||
import io.smallrye.config.FallbackConfigSourceInterceptor; | ||
import io.smallrye.config.NameIterator; | ||
import io.smallrye.config.Priorities; | ||
|
||
/** | ||
* Relocates unquoted config keys to quoted | ||
* <p> | ||
* In the case of {@link RegisterRestClient#configKey()}, users either use quoted or unquoted configuration names for | ||
* single config key segments. Again, the Config system does not support such a feature (but could be implemented), so | ||
* the interceptor also relocates to unquoted configuration names. | ||
* <p> | ||
* We need a double-way relocation / fallback mapping between unquoted and quoted because SmallRye Config will use the | ||
* first distict key it finds to populate {@link RestClientsConfig#clients()} in the list of property names. If quoted, | ||
* it will search for all quoted. If unquoted, it will search for all unquoted. We cannot be sure how the user sets the | ||
* configuration, especially considering that we may not be able to query the list directly if the config comes from a | ||
* source that does not support listing property names. | ||
*/ | ||
@Priority(Priorities.LIBRARY + 605) | ||
public class RestClientNameUnquotedFallbackInterceptor extends FallbackConfigSourceInterceptor { | ||
public RestClientNameUnquotedFallbackInterceptor(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
super(relocate(restClients, ignoreNames)); | ||
} | ||
|
||
private static Function<String, String> relocate(final List<RegisteredRestClient> restClients, | ||
final Set<String> ignoreNames) { | ||
return new Function<String, String>() { | ||
@Override | ||
public String apply(final String name) { | ||
int indexOfRestClient = indexOfRestClient(name); | ||
if (indexOfRestClient != -1) { | ||
if (ignoreNames.contains(name)) { | ||
return name; | ||
} | ||
|
||
for (RegisteredRestClient restClient : restClients) { | ||
String configKey = restClient.getConfigKey(); | ||
if (configKey == null || configKey.isEmpty() || restClient.isConfigKeySegments()) { | ||
continue; | ||
} | ||
|
||
int endOfConfigKey = indexOfRestClient + configKey.length(); | ||
if (name.regionMatches(indexOfRestClient, configKey, 0, configKey.length())) { | ||
if (name.length() > endOfConfigKey && name.charAt(endOfConfigKey) == '.') { | ||
return "quarkus.rest-client.\"" + configKey + "\"" + name.substring(endOfConfigKey); | ||
} | ||
} | ||
} | ||
} | ||
return name; | ||
} | ||
}; | ||
} | ||
|
||
private static final Comparator<ConfigValue> CONFIG_SOURCE_COMPARATOR = new Comparator<ConfigValue>() { | ||
@Override | ||
public int compare(ConfigValue original, ConfigValue candidate) { | ||
int result = Integer.compare(original.getConfigSourceOrdinal(), candidate.getConfigSourceOrdinal()); | ||
if (result != 0) { | ||
return result; | ||
} | ||
result = Integer.compare(original.getConfigSourcePosition(), candidate.getConfigSourcePosition()) * -1; | ||
if (result != 0) { | ||
return result; | ||
} | ||
// If both properties are profiled, prioritize the one with the most specific profile. | ||
if (original.getName().charAt(0) == '%' && candidate.getName().charAt(0) == '%') { | ||
List<String> originalProfiles = convertProfile( | ||
new NameIterator(original.getName()).getNextSegment().substring(1)); | ||
List<String> candidateProfiles = convertProfile( | ||
new NameIterator(candidate.getName()).getNextSegment().substring(1)); | ||
return Integer.compare(originalProfiles.size(), candidateProfiles.size()) * -1; | ||
} | ||
return result; | ||
} | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we doing that and enforcing this value here? I don't understand why we would have to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so I have done my homework about this thing and it seems reasonable.
I wonder though if we should come up with a way to filter it from the Dev UI? Maybe by filtering config that shouldn't appear in the doc? If it's not in the doc, I would thing that we wouldn't want it to be visible.
Or we would need another annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I'm not requesting a change for this patch. Just thinking out loud for the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to add that explanation here:
quarkus/extensions/resteasy-classic/rest-client-config/runtime/src/main/java/io/quarkus/restclient/config/AbstractRestClientConfigBuilder.java
Lines 19 to 27 in 1da0004
Let me expand that further, and then I can update the javadoc too:
In config, when we populate a
Map
, we must rely on the list of property names because the key is dynamic. As an example, if we want to retrieve the list of datasources, we have to look for propertiesquarkus.datasource."datasource-name"
, and then we do the rest.There is a catch: of course, this only works when we get at least one property of the tree with the expected name. The problem is that some sources (like Vault) cannot list the properties. In the example case, if all the datasource properties are only set in Vault, we cannot load that configuration because we don't know it exists. What we recommend is to set some base properties and then override them.
In the case of the REST Client, it's a bit of a mix. At the mapping level, we use a
Map
because, obviously, we can't represent an unknown number of clients in the mapping structure, but we do know all the possible keys in advance. So, I'm adding a dummy key to force the lookup. Once the Config knows the dynamic part of a key, it will then query all the other pieces directly so that the Vault config will work.I plan to support this out of the box without having to rely on workarounds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I missed it because of the context of the diff but the Javadoc you already have is perfectly clear. That's what I was trying to say in my previous message by "doing my homework" :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is annoying, and I didn't think of that when I added it in the first place. We could filter it with the doc annotation, or maybe
@Deprecated
?Anyway, I'll add something in the Config system for such cases (we have the same case in OTel), where we can't statically map the configuration, but the keys are well-known. I'll add something that will always query specific keys without relying on the list provided by the sources, so these types of hacks will no longer be required.