8000 implementing json patch by shawkins · Pull Request #40904 · keycloak/keycloak · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

implementing json patch #40904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: feature/admin-api-v2
Choose a base branch
from
Draft
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 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,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

import io.fabric8.zjsonpatch.JsonPatch;
import jakarta.json.Json;
import jakarta.json.stream.JsonParser;
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 +64,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 Down
Loading
0