Skip to content

Commit

Permalink
Merge pull request #1228 from adorsys/protocol-mappers-not-deleting
Browse files Browse the repository at this point in the history
fix: Deleting protocol mappers during config import
  • Loading branch information
Motouom authored Dec 19, 2024
2 parents 7b64628 + e56fdc6 commit 1e3badb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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
}

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

0 comments on commit 1e3badb

Please sign in to comment.