-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly cache ContextResolver usage for ObjectMapper in client code
Fixes: #36067
- Loading branch information
Showing
5 changed files
with
143 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 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 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
53 changes: 53 additions & 0 deletions
53
...rc/main/java/io/quarkus/rest/client/reactive/jackson/runtime/serialisers/JacksonUtil.java
This file contains 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,53 @@ | ||
package io.quarkus.rest.client.reactive.jackson.runtime.serialisers; | ||
|
||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Function; | ||
|
||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.ext.ContextResolver; | ||
import jakarta.ws.rs.ext.Providers; | ||
|
||
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
final class JacksonUtil { | ||
|
||
private JacksonUtil() { | ||
} | ||
|
||
static ObjectMapper getObjectMapperFromContext(Class<?> type, MediaType responseMediaType, RestClientRequestContext context, | ||
ConcurrentMap<ResolverMapKey, ObjectMapper> contextResolverMap) { | ||
Providers providers = getProviders(context); | ||
if (providers == null) { | ||
return null; | ||
} | ||
|
||
ContextResolver<ObjectMapper> contextResolver = providers.getContextResolver(ObjectMapper.class, | ||
responseMediaType); | ||
if (contextResolver == null) { | ||
// TODO: not sure if this is correct, but Jackson does this as well... | ||
contextResolver = providers.getContextResolver(ObjectMapper.class, null); | ||
} | ||
if (contextResolver != null) { | ||
var cr = contextResolver; | ||
var key = new ResolverMapKey(type, context.getConfiguration(), context.getInvokedMethod().getDeclaringClass()); | ||
return contextResolverMap.computeIfAbsent(key, new Function<>() { | ||
@Override | ||
public ObjectMapper apply(ResolverMapKey resolverMapKey) { | ||
return cr.getContext(resolverMapKey.getType()); | ||
} | ||
}); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static Providers getProviders(RestClientRequestContext context) { | ||
if (context != null && context.getClientRequestContext() != null) { | ||
return context.getClientRequestContext().getProviders(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...main/java/io/quarkus/rest/client/reactive/jackson/runtime/serialisers/ResolverMapKey.java
This file contains 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,53 @@ | ||
package io.quarkus.rest.client.reactive.jackson.runtime.serialisers; | ||
|
||
import java.util.Objects; | ||
|
||
import jakarta.ws.rs.core.Configuration; | ||
|
||
/** | ||
* Each REST Client can potentially have different providers, so we need to make sure that | ||
* caching for one client does not affect caching of another | ||
*/ | ||
public final class ResolverMapKey { | ||
|
||
private final Class<?> type; | ||
private final Configuration configuration; | ||
|
||
private final Class<?> restClientClass; | ||
|
||
public ResolverMapKey(Class<?> type, Configuration configuration, Class<?> restClientClass) { | ||
this.type = type; | ||
this.configuration = configuration; | ||
this.restClientClass = restClientClass; | ||
} | ||
|
||
public Class<?> getType() { | ||
return type; | ||
} | ||
|
||
public Configuration getConfiguration() { | ||
return configuration; | ||
} | ||
|
||
public Class<?> getRestClientClass() { | ||
return restClientClass; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof ResolverMapKey)) { | ||
return false; | ||
} | ||
ResolverMapKey that = (ResolverMapKey) o; | ||
return Objects.equals(type, that.type) && Objects.equals(configuration, that.configuration) | ||
&& Objects.equals(restClientClass, that.restClientClass); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(type, configuration, restClientClass); | ||
} | ||
} |