Skip to content

Commit

Permalink
fix: add a custom component class to resolve serialization issues wit…
Browse files Browse the repository at this point in the history
…h JSONB
  • Loading branch information
MarcScheib committed Apr 16, 2024
1 parent 6b7f4e6 commit c9b3ad8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.keycloak.representations.idm.ComponentRepresentation;

import com.cycrilabs.keycloak.configurator.commands.configure.entity.CustomComponentRepresentation;
import com.cycrilabs.keycloak.configurator.shared.control.JsonUtil;
import com.cycrilabs.keycloak.configurator.shared.entity.EntityType;

Expand All @@ -23,13 +24,12 @@ public EntityType getType() {

@Override
protected Object importFile(final Path file) {
final ComponentRepresentation component =
JsonUtil.loadEntity(file, ComponentRepresentation.class);
final CustomComponentRepresentation component = JsonUtil.loadEntity(file, CustomComponentRepresentation.class);

final String[] fileNameParts = file.toString().split(PATH_SEPARATOR);
final String realmName = fileNameParts[fileNameParts.length - 3];

if (component.getParentId() != null) {
if (component.getParentId() != null && !component.getParentId().equals(realmName)) {
final ComponentRepresentation parent =
findComponentByName(realmName, component.getParentId());
if (parent == null) {
Expand All @@ -45,7 +45,7 @@ protected Object importFile(final Path file) {

try (final Response response = keycloak.realm(realmName)
.components()
.add(component)) {
.add(component.toAPI())) {
if (response.getStatus() == 409) {
Log.errorf("Could not import component from file for realm '%s': %s", realmName,
extractError(response).getErrorMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.cycrilabs.keycloak.configurator.commands.configure.entity;

import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.representations.idm.ComponentRepresentation;

/**
* Custom representation of a Keycloak component that avoids MultivaluedHashMap for the config property.
* This class can't be serialized/deserialized out-of-the-box by JSONB.
*/
@Getter
@Setter
public class CustomComponentRepresentation {

private String id;
private String name;
private String providerId;
private String providerType;
private String parentId;
private String subType;
private Map<String, List<String>> config;

public ComponentRepresentation toAPI() {
final ComponentRepresentation componentRepresentation = new ComponentRepresentation();
componentRepresentation.setId(id);
componentRepresentation.setName(name);
componentRepresentation.setProviderId(providerId);
componentRepresentation.setProviderType(providerType);
componentRepresentation.setParentId(parentId);
componentRepresentation.setSubType(subType);
componentRepresentation.setConfig(new MultivaluedHashMap<>(config));
return componentRepresentation;
}

public static CustomComponentRepresentation fromAPI(final ComponentRepresentation componentRepresentation) {
final CustomComponentRepresentation customComponentRepresentation = new CustomComponentRepresentation();
customComponentRepresentation.setId(componentRepresentation.getId());
customComponentRepresentation.setName(componentRepresentation.getName());
customComponentRepresentation.setProviderId(componentRepresentation.getProviderId());
customComponentRepresentation.setProviderType(componentRepresentation.getProviderType());
customComponentRepresentation.setParentId(componentRepresentation.getParentId());
customComponentRepresentation.setSubType(componentRepresentation.getSubType());
customComponentRepresentation.setConfig(componentRepresentation.getConfig());
return customComponentRepresentation;
}

}

0 comments on commit c9b3ad8

Please sign in to comment.