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

fix: Deleting protocol mappers during config import #1228

Merged
merged 3 commits into from
Dec 19, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed
- Fix to manage Remote state import for clientscopes and scopeMappings [#1012](https://github.com/adorsys/keycloak-config-cli/issues/1012)

### Fixed
- Fixed to delete protocol mappers if not in the import[#746](https://github.com/orgs/adorsys/projects/5/views/1?pane=issue&itemId=80856370&issue=adorsys%7Ckeycloak-config-cli%7C746)

### Fixed
- Allow environment variables from existing secrets [#822](https://github.com/adorsys/keycloak-config-cli/issues/822)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@

import org.keycloak.representations.idm.ProtocolMapperRepresentation;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

public class ProtocolMapperUtil {
private ProtocolMapperUtil() {
Expand All @@ -35,23 +33,19 @@ public static List<ProtocolMapperRepresentation> estimateProtocolMappersToRemove
List<ProtocolMapperRepresentation> protocolMappers,
List<ProtocolMapperRepresentation> existingProtocolMappers
) {
List<ProtocolMapperRepresentation> protocolMappersToRemove = new ArrayList<>();

if (existingProtocolMappers == null) {
return protocolMappersToRemove;
if (existingProtocolMappers == null || existingProtocolMappers.isEmpty()) {
return List.of(); // Return an immutable empty list
AssahBismarkabah marked this conversation as resolved.
Show resolved Hide resolved
}

for (ProtocolMapperRepresentation existingProtocolMapper : existingProtocolMappers) {
boolean shouldRemove = protocolMappers.stream().noneMatch(
m -> Objects.equals(m.getName(), existingProtocolMapper.getName())
);

if (shouldRemove) {
protocolMappersToRemove.add(existingProtocolMapper);
}
}
Set<String> protocolMapperNames = Optional.ofNullable(protocolMappers)
.stream()
.flatMap(List::stream)
.map(ProtocolMapperRepresentation::getName)
.collect(Collectors.toSet());

return protocolMappersToRemove;
return existingProtocolMappers.stream()
.filter(existingMapper -> !protocolMapperNames.contains(existingMapper.getName()))
.collect(Collectors.toCollection(ArrayList::new));
}

public static List<ProtocolMapperRepresentation> estimateProtocolMappersToAdd(
Expand Down
Loading