Ordering the group and role ids in the policy representation

Closes #28824

Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
This commit is contained in:
Pedro Igor 2024-04-19 18:59:45 -03:00 committed by Alexander Schwartz
parent 337a337bf9
commit 8e48bac278
4 changed files with 28 additions and 7 deletions

View file

@ -21,7 +21,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -221,6 +221,8 @@ public class GroupPolicyProviderFactory implements PolicyProviderFactory<GroupPo
return Collections.emptySet();
}
return new HashSet<>(Arrays.asList(JsonSerialization.readValue(groups, GroupPolicyRepresentation.GroupDefinition[].class)));
return Arrays.stream(JsonSerialization.readValue(groups, GroupPolicyRepresentation.GroupDefinition[].class))
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
}
}

View file

@ -38,8 +38,10 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
@ -191,9 +193,10 @@ public class RolePolicyProviderFactory implements PolicyProviderFactory<RolePoli
private Set<RoleDefinition> getRoles(String rawRoles, RealmModel realm) {
if (rawRoles != null) {
try {
Set<RoleDefinition> roles = new HashSet<>(Arrays.asList(JsonSerialization.readValue(rawRoles, RoleDefinition[].class)));
roles.removeIf(definition -> getRole(definition, realm) == null);
return roles;
return Arrays.stream(JsonSerialization.readValue(rawRoles, RoleDefinition[].class))
.filter(definition -> getRole(definition, realm) != null)
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
} catch (IOException e) {
throw new RuntimeException("Could not parse roles from config: [" + rawRoles + "]", e);
}

View file

@ -89,7 +89,7 @@ public class GroupPolicyRepresentation extends AbstractPolicyRepresentation {
}
}
public static class GroupDefinition {
public static class GroupDefinition implements Comparable<GroupDefinition> {
private String id;
private String path;
@ -136,5 +136,13 @@ public class GroupPolicyRepresentation extends AbstractPolicyRepresentation {
public void setExtendChildren(boolean extendChildren) {
this.extendChildren = extendChildren;
}
@Override
public int compareTo(GroupDefinition o) {
if (o.id == null || id == null) {
return 1;
}
return id.compareTo(o.id);
}
}
}

View file

@ -67,7 +67,7 @@ public class RolePolicyRepresentation extends AbstractPolicyRepresentation {
this.fetchRoles = fetchRoles;
}
public static class RoleDefinition {
public static class RoleDefinition implements Comparable<RoleDefinition> {
private String id;
private boolean required;
@ -96,5 +96,13 @@ public class RolePolicyRepresentation extends AbstractPolicyRepresentation {
public void setRequired(boolean required) {
this.required = required;
}
@Override
public int compareTo(RoleDefinition o) {
if (id == null || o.id == null) {
return 1;
}
return id.compareTo(o.id);
}
}
}