Skip to content
Snippets Groups Projects
Commit 9a50ddb0 authored by Martin's avatar Martin
Browse files

remove type from clienRepresentation

parent 129a65a3
Branches
No related tags found
No related merge requests found
package de.ozgcloud.operator.keycloak.client;
import org.keycloak.representations.idm.ClientRepresentation;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = { "type" }, ignoreUnknown = true)
public class CustomClientRepresentation extends ClientRepresentation {
}
\ No newline at end of file
......@@ -31,6 +31,9 @@ import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.ozgcloud.operator.keycloak.KeycloakResultParser;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
......@@ -47,8 +50,9 @@ class KeycloakClientRemoteService {
}
public String createClient(ClientRepresentation client, String realm) {
LOG.debug("Creating client {} in realm {}", client.getId(), realm);
var response = getRealm(realm).clients().create(client);
CustomClientRepresentation ccr = filterClientRepresentation(client);
LOG.debug("Creating client {} in realm {}", ccr.getId(), realm);
var response = getRealm(realm).clients().create(ccr);
KeycloakResultParser.parseCreatedResponse(response);
return CreatedResponseUtil.getCreatedId(response);
}
......@@ -68,4 +72,18 @@ class KeycloakClientRemoteService {
private RealmResource getRealm(String realm) {
return keycloak.realm(realm);
}
public CustomClientRepresentation filterClientRepresentation(ClientRepresentation cr) {
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.valueToTree(cr);
node.remove("type");
CustomClientRepresentation filtered = mapper.treeToValue(node, CustomClientRepresentation.class);
return filtered;
} catch (Exception e) {
throw new RuntimeException("FIX_ERROR: Failed to filter ClientRepresentation", e);
}
}
}
package de.ozgcloud.operator.keycloak;
package de.ozgcloud.operator.keycloak.realm;
import org.keycloak.representations.idm.RealmRepresentation;
......
......@@ -23,9 +23,6 @@
*/
package de.ozgcloud.operator.keycloak.realm;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
......@@ -38,7 +35,6 @@ import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.ozgcloud.operator.keycloak.CustomRealmRepresentation;
import de.ozgcloud.operator.keycloak.KeycloakException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
......@@ -98,16 +94,6 @@ class KeycloakRealmRemoteService {
.update(userProfileConfig);
}
// private void removeUnknownFields(RealmRepresentation realmRepresentation) {
// try {
// var field = RealmRepresentation.class.getDeclaredField("organizationsEnabled");
// field.setAccessible(true);
// field.set(realmRepresentation, null);
// } catch (Exception e) {
// LOG.error("Error on removing unknown fields", e);
// }
// }
public CustomRealmRepresentation filterRealmRepresentation(RealmRepresentation rr) {
try {
ObjectMapper mapper = new ObjectMapper();
......@@ -117,42 +103,9 @@ class KeycloakRealmRemoteService {
CustomRealmRepresentation filtered = mapper.treeToValue(node, CustomRealmRepresentation.class);
try {
LOG.error("FIX_ERROR: Object has field: " + objectHasProperty(filtered, "organizationsEnabled"));
} catch (Exception e) {
LOG.error("FIX_ERROR: cant evaluate if field exists", e);
}
return filtered;
} catch (Exception e) {
throw new RuntimeException("FIX_ERROR: Failed to filter RealmRepresentation", e);
}
}
private Boolean objectHasProperty(Object obj, String propertyName) {
var properties = getAllFields(obj);
for (Field field : properties) {
if (field.getName().equalsIgnoreCase(propertyName)) {
return true;
}
}
return false;
}
private List<Field> getAllFields(Object obj) {
var fields = new ArrayList<Field>();
getAllFieldsRecursive(fields, obj.getClass());
return fields;
}
private List<Field> getAllFieldsRecursive(List<Field> fields, Class<?> type) {
for (var field : type.getDeclaredFields()) {
fields.add(field);
}
if (type.getSuperclass() != null) {
fields = getAllFieldsRecursive(fields, type.getSuperclass());
}
return fields;
}
}
......@@ -29,6 +29,7 @@ import static org.mockito.Mockito.*;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak;
......@@ -42,11 +43,10 @@ import org.keycloak.representations.idm.RoleRepresentation;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.stereotype.Component;
import de.ozgcloud.operator.keycloak.KeycloakException;
@Component
@Disabled("Due to Bugfix")
class KeycloakClientRemoteServiceTest {
private static final String REALM = "TestNamespace";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment