diff --git a/serde-support/src/main/java/io/micronaut/serde/support/LegacyBeansFactory.java b/serde-support/src/main/java/io/micronaut/serde/support/LegacyBeansFactory.java index 71125e830..9dcfc077f 100644 --- a/serde-support/src/main/java/io/micronaut/serde/support/LegacyBeansFactory.java +++ b/serde-support/src/main/java/io/micronaut/serde/support/LegacyBeansFactory.java @@ -25,11 +25,14 @@ import io.micronaut.core.type.Argument; import io.micronaut.inject.ArgumentInjectionPoint; import io.micronaut.inject.InjectionPoint; +import io.micronaut.serde.Deserializer; import io.micronaut.serde.Serde; import io.micronaut.serde.SerdeIntrospections; +import io.micronaut.serde.Serializer; import io.micronaut.serde.config.DeserializationConfiguration; import io.micronaut.serde.config.SerdeConfiguration; import io.micronaut.serde.config.SerializationConfiguration; +import io.micronaut.serde.exceptions.SerdeException; import io.micronaut.serde.support.deserializers.ObjectDeserializer; import io.micronaut.serde.support.deserializers.SerdeDeserializationPreInstantiateCallback; import io.micronaut.serde.support.serdes.InetAddressSerde; @@ -69,6 +72,32 @@ , T> S provideSerde(InjectionPoint> serdeInjectionPo return null; } + @Any + @Prototype + @BootstrapContextCompatible + , T> S provideDeserializer(InjectionPoint> injectionPoint, + BeanProvider serdeRegistry) throws SerdeException { + if (injectionPoint instanceof ArgumentInjectionPoint argumentInjectionPoint) { + Argument typeParameter = argumentInjectionPoint.getArgument().getTypeParameters()[0]; + DefaultSerdeRegistry defaultSerdeRegistry = serdeRegistry.get(); + return (S) defaultSerdeRegistry.findDeserializer(typeParameter); + } + return null; + } + + @Any + @Prototype + @BootstrapContextCompatible + , T> S provideSerializer(InjectionPoint> injectionPoint, + BeanProvider serdeRegistry) throws SerdeException { + if (injectionPoint instanceof ArgumentInjectionPoint argumentInjectionPoint) { + Argument typeParameter = argumentInjectionPoint.getArgument().getTypeParameters()[0]; + DefaultSerdeRegistry defaultSerdeRegistry = serdeRegistry.get(); + return (S) defaultSerdeRegistry.findSerializer(typeParameter); + } + return null; + } + @Singleton @BootstrapContextCompatible ObjectSerializer provideObjectSerializer(BeanContext beanContext, diff --git a/serde-support/src/test/groovy/io/micronaut/serde/support/serdes/BeanContextSerdeSpec.groovy b/serde-support/src/test/groovy/io/micronaut/serde/support/serdes/BeanContextSerdeSpec.groovy index 47f32bdd1..060861a6d 100644 --- a/serde-support/src/test/groovy/io/micronaut/serde/support/serdes/BeanContextSerdeSpec.groovy +++ b/serde-support/src/test/groovy/io/micronaut/serde/support/serdes/BeanContextSerdeSpec.groovy @@ -2,9 +2,16 @@ package io.micronaut.serde.support.serdes import io.micronaut.context.BeanContext import io.micronaut.core.type.Argument +import io.micronaut.json.JsonMapper +import io.micronaut.serde.Deserializer +import io.micronaut.serde.LimitingStream import io.micronaut.serde.Serde +import io.micronaut.serde.SerdeRegistry +import io.micronaut.serde.Serializer import io.micronaut.serde.support.deserializers.ObjectDeserializer import io.micronaut.serde.support.serializers.ObjectSerializer +import io.micronaut.serde.support.util.JsonNodeDecoder +import io.micronaut.serde.support.util.JsonNodeEncoder import io.micronaut.test.extensions.spock.annotation.MicronautTest import jakarta.inject.Inject import spock.lang.Specification @@ -71,5 +78,30 @@ class BeanContextSerdeSpec extends Specification { enumSerde.getClass() == EnumSerde } + void "test retrieving Map serializer / deserializer"() { + when: + def value = Map.of("name", "Denis") + def serdeRegistry = beanContext.getBean(SerdeRegistry) + def jsonMapper = beanContext.getBean(JsonMapper) + def encoderContext = serdeRegistry.newEncoderContext(null) + def decoderContext = serdeRegistry.newDecoderContext(null) + def argument = Argument.mapOf(String, Object) + def serializer = beanContext.getBean(Argument.of(Serializer, argument)).createSpecific(encoderContext, argument) + def deserializer = beanContext.getBean(Argument.of(Deserializer, argument)).createSpecific(decoderContext, argument) + def encoder = JsonNodeEncoder.create() + + then: + serializer.serialize(encoder, encoderContext, argument, value) + def jsonNode = encoder.getCompletedValue() + def str = jsonMapper.writeValueAsString(jsonNode) + then: + str == '{"name":"Denis"}' + when: + def decoder = JsonNodeDecoder.create(jsonNode, LimitingStream.DEFAULT_LIMITS) + def deserResult = deserializer.deserialize(decoder, decoderContext, argument) + then: + deserResult == value + } + static enum MyEnum {} }