KEYCLOAK-16244 RealmRealmLocalizationResourceTest fails on auth-server-quarkus

This commit is contained in:
mposolda 2020-11-11 12:01:11 +01:00 committed by Marek Posolda
parent 030a077e99
commit 9b2f2015f7
3 changed files with 32 additions and 9 deletions

View file

@ -873,8 +873,9 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, GroupPro
public boolean updateLocalizationText(RealmModel realm, String locale, String key, String text) { public boolean updateLocalizationText(RealmModel realm, String locale, String key, String text) {
RealmLocalizationTextsEntity entity = getRealmLocalizationTextsEntity(locale, realm.getId()); RealmLocalizationTextsEntity entity = getRealmLocalizationTextsEntity(locale, realm.getId());
if (entity != null && entity.getTexts() != null && entity.getTexts().containsKey(key)) { if (entity != null && entity.getTexts() != null && entity.getTexts().containsKey(key)) {
entity.getTexts().put(key, text); Map<String, String> keys = new HashMap<>(entity.getTexts());
keys.put(key, text);
entity.setTexts(keys);
em.persist(entity); em.persist(entity);
return true; return true;
} else { } else {
@ -891,7 +892,9 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, GroupPro
entity.setLocale(locale); entity.setLocale(locale);
entity.setTexts(new HashMap<>()); entity.setTexts(new HashMap<>());
} }
entity.getTexts().put(key, text); Map<String, String> keys = new HashMap<>(entity.getTexts());
keys.put(key, text);
entity.setTexts(keys);
em.persist(entity); em.persist(entity);
} }
@ -931,8 +934,9 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, GroupPro
public boolean deleteLocalizationText(RealmModel realm, String locale, String key) { public boolean deleteLocalizationText(RealmModel realm, String locale, String key) {
RealmLocalizationTextsEntity entity = getRealmLocalizationTextsEntity(locale, realm.getId()); RealmLocalizationTextsEntity entity = getRealmLocalizationTextsEntity(locale, realm.getId());
if (entity != null && entity.getTexts() != null && entity.getTexts().containsKey(key)) { if (entity != null && entity.getTexts() != null && entity.getTexts().containsKey(key)) {
entity.getTexts().remove(key); Map<String, String> keys = new HashMap<>(entity.getTexts());
keys.remove(key);
entity.setTexts(keys);
em.persist(entity); em.persist(entity);
return true; return true;
} else { } else {

View file

@ -2230,6 +2230,9 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
Map<String, RealmLocalizationTextsEntity> currentLocalizationTexts = realm.getRealmLocalizationTexts(); Map<String, RealmLocalizationTextsEntity> currentLocalizationTexts = realm.getRealmLocalizationTexts();
if(currentLocalizationTexts.containsKey(locale)) { if(currentLocalizationTexts.containsKey(locale)) {
RealmLocalizationTextsEntity localizationTextsEntity = currentLocalizationTexts.get(locale); RealmLocalizationTextsEntity localizationTextsEntity = currentLocalizationTexts.get(locale);
Map<String, String> keys = new HashMap<>(localizationTextsEntity.getTexts());
keys.putAll(localizationTexts);
localizationTextsEntity.setTexts(keys);
localizationTextsEntity.getTexts().putAll(localizationTexts); localizationTextsEntity.getTexts().putAll(localizationTexts);
em.persist(localizationTextsEntity); em.persist(localizationTextsEntity);

View file

@ -18,6 +18,7 @@
package org.keycloak.models.jpa.entities; package org.keycloak.models.jpa.entities;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import javax.persistence.Column; import javax.persistence.Column;
@ -32,6 +33,11 @@ import org.keycloak.models.jpa.converter.MapStringConverter;
@IdClass(RealmLocalizationTextsEntity.RealmLocalizationTextEntityKey.class) @IdClass(RealmLocalizationTextsEntity.RealmLocalizationTextEntityKey.class)
@Table(name = "REALM_LOCALIZATIONS") @Table(name = "REALM_LOCALIZATIONS")
public class RealmLocalizationTextsEntity { public class RealmLocalizationTextsEntity {
// TODO: Remove this constant once the quarkus issue is fixed and use the @Convert annotation in the proper JPA way. Ideally see the github history and revert whole commit,
// which adds this "TODO" once the quarkus issue is fixed
private static final MapStringConverter MAP_STRING_CONVERTER = new MapStringConverter();
static public class RealmLocalizationTextEntityKey implements Serializable { static public class RealmLocalizationTextEntityKey implements Serializable {
private String realmId; private String realmId;
private String locale; private String locale;
@ -76,15 +82,25 @@ public class RealmLocalizationTextsEntity {
private String locale; private String locale;
@Column(name = "TEXTS") @Column(name = "TEXTS")
@Convert(converter = MapStringConverter.class) private String texts;
private Map<String,String> texts; // TODO: The @Convert does not work as expected on quarkus. It doesn't update the "texts" in case that updated map has same keys (but different values) as old map had
// @Convert(converter = MapStringConverter.class)
// private Map<String,String> texts;
public Map<String,String> getTexts() { public Map<String,String> getTexts() {
return texts; if (texts == null) {
return Collections.emptyMap();
} else {
return Collections.unmodifiableMap(MAP_STRING_CONVERTER.convertToEntityAttribute(texts));
}
} }
public void setTexts(Map<String,String> texts) { public void setTexts(Map<String,String> texts) {
this.texts = texts; if (texts == null) {
this.texts = null;
} else {
this.texts = MAP_STRING_CONVERTER.convertToDatabaseColumn(texts);
}
} }
public String getLocale() { public String getLocale() {