Avoid caching the list of clientscopes in two places

Closes #20426

Co-authored-by: Martin Kanis <mkanis@redhat.com>
This commit is contained in:
Alexander Schwartz 2023-05-17 18:53:15 +02:00 committed by Hynek Mlnařík
parent c998193797
commit e410a76c42
3 changed files with 32 additions and 13 deletions

View file

@ -1454,13 +1454,7 @@ public class RealmAdapter implements CachedRealmModel {
@Override
public Stream<ClientScopeModel> getClientScopesStream() {
if (isUpdated()) return updated.getClientScopesStream();
return cached.getClientScopes().stream().map(scope -> {
ClientScopeModel model = cacheSession.getClientScopeById(this, scope);
if (model == null) {
throw new IllegalStateException("Cached clientScope not found: " + scope);
}
return model;
});
return cacheSession.getClientScopesStream(this);
}
@Override

View file

@ -159,7 +159,6 @@ public class CachedRealm extends AbstractExtendableRevisioned {
}
protected List<String> defaultGroups;
protected List<String> clientScopes = new LinkedList<>();
protected List<String> defaultDefaultClientScopes = new LinkedList<>();
protected List<String> optionalDefaultClientScopes = new LinkedList<>();
protected boolean internationalizationEnabled;
@ -319,7 +318,6 @@ public class CachedRealm extends AbstractExtendableRevisioned {
}
protected void cacheClientScopes(RealmModel model) {
clientScopes = model.getClientScopesStream().map(ClientScopeModel::getId).collect(Collectors.toList());
defaultDefaultClientScopes = model.getDefaultClientScopesStream(true).map(ClientScopeModel::getId)
.collect(Collectors.toList());
optionalDefaultClientScopes = model.getDefaultClientScopesStream(false).map(ClientScopeModel::getId)
@ -687,10 +685,6 @@ public class CachedRealm extends AbstractExtendableRevisioned {
return defaultGroups;
}
public List<String> getClientScopes() {
return clientScopes;
}
public List<String> getDefaultDefaultClientScopes() {
return defaultDefaultClientScopes;
}

View file

@ -16,6 +16,7 @@
*/
package org.keycloak.testsuite.model.clientscope;
import org.hamcrest.Matchers;
import org.keycloak.models.ClientModel;
import org.keycloak.models.ClientProvider;
import org.keycloak.models.ClientScopeModel;
@ -25,6 +26,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.RoleProvider;
import org.keycloak.models.cache.CacheRealmProvider;
import org.keycloak.testsuite.model.KeycloakModelTest;
import org.keycloak.testsuite.model.RequireProvider;
import java.util.LinkedList;
@ -97,4 +99,33 @@ public class ClientScopeModelTest extends KeycloakModelTest {
return null;
});
}
@Test
@RequireProvider(value=ClientScopeProvider.class, only="jpa")
@RequireProvider(value=CacheRealmProvider.class)
public void testClientScopesCaching() {
List<String> clientScopes = new LinkedList<>();
withRealm(realmId, (session, realm) -> {
ClientScopeModel clientScope = session.clientScopes().addClientScope(realm, "myClientScopeForCaching");
clientScopes.add(clientScope.getId());
assertionsForClientScopesCaching(clientScopes, session, realm);
return null;
});
withRealm(realmId, (session, realm) -> {
assertionsForClientScopesCaching(clientScopes, session, realm);
return null;
});
}
private static void assertionsForClientScopesCaching(List<String> clientScopes, KeycloakSession session, RealmModel realm) {
assertThat(clientScopes, Matchers.containsInAnyOrder(realm.getClientScopesStream()
.map(ClientScopeModel::getId).toArray(String[]::new)));
assertThat(clientScopes, Matchers.containsInAnyOrder(session.clientScopes().getClientScopesStream(realm)
.map(ClientScopeModel::getId).toArray(String[]::new)));
}
}