diff --git a/core/src/main/java/org/keycloak/representations/idm/authorization/Permission.java b/core/src/main/java/org/keycloak/representations/idm/authorization/Permission.java index 47c26cf57c..7e865cda50 100644 --- a/core/src/main/java/org/keycloak/representations/idm/authorization/Permission.java +++ b/core/src/main/java/org/keycloak/representations/idm/authorization/Permission.java @@ -16,8 +16,10 @@ */ package org.keycloak.representations.idm.authorization; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.HashSet; import java.util.Set; /** @@ -31,6 +33,7 @@ public class Permission { @JsonProperty("resource_set_name") private final String resourceSetName; + @JsonInclude(JsonInclude.Include.NON_EMPTY) private Set scopes; public Permission() { @@ -52,6 +55,10 @@ public class Permission { } public Set getScopes() { + if (this.scopes == null) { + this.scopes = new HashSet<>(); + } + return this.scopes; } diff --git a/services/src/main/java/org/keycloak/authorization/util/Permissions.java b/services/src/main/java/org/keycloak/authorization/util/Permissions.java index 4d84b03ee5..f2cce514b5 100644 --- a/services/src/main/java/org/keycloak/authorization/util/Permissions.java +++ b/services/src/main/java/org/keycloak/authorization/util/Permissions.java @@ -95,11 +95,7 @@ public final class Permissions { resourceName = resource.getName(); } - Set scopes = null; - - if (!permission.getScopes().isEmpty()) { - scopes = permission.getScopes().stream().map(Scope::getName).collect(Collectors.toSet()); - } + Set scopes = permission.getScopes().stream().map(Scope::getName).collect(Collectors.toSet()); return new Permission(resourceId, resourceName, scopes); }).collect(Collectors.toList()); @@ -111,9 +107,6 @@ public final class Permissions { if (evalPermission == null) { evalPermission = permission; - if (evalPermission.getScopes() != null && evalPermission.getScopes().isEmpty()) { - evalPermission.setScopes(null); - } perms.put(permission.getResourceSetId(), evalPermission); }