From f494fd519c7ec23013cb800396c587a582a92d8d Mon Sep 17 00:00:00 2001 From: Alex Martel <13215031+manofthepeace@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:13:31 -0500 Subject: [PATCH] Lazily access ObjectMapper in Quarkus REST FullyFeaturedServerJacksonMessageBodyWriter --- ...eaturedServerJacksonMessageBodyWriter.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/extensions/resteasy-reactive/rest-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/FullyFeaturedServerJacksonMessageBodyWriter.java b/extensions/resteasy-reactive/rest-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/FullyFeaturedServerJacksonMessageBodyWriter.java index 93d0b58600aae..6728c7f11553b 100644 --- a/extensions/resteasy-reactive/rest-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/FullyFeaturedServerJacksonMessageBodyWriter.java +++ b/extensions/resteasy-reactive/rest-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/FullyFeaturedServerJacksonMessageBodyWriter.java @@ -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; @@ -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 originalMapper; private final Providers providers; - private final ObjectWriter defaultWriter; + private final LazyValue defaultWriter; private final ConcurrentMap perMethodWriter = new ConcurrentHashMap<>(); private final ConcurrentMap perTypeWriter = new ConcurrentHashMap<>(); private final ConcurrentMap, ObjectMapper> contextResolverMap = new ConcurrentHashMap<>(); private final ConcurrentMap objectWriterMap = new ConcurrentHashMap<>(); @Inject - public FullyFeaturedServerJacksonMessageBodyWriter(ObjectMapper mapper, Providers providers) { + public FullyFeaturedServerJacksonMessageBodyWriter(Instance 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 @@ -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 @@ -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 contextResolver = providers.getContextResolver(ObjectMapper.class, context.getResponseMediaType()); if (contextResolver == null) { @@ -163,7 +171,7 @@ public ObjectMapper apply(Class aClass) { @Override public void writeTo(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap 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 {