8000 implementing json patch by shawkins · Pull Request #40904 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions quarkus/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@
</dependency>

<!-- Keycloak Dependencies-->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>zjsonpatch</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>commons-logging-jboss-logging</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions rest/admin-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>zjsonpatch</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}
Expand Down
100 changes: 100 additions & 0 deletions tests/base/src/test/java/org/keycloak/tests/admin/AdminV2Test.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
0