Repository navigation
[Quarkus 4] Adapt Keycloak JSON handling to Jackson 3 #54612
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
Open
Pepo48
wants to merge
2
commits into
keycloak:quarkus-next
Choose a base branch
from
Pepo48:issue-54599
base: quarkus-next
Could not load branches
Branch not found: {{ refName }}
Loading
8000
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,034
−39
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Jackson 3 representation adapters | ||
|
|
||
| This module provides Jackson 3 serializers and deserializers for Keycloak's shared representation annotations. | ||
| The adapters are used by the server and the Jackson 3 admin client. | ||
|
|
||
| The module requires Java 17. Mapper configuration and provider registration are handled by the consuming modules. | ||
|
|
||
| ## Synchronizing public client sources | ||
|
|
||
| The public Java clients are maintained in [keycloak/keycloak-client](https://github.com/keycloak/keycloak-client). | ||
| When synchronizing the Jackson 3 admin client, include `org/keycloak/json/jackson3/**/*.java` from the | ||
| `keycloak-core-jackson3` sources JAR alongside the admin-client sources. Source JARs are generated with the | ||
| `jboss-release` Maven profile. | ||
|
|
||
| The adapters are compiled as part of the Java 17 client. The shared `core` and `common` sources retain their Java 8 baseline. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.keycloak</groupId> | ||
| <artifactId>keycloak-parent</artifactId> | ||
| <version>999.0.0-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>keycloak-core-jackson3</artifactId> | ||
| <name>Keycloak 10000 Core Jackson 3 Adapters</name> | ||
| <description>Jackson 3 adapters for shared Keycloak representation annotations, without mapper policy or provider registration.</description> | ||
|
|
||
| <properties> | ||
| <maven.compiler.release>17</maven.compiler.release> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.keycloak</groupId> | ||
| <artifactId>keycloak-core</artifactId> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>*</groupId> | ||
| <artifactId>*</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.keycloak</groupId> | ||
| <artifactId>keycloak-common</artifactId> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>*</groupId> | ||
| <artifactId>*</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <!-- Reference mapper for wire-contract parity tests only. --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>tools.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-jar-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>test-jar</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...nt/jackson3/StringOrArraySerializer3.java → ...on/jackson3/StringOrArraySerializer3.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
core-jackson3/src/test/java/org/keycloak/json/jackson3/RepresentationAdaptersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package org.keycloak.json.jackson3; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.keycloak.common.util.MultivaluedHashMap; | ||
| import org.keycloak.json.MultivaluedHashMapValue; | ||
| import org.keycloak.json.ResourceTypeMap; | ||
| import org.keycloak.representations.JsonWebToken; | ||
| import org.keycloak.representations.idm.UserRepresentation; | ||
| import org.keycloak.representations.idm.authorization.ResourceType; | ||
| import org.keycloak.representations.workflows.WorkflowRepresentation; | ||
| import org.keycloak.representations.workflows.WorkflowStepRepresentation; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.junit.Test; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| /** | ||
| * Client and server run the same representation contracts with their own mapper policies. | ||
| */ | ||
| public class RepresentationAdaptersTest { | ||
|
|
||
| protected JsonMapper mapper() { | ||
| return JsonMapper.builder() | ||
| .configureForJackson2() | ||
| .annotationIntrospector(new KeycloakAnnotationIntrospector3()) | ||
| .changeDefaultPropertyInclusion(value -> JsonInclude.Value.construct( | ||
| JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_NULL)) | ||
| .build(); | ||
| } | ||
|
|
||
| private final ObjectMapper legacyMapper = new ObjectMapper() | ||
| .setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
|
|
||
| @Test | ||
| public void stringListMapAcceptsScalarArrayAndNullAttributes() throws Exception { | ||
| String json = """ | ||
| {"username":"alice","attributes":{ | ||
| "scalar":"value","array":["first","second"],"empty":[], | ||
| "null":null,"mixed":[null,"text",true,42],"boolean":false,"number":12}} | ||
| """; | ||
| var expected = legacyMapper.readValue(json, UserRepresentation.class); | ||
| var actual = mapper().readValue(json, UserRepresentation.class); | ||
|
|
||
| assertThat(actual.getAttributes(), equalTo(expected.getAttributes())); | ||
| assertThat(actual.getAttributes().get("scalar"), equalTo(List.of("value"))); | ||
| assertThat(actual.getAttributes().get("mixed"), equalTo(Arrays.asList(null, "text", "true", "42"))); | ||
| assertWireParity(actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void audienceRetainsScalarAndArrayWireForms() throws Exception { | ||
| for (String audience : List.of("\"one\"", "[\"one\"]", "[\"one\",\"two\"]", "[]", "null")) { | ||
| String json = "{\"aud\":" + audience + "}"; | ||
| var expected = legacyMapper.readValue(json, JsonWebToken.class); | ||
| var actual = mapper().readValue(json, JsonWebToken.class); | ||
|
|
||
| assertThat(actual.getAudience(), equalTo(expected.getAudience())); | ||
| assertWireParity(actual); | ||
| } | ||
| var token = new JsonWebToken().audience("one"); | ||
| assertThat(mapper().readTree(mapper().writeValueAsString(token)).get("aud").stringValue(), equalTo("one")); | ||
| } | ||
|
|
||
| @Test | ||
| public void audiencePreservesLegacyNonTextValues() throws Exception { | ||
| for (String audience : List.of("42", "true", "{}", "[null,42,true,{},[],\"one\"]")) { | ||
| String json = "{\"aud\":" + audience + "}"; | ||
| var expected = legacyMapper.readValue(json, JsonWebToken.class); | ||
| var actual = mapper().readValue(json, JsonWebToken.class); | ||
|
|
||
| assertThat(actual.getAudience(), equalTo(expected.getAudience())); | ||
| assertWireParity(actual); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void resourceTypesAcceptArrayAndObjectForms() throws Exception { | ||
| String resource = "{\"type\":\"document\",\"scopes\":[\"read\",\"write\"]}"; | ||
| for (String types : List.of("[" + resource + "]", "{\"document\":" + resource + "}")) { | ||
| String json = "{\"resourceTypes\":" + types + "}"; | ||
| var expected = legacyMapper.readValue(json, ResourceTypesValue.class); | ||
| var actual = mapper().readValue(json, ResourceTypesValue.class); | ||
|
|
||
| assertThat(actual.resourceTypes.keySet(), equalTo(Set.of("document"))); | ||
| assertThat(actual.resourceTypes.get("document").getScopes(), equalTo(expected.resourceTypes.get("document").getScopes())); | ||
| assertWireParity(actual); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void resourceTypesRejectScalarInput() { | ||
| assertThrows(tools.jackson.databind.exc.MismatchedInputException.class, | ||
| () -> mapper().readValue("{\"resourceTypes\":42}", ResourceTypesValue.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void multivaluedWorkflowConfigurationSerializationPreservesWireShape() throws Exception { | ||
| var workflow = WorkflowRepresentation.withName("test") | ||
| .onEvent("user-created") | ||
| .withSteps(WorkflowStepRepresentation.create().of("set-user-attribute") | ||
| .withConfig("key", "value") | ||
| .withConfig("enabled", "true") | ||
| .withConfig("values", "first", "second").build()) | ||
| .build(); | ||
| assertWireParity(workflow); | ||
| } | ||
|
|
||
| @Test | ||
| public void multivaluedConfigurationPreservesValuesAndFiltersReservedProperties() throws Exception { | ||
| String json = """ | ||
| {"config":{"reserved":"hidden","scalar":"value","flag":true,"values":["first","second"]}} | ||
| """; | ||
| var expected = legacyMapper.readValue(json, MultivaluedValue.class); | ||
| var actual = mapper().readValue(json, MultivaluedValue.class); | ||
|
|
||
| assertThat(actual.config, equalTo(expected.config)); | ||
| assertThat(actual.config.get("flag"), equalTo(List.of("true"))); | ||
| assertThat(mapper().readTree(mapper().writeValueAsString(actual)).get("config").has("reserved"), is(false)); | ||
| assertWireParity(actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void nativeJacksonNamingWorksInBothDirections() { | ||
| var value = mapper().readValue("{\"some-value\":\"text\"}", NativeNamingValue.class); | ||
|
|
||
| assertThat(value.someValue, equalTo("text")); | ||
| assertThat(mapper().writeValueAsString(value), equalTo("{\"some-value\":\"text\"}")); | ||
| } | ||
|
|
||
| private void assertWireParity(Object value) throws Exception { | ||
| assertThat(legacyMapper.readTree(mapper().writeValueAsString(value)), | ||
| equalTo(legacyMapper.readTree(legacyMapper.writeValueAsString(value)))); | ||
| } | ||
|
|
||
| @tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.KebabCaseStrategy.class) | ||
| public static class NativeNamingValue { | ||
| public String someValue; | ||
| } | ||
|
|
||
| public static class ResourceTypesValue { | ||
| @ResourceTypeMap | ||
| public Map<String, ResourceType> resourceTypes; | ||
| } | ||
|
|
||
| public static class MultivaluedValue { | ||
| @MultivaluedHashMapValue | ||
| public MultivaluedHashMap<String, String> config; | ||
|
|
||
| public String getReserved() { | ||
| return "visible"; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth double checking if we need this compatibility layer, if we can't just migrate to Jackson 3.