Skip to content

Commit

Permalink
Merge pull request #45237 from manofthepeace/lazyjackson
Browse files Browse the repository at this point in the history
Lazily access ObjectMapper in Quarkus REST P2
  • Loading branch information
geoand authored Dec 20, 2024
2 parents 33d99fd + f494fd5 commit 93eb395
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
Expand All @@ -30,24 +32,30 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import io.quarkus.arc.impl.LazyValue;
import io.quarkus.resteasy.reactive.jackson.runtime.ResteasyReactiveServerJacksonRecorder;
import io.quarkus.resteasy.reactive.jackson.runtime.mappers.JacksonMapperUtil;

public class FullyFeaturedServerJacksonMessageBodyWriter extends ServerMessageBodyWriter.AllWriteableMessageBodyWriter {

private final ObjectMapper originalMapper;
private final Instance<ObjectMapper> originalMapper;
private final Providers providers;
private final ObjectWriter defaultWriter;
private final LazyValue<ObjectWriter> defaultWriter;
private final ConcurrentMap<String, ObjectWriter> perMethodWriter = new ConcurrentHashMap<>();
private final ConcurrentMap<String, ObjectWriter> perTypeWriter = new ConcurrentHashMap<>();
private final ConcurrentMap<Class<?>, ObjectMapper> contextResolverMap = new ConcurrentHashMap<>();
private final ConcurrentMap<ObjectMapper, ObjectWriter> objectWriterMap = new ConcurrentHashMap<>();

@Inject
public FullyFeaturedServerJacksonMessageBodyWriter(ObjectMapper mapper, Providers providers) {
public FullyFeaturedServerJacksonMessageBodyWriter(Instance<ObjectMapper> mapper, Providers providers) {
this.originalMapper = mapper;
this.defaultWriter = createDefaultWriter(mapper);
this.providers = providers;
this.defaultWriter = new LazyValue<>(new Supplier<>() {
@Override
public ObjectWriter get() {
return createDefaultWriter(mapper.get());
}
});
}

@Override
Expand Down Expand Up @@ -122,8 +130,8 @@ private ObjectWriter getObjectWriterFromAnnotations(ResteasyReactiveResourceInfo
}

private ObjectWriter getEffectiveWriter(ObjectMapper effectiveMapper) {
if (effectiveMapper == originalMapper) {
return defaultWriter;
if (effectiveMapper == originalMapper.get()) {
return defaultWriter.get();
}
return objectWriterMap.computeIfAbsent(effectiveMapper, new Function<>() {
@Override
Expand All @@ -138,7 +146,7 @@ public ObjectWriter apply(ObjectMapper objectMapper) {
* Otherwise, returns the default {@link ObjectMapper}.
*/
private ObjectMapper getEffectiveMapper(Object o, ServerRequestContext context) {
ObjectMapper effectiveMapper = originalMapper;
ObjectMapper effectiveMapper = originalMapper.get();
ContextResolver<ObjectMapper> contextResolver = providers.getContextResolver(ObjectMapper.class,
context.getResponseMediaType());
if (contextResolver == null) {
Expand All @@ -163,7 +171,7 @@ public ObjectMapper apply(Class<?> aClass) {
@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
doLegacyWrite(o, annotations, httpHeaders, entityStream, defaultWriter);
doLegacyWrite(o, annotations, httpHeaders, entityStream, defaultWriter.get());
}

private static class MethodObjectWriterFunction implements Function<String, ObjectWriter> {
Expand Down

0 comments on commit 93eb395

Please sign in to comment.