diff --git a/quarkus/runtime/pom.xml b/quarkus/runtime/pom.xml
index 0d970b572f7..dcb248e49e4 100644
--- a/quarkus/runtime/pom.xml
+++ b/quarkus/runtime/pom.xml
@@ -407,6 +407,10 @@
+
+ io.fabric8
+ zjsonpatch
+
org.jboss.logging
commons-logging-jboss-logging
diff --git a/rest/admin-api/pom.xml b/rest/admin-api/pom.xml
index 638446400b6..502516edca0 100644
--- a/rest/admin-api/pom.xml
+++ b/rest/admin-api/pom.xml
@@ -36,5 +36,9 @@
org.keycloak
keycloak-services
+
+ io.fabric8
+ zjsonpatch
+
\ No newline at end of file
diff --git a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java
index fe7c74a487e..bfefe259196 100644
--- a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java
+++ b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java
@@ -15,6 +15,9 @@
public interface ClientApi {
+ // TODO move these
+ public static final String CONENT_TYPE_MERGE_PATCH = "application/merge-patch+json";
+
@GET
@Produces(MediaType.APPLICATION_JSON)
ClientRepresentation getClient();
@@ -25,7 +28,7 @@ public interface ClientApi {
ClientRepresentation createOrUpdateClient(ClientRepresentation client, @PathParam("fieldValidation") FieldValidation fieldValidation);
@PATCH
- @Consumes("application/merge-patch+json")
+ @Consumes({MediaType.APPLICATION_JSON_PATCH_JSON, CONENT_TYPE_MERGE_PATCH})
@Produces(MediaType.APPLICATION_JSON)
ClientRepresentation patchClient(JsonNode patch, @PathParam("fieldValidation") FieldValidation fieldValidation);
diff --git a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java
index 556fb077137..8cea60fc83b 100644
--- a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java
+++ b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java
@@ -16,8 +16,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
+import io.fabric8.zjsonpatch.JsonPatch;
+import io.fabric8.zjsonpatch.JsonPatchException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.WebApplicationException;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
public class DefaultClientApi implements ClientApi {
@@ -59,9 +63,19 @@ public ClientRepresentation patchClient(JsonNode patch, FieldValidation fieldVal
// patches don't yet allow for creating
ClientRepresentation client = getClient();
try {
+ String contentType = session.getContext().getHttpRequest().getHttpHeaders().getHeaderString(HttpHeaders.CONTENT_TYPE);
+
+ ClientRepresentation updated = null;
+
// TODO: there should be a more centralized objectmapper
- final ObjectReader objectReader = new ObjectMapper().readerForUpdating(client);
- ClientRepresentation updated = objectReader.readValue(patch);
+ ObjectMapper objectMapper = new ObjectMapper();
+ if (MediaType.valueOf(contentType).getSubtype().equals(MediaType.APPLICATION_JSON_PATCH_JSON_TYPE.getSubtype())) {
+ JsonNode patchedNode = JsonPatch.apply(patch, objectMapper.convertValue(client, JsonNode.class));
+ updated = objectMapper.convertValue(patchedNode, ClientRepresentation.class);
+ } else { // must be merge patch
+ final ObjectReader objectReader = objectMapper.readerForUpdating(client);
+ updated = objectReader.readValue(patch);
+ }
// TODO: reuse in the other methods
if (!updated.getAdditionalFields().isEmpty()) {
@@ -73,9 +87,11 @@ public ClientRepresentation patchClient(JsonNode patch, FieldValidation fieldVal
}
}
return clientService.createOrUpdate(realm, updated, true).representation();
- } catch (JsonProcessingException e) {
+ } catch (JsonPatchException e) {
// TODO: kubernetes uses 422 instead
throw new WebApplicationException(e.getMessage(), Response.Status.BAD_REQUEST);
+ } catch (JsonProcessingException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.BAD_REQUEST);
} catch (IOException e) {
throw ErrorResponse.error("Unknown Error Occurred", Response.Status.INTERNAL_SERVER_ERROR);
}
diff --git a/tests/base/src/test/java/org/keycloak/tests/admin/AdminV2Test.java b/tests/base/src/test/java/org/keycloak/tests/admin/AdminV2Test.java
new file mode 100644
index 00000000000..91547c4ea71
--- /dev/null
+++ b/tests/base/src/test/java/org/keycloak/tests/admin/AdminV2Test.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2025 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.keycloak.tests.admin;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPatch;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.keycloak.admin.api.client.ClientApi;
+import org.keycloak.representations.admin.v2.ClientRepresentation;
+import org.keycloak.testframework.annotations.InjectHttpClient;
+import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MediaType;
+
+@KeycloakIntegrationTest()
+public class AdminV2Test {
+
+ private static final String HOSTNAME_LOCAL_ADMIN = "http://localhost:8080/admin/api/v2";
+
+ @InjectHttpClient
+ private HttpClient client;
+
+ @Test
+ public void testGetClient() throws Exception {
+ HttpGet request = new HttpGet(HOSTNAME_LOCAL_ADMIN + "/realms/master/clients/account");
+ HttpResponse response = client.execute(request);
+ assertEquals(200, response.getStatusLine().getStatusCode());
+ ObjectMapper mapper = new ObjectMapper();
+ ClientRepresentation client = mapper.createParser(response.getEntity().getContent()).readValueAs(ClientRepresentation.class);
+ assertEquals("account", client.getClientId());
+ }
+
+ @Test
+ public void testJsonPatchClient() throws Exception {
+ HttpPatch request = new HttpPatch(HOSTNAME_LOCAL_ADMIN + "/realms/master/clients/account");
+ request.setEntity(new StringEntity("not json"));
+ request.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_PATCH_JSON);
+ HttpResponse response = client.execute(request);
+ EntityUtils.consumeQuietly(response.getEntity());
+ assertEquals(400, response.getStatusLine().getStatusCode());
+
+ request.setEntity(new StringEntity(
+ """
+ [{"op": "add", "path": "/description", "value": "I'm a description"}]
+ """));
+
+ response = client.execute(request);
+ assertEquals(200, response.getStatusLine().getStatusCode());
+
+ ObjectMapper mapper = new ObjectMapper();
+ ClientRepresentation client = mapper.createParser(response.getEntity().getContent()).readValueAs(ClientRepresentation.class);
+ assertEquals("I'm a description", client.getDescription());
+ }
+
+ @Disabled
+ @Test
+ public void testJsonMergePatchClient() throws Exception {
+ HttpPatch request = new HttpPatch(HOSTNAME_LOCAL_ADMIN + "/realms/master/clients/account");
+ request.setHeader(HttpHeaders.CONTENT_TYPE, ClientApi.CONENT_TYPE_MERGE_PATCH);
+
+ ClientRepresentation patch = new ClientRepresentation();
+ patch.setDescription("I'm also a description");
+
+ ObjectMapper mapper = new ObjectMapper();
+
+ request.setEntity(new StringEntity(mapper.writeValueAsString(patch)));
+
+ HttpResponse response = client.execute(request);
+ assertEquals(200, response.getStatusLine().getStatusCode());
+
+ ClientRepresentation client = mapper.createParser(response.getEntity().getContent()).readValueAs(ClientRepresentation.class);
+ assertEquals("I'm also a description", client.getDescription());
+ }
+
+}