Skip to content
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

Fixes the order of profiles and application names in CredhubEnvironmentRepository #2458

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
Expand Up @@ -17,8 +17,11 @@
package org.springframework.cloud.config.server.environment;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
Expand Down Expand Up @@ -61,10 +64,10 @@ public Environment findOne(String application, String profile, String label) {
label = DEFAULT_LABEL;
}

String[] applications = deDuplicateAndAddDefault(application, DEFAULT_APPLICATION);
String[] profiles = deDuplicateAndAddDefault(profile, DEFAULT_PROFILE);
List<String> applications = normalize(application, DEFAULT_APPLICATION);
List<String> profiles = normalize(profile, DEFAULT_PROFILE);

Environment environment = new Environment(application, profiles, label, null, null);
Environment environment = new Environment(application, split(profile), label, null, null);
for (String prof : profiles) {
for (String app : applications) {
addPropertySource(environment, app, prof, label);
Expand All @@ -75,17 +78,16 @@ public Environment findOne(String application, String profile, String label) {
}

/**
* Converts the comma delimited items to a List and then: - Removes duplicates and
* keeps unique items only. - Moves or Adds the given default item to the end of List.
* Splits the comma delimited items and returns the reversed distinct items with given
* default item at the end.
*/
private String[] deDuplicateAndAddDefault(String commaDelimitedItems, String defaultItem) {
var items = Arrays.stream(StringUtils.commaDelimitedListToStringArray(commaDelimitedItems))
private List<String> normalize(String commaDelimitedItems, String defaultItem) {
var items = Stream.concat(Stream.of(defaultItem), Arrays.stream(split(commaDelimitedItems)))
.distinct()
.filter(item -> !defaultItem.equals(item))
.collect(Collectors.toList());

items.add(defaultItem);
return items.toArray(new String[0]);
Collections.reverse(items);
return items;
}

private void addPropertySource(Environment environment, String application, String profile, String label) {
Expand Down Expand Up @@ -113,6 +115,10 @@ private Map<Object, Object> findProperties(String application, String profile, S
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b));
}

private String[] split(String str) {
return StringUtils.commaDelimitedListToStringArray(str);
}

@Override
public int getOrder() {
return order;
Expand Down
Loading