diff --git a/aws-alexa-httpserver/build.gradle b/aws-alexa-httpserver/build.gradle index 7ca509ab8f..d50e522131 100644 --- a/aws-alexa-httpserver/build.gradle +++ b/aws-alexa-httpserver/build.gradle @@ -4,10 +4,14 @@ plugins { dependencies { annotationProcessor libs.micronaut.validation + annotationProcessor libs.micronaut.serde.processor implementation libs.micronaut.validation implementation libs.spotbugs.annotations + implementation libs.micronaut.serde + implementation libs.micronaut.serde.jackson + api project(":aws-alexa") implementation libs.micronaut.http.server diff --git a/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/conf/AlexaControllerConfiguration.java b/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/conf/AlexaControllerConfiguration.java index 1b7a31c97d..27d2c94a38 100644 --- a/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/conf/AlexaControllerConfiguration.java +++ b/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/conf/AlexaControllerConfiguration.java @@ -15,8 +15,17 @@ */ package io.micronaut.aws.alexa.httpserver.conf; +import com.amazon.ask.model.Application; +import com.amazon.ask.model.Context; +import com.amazon.ask.model.LaunchRequest; +import com.amazon.ask.model.RequestEnvelope; +import com.amazon.ask.model.ResponseEnvelope; +import com.amazon.ask.model.Session; +import com.amazon.ask.model.User; +import com.amazon.ask.model.interfaces.system.SystemState; import edu.umd.cs.findbugs.annotations.Nullable; import io.micronaut.core.util.Toggleable; +import io.micronaut.serde.annotation.SerdeImport; /** * Defines configuration for the Alexa controller. @@ -24,6 +33,14 @@ * @author sdelamo * @since 2.0.0 */ +@SerdeImport(RequestEnvelope.class) +@SerdeImport(ResponseEnvelope.class) +@SerdeImport(Session.class) +@SerdeImport(User.class) +@SerdeImport(Application.class) +@SerdeImport(Context.class) +@SerdeImport(SystemState.class) +@SerdeImport(LaunchRequest.class) public interface AlexaControllerConfiguration extends Toggleable { /** diff --git a/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/controllers/SkillController.java b/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/controllers/SkillController.java index 41d2079cf1..bf511615f4 100644 --- a/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/controllers/SkillController.java +++ b/aws-alexa-httpserver/src/main/java/io/micronaut/aws/alexa/httpserver/controllers/SkillController.java @@ -17,7 +17,6 @@ import com.amazon.ask.model.RequestEnvelope; import com.amazon.ask.model.ResponseEnvelope; -import com.fasterxml.jackson.databind.ObjectMapper; import io.micronaut.aws.alexa.httpserver.AskHttpServerConstants; import io.micronaut.aws.alexa.httpserver.conf.AlexaControllerConfigurationProperties; import io.micronaut.aws.alexa.httpserver.services.RequestEnvelopeService; @@ -29,6 +28,7 @@ import io.micronaut.http.annotation.Body; import io.micronaut.http.annotation.Controller; import io.micronaut.http.annotation.Post; +import io.micronaut.serde.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/aws-alexa/build.gradle b/aws-alexa/build.gradle index 36f714e539..e76d4ff80c 100644 --- a/aws-alexa/build.gradle +++ b/aws-alexa/build.gradle @@ -4,10 +4,14 @@ plugins { dependencies { annotationProcessor libs.micronaut.validation + annotationProcessor libs.micronaut.serde.processor implementation libs.micronaut.validation implementation libs.spotbugs.annotations + implementation libs.micronaut.serde + implementation libs.micronaut.serde.jackson + compileOnly(libs.alexa.ask.sdk) api(libs.managed.alexa.ask.sdk.core) diff --git a/aws-alexa/src/main/java/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItem.java b/aws-alexa/src/main/java/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItem.java index eb82fe36ef..1923a5a8e1 100644 --- a/aws-alexa/src/main/java/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItem.java +++ b/aws-alexa/src/main/java/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItem.java @@ -19,7 +19,8 @@ import com.fasterxml.jackson.annotation.JsonInclude; import io.micronaut.core.annotation.NonNull; import io.micronaut.core.annotation.Nullable; -import io.micronaut.core.annotation.Introspected; +import io.micronaut.serde.annotation.Serdeable; + import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.PastOrPresent; @@ -32,7 +33,7 @@ * @author sdelamo * @since 2.0.0 */ -@Introspected +@Serdeable public class FlashBriefingItem implements Comparable { /** @@ -49,7 +50,7 @@ public class FlashBriefingItem implements Comparable { @NotNull @NonNull @PastOrPresent - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ") + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ") private ZonedDateTime updateDate; /** @@ -65,7 +66,7 @@ public class FlashBriefingItem implements Comparable { @NonNull @NotNull @Size(max = 4500) - @JsonInclude() + @JsonInclude(value = JsonInclude.Include.ALWAYS) private String mainText; /** diff --git a/aws-alexa/src/test/groovy/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItemSpec.groovy b/aws-alexa/src/test/groovy/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItemSpec.groovy index fd2066fc33..42bf37abd9 100644 --- a/aws-alexa/src/test/groovy/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItemSpec.groovy +++ b/aws-alexa/src/test/groovy/io/micronaut/aws/alexa/flashbriefing/FlashBriefingItemSpec.groovy @@ -1,8 +1,8 @@ package io.micronaut.aws.alexa.flashbriefing -import com.fasterxml.jackson.databind.ObjectMapper import groovy.json.JsonSlurper import io.micronaut.aws.ApplicationContextSpecification +import io.micronaut.serde.ObjectMapper import spock.lang.PendingFeature import spock.lang.Shared import spock.lang.Unroll diff --git a/aws-common/build.gradle b/aws-common/build.gradle index be8b80acc7..062b6448e2 100644 --- a/aws-common/build.gradle +++ b/aws-common/build.gradle @@ -3,6 +3,12 @@ plugins { } dependencies { + annotationProcessor libs.micronaut.serde.processor + compileOnly libs.micronaut.runtime + + implementation libs.micronaut.serde + implementation libs.micronaut.serde.jackson + testImplementation libs.micronaut.runtime } diff --git a/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonComputeInstanceMetadataResolver.java b/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonComputeInstanceMetadataResolver.java index 854fcddb89..f50f8f84c6 100644 --- a/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonComputeInstanceMetadataResolver.java +++ b/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonComputeInstanceMetadataResolver.java @@ -15,8 +15,7 @@ */ package io.micronaut.discovery.cloud.aws; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonFactory; import io.micronaut.context.annotation.Primary; import io.micronaut.context.annotation.Requires; import io.micronaut.context.env.Environment; @@ -24,6 +23,10 @@ import io.micronaut.core.util.StringUtils; import io.micronaut.discovery.cloud.ComputeInstanceMetadata; import io.micronaut.discovery.cloud.ComputeInstanceMetadataResolver; +import io.micronaut.jackson.core.tree.JsonNodeTreeCodec; +import io.micronaut.json.tree.JsonNode; +import io.micronaut.serde.ObjectMapper; +import io.micronaut.serde.annotation.SerdeImport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,6 +56,7 @@ @Requires(property = AmazonMetadataConfiguration.PREFIX + ".enabled", value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) @Requires(classes = ComputeInstanceMetadataResolver.class) @Primary +@SerdeImport(value = JsonNode.class) public class AmazonComputeInstanceMetadataResolver implements ComputeInstanceMetadataResolver { private static final Logger LOG = LoggerFactory.getLogger(AmazonComputeInstanceMetadataResolver.class); @@ -62,6 +66,7 @@ public class AmazonComputeInstanceMetadataResolver implements ComputeInstanceMet private static final Pattern DRIVE_LETTER_PATTERN = Pattern.compile("^\\/*[a-zA-z]:.*$"); private final ObjectMapper objectMapper; + private final JsonFactory jsonFactory = new JsonFactory(); private final AmazonMetadataConfiguration configuration; private AmazonEC2InstanceMetadata cachedMetadata; @@ -77,12 +82,8 @@ public AmazonComputeInstanceMetadataResolver(ObjectMapper objectMapper, AmazonMe this.configuration = configuration; } - /** - * Create a new instance to resolve {@link ComputeInstanceMetadata} for Amazon EC2 with default configurations. - */ - public AmazonComputeInstanceMetadataResolver() { - this.objectMapper = new ObjectMapper(); - this.configuration = new AmazonMetadataConfiguration(); + private static Optional stringValue(JsonNode json, String key) { + return Optional.ofNullable(json.get(key)).map(JsonNode::coerceStringValue); } @Override @@ -98,7 +99,7 @@ public Optional resolve(Environment environment) { try { String ec2InstanceIdentityDocURL = configuration.getInstanceDocumentUrl(); String ec2InstanceMetadataURL = configuration.getMetadataUrl(); - JsonNode metadataJson = readMetadataUrl(new URL(ec2InstanceIdentityDocURL), CONNECTION_TIMEOUT_IN_MILLS, READ_TIMEOUT_IN_MILLS, objectMapper, new HashMap<>()); + JsonNode metadataJson = readMetadataUrl(new URL(ec2InstanceIdentityDocURL), CONNECTION_TIMEOUT_IN_MILLS, READ_TIMEOUT_IN_MILLS, JsonNodeTreeCodec.getInstance().withConfig(objectMapper.getStreamConfig()), jsonFactory, new HashMap<>()); if (metadataJson != null) { stringValue(metadataJson, EC2MetadataKeys.instanceId.name()).ifPresent(ec2InstanceMetadata::setInstanceId); stringValue(metadataJson, EC2MetadataKeys.accountId.name()).ifPresent(ec2InstanceMetadata::setAccount); @@ -138,7 +139,7 @@ public Optional resolve(Environment environment) { LOG.error("error getting public host name from:{}{}", ec2InstanceMetadataURL, EC2MetadataKeys.publicHostname.getName(), e); } - Map metadata = objectMapper.convertValue(ec2InstanceMetadata, Map.class); + Map metadata = objectMapper.readValue(objectMapper.writeValueAsString(ec2InstanceMetadata), Map.class); populateMetadata(ec2InstanceMetadata, metadata); LOG.debug("EC2 Metadata found:{}", ec2InstanceMetadata.getMetadata()); //TODO make individual calls for building network interfaces.. required recursive http calls for all mac addresses diff --git a/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonEC2InstanceMetadata.java b/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonEC2InstanceMetadata.java index dfd90362fb..4141b26abc 100644 --- a/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonEC2InstanceMetadata.java +++ b/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonEC2InstanceMetadata.java @@ -17,8 +17,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import io.micronaut.context.env.ComputePlatform; -import io.micronaut.core.annotation.Introspected; import io.micronaut.discovery.cloud.AbstractComputeInstanceMetadata; +import io.micronaut.serde.annotation.Serdeable; /** * Represents {@link io.micronaut.discovery.cloud.ComputeInstanceMetadata} for Amazon's EC2. @@ -27,7 +27,7 @@ * @author Graeme Rocher * @since 1.0 */ -@Introspected +@Serdeable public class AmazonEC2InstanceMetadata extends AbstractComputeInstanceMetadata { private final ComputePlatform computePlatform = ComputePlatform.AMAZON_EC2; diff --git a/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonNetworkInterface.java b/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonNetworkInterface.java index 139c9b9cf5..8d957896ef 100644 --- a/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonNetworkInterface.java +++ b/aws-common/src/main/java/io/micronaut/discovery/cloud/aws/AmazonNetworkInterface.java @@ -17,6 +17,7 @@ import io.micronaut.core.annotation.Internal; import io.micronaut.discovery.cloud.NetworkInterface; +import io.micronaut.serde.annotation.Serdeable; /** * A {@link NetworkInterface} implementation for Amazon. @@ -25,6 +26,7 @@ * @since 1.0 */ @Internal +@Serdeable class AmazonNetworkInterface extends NetworkInterface { @Override diff --git a/aws-common/src/test/groovy/io/micronaut/discovery/cloud/AmazonEC2InstanceResolverSpec.groovy b/aws-common/src/test/groovy/io/micronaut/discovery/cloud/AmazonEC2InstanceResolverSpec.groovy index 9f762790a7..fdf511788b 100644 --- a/aws-common/src/test/groovy/io/micronaut/discovery/cloud/AmazonEC2InstanceResolverSpec.groovy +++ b/aws-common/src/test/groovy/io/micronaut/discovery/cloud/AmazonEC2InstanceResolverSpec.groovy @@ -15,11 +15,13 @@ */ package io.micronaut.discovery.cloud -import com.fasterxml.jackson.databind.ObjectMapper +import io.micronaut.context.ApplicationContext import io.micronaut.context.env.ComputePlatform import io.micronaut.context.env.Environment import io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver import io.micronaut.discovery.cloud.aws.AmazonMetadataConfiguration +import io.micronaut.serde.ObjectMapper +import spock.lang.AutoCleanup import spock.lang.Specification import java.nio.file.Path @@ -27,6 +29,9 @@ import java.nio.file.Paths class AmazonEC2InstanceResolverSpec extends Specification { + @AutoCleanup + ApplicationContext context = ApplicationContext.run() + void "test building ec2 metadata"() { given: Environment environment = Mock(Environment) @@ -35,7 +40,6 @@ class AmazonEC2InstanceResolverSpec extends Specification { Optional computeInstanceMetadata = resolver.resolve(environment) - expect: computeInstanceMetadata.isPresent() computeInstanceMetadata.get().getInterfaces() != null @@ -46,7 +50,6 @@ class AmazonEC2InstanceResolverSpec extends Specification { networkInterface.gateway == "vpc-75d5d111" networkInterface.id == "eni-d88bca3d" - computeInstanceMetadata.get().publicIpV4 == "34.230.77.169" computeInstanceMetadata.get().account == "057654311259" computeInstanceMetadata.get().availabilityZone == "us-east-1d" @@ -67,7 +70,7 @@ class AmazonEC2InstanceResolverSpec extends Specification { configuration.metadataUrl = "file:///${s}/src/test/groovy/io/micronaut/discovery/cloud/" configuration.instanceDocumentUrl = "file:///${s}/src/test/groovy/io/micronaut/discovery/cloud/identity-document.json" AmazonComputeInstanceMetadataResolver resolver = new AmazonComputeInstanceMetadataResolver( - new ObjectMapper(), + context.getBean(ObjectMapper), configuration ) resolver diff --git a/aws-secretsmanager/build.gradle b/aws-secretsmanager/build.gradle index 8ed65ffb58..5196ce2ca2 100644 --- a/aws-secretsmanager/build.gradle +++ b/aws-secretsmanager/build.gradle @@ -3,7 +3,12 @@ plugins { } dependencies { + annotationProcessor libs.micronaut.serde.processor + api project(":aws-sdk-v2") api project(":aws-distributed-configuration") api libs.awssdk.secretsmanager + + implementation libs.micronaut.serde + implementation libs.micronaut.serde.jackson } diff --git a/aws-secretsmanager/src/main/java/io/micronaut/aws/secretsmanager/SecretsManagerKeyValueFetcher.java b/aws-secretsmanager/src/main/java/io/micronaut/aws/secretsmanager/SecretsManagerKeyValueFetcher.java index 5b739f2169..25d3665ef8 100644 --- a/aws-secretsmanager/src/main/java/io/micronaut/aws/secretsmanager/SecretsManagerKeyValueFetcher.java +++ b/aws-secretsmanager/src/main/java/io/micronaut/aws/secretsmanager/SecretsManagerKeyValueFetcher.java @@ -15,13 +15,12 @@ */ package io.micronaut.aws.secretsmanager; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import io.micronaut.aws.distributedconfiguration.KeyValueFetcher; import io.micronaut.context.annotation.BootstrapContextCompatible; import io.micronaut.context.annotation.Requires; import io.micronaut.core.annotation.Experimental; import io.micronaut.core.annotation.NonNull; +import io.micronaut.serde.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; @@ -39,6 +38,8 @@ import software.amazon.awssdk.services.secretsmanager.model.SecretListEntry; import software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException; import jakarta.inject.Singleton; + +import java.io.IOException; import java.util.Base64; import java.util.HashMap; import java.util.List; @@ -105,7 +106,7 @@ public Optional keyValuesByPrefix(@NonNull String prefix) { if (secretValueOptional.isPresent()) { try { result.putAll(objectMapper.readValue(secretValueOptional.get(), Map.class)); - } catch (JsonProcessingException e) { + } catch (IOException e) { if (LOG.isWarnEnabled()) { LOG.warn("could not read secret ({}) value from JSON to Map", secret.name()); } diff --git a/config/accepted-api-changes.json b/config/accepted-api-changes.json index c033dc6a48..63d8f1961a 100644 --- a/config/accepted-api-changes.json +++ b/config/accepted-api-changes.json @@ -1,77 +1,112 @@ [ + { + "type": "io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver", + "member": "Class io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver", + "reason": "Replacing Jackson mapping with Serde" + }, + { + "type": "io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver", + "member": "Constructor io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver(com.fasterxml.jackson.databind.ObjectMapper,io.micronaut.discovery.cloud.aws.AmazonMetadataConfiguration)", + "reason": "Replacing Jackson mapping with Serde" + }, + { + "type": "io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver", + "member": "Constructor io.micronaut.discovery.cloud.aws.AmazonComputeInstanceMetadataResolver()", + "reason": "Replacing Jackson mapping with Serde" + }, + { + "type": "io.micronaut.aws.alexa.httpserver.controllers.SkillController", + "member": "Class io.micronaut.aws.alexa.httpserver.controllers.SkillController", + "reason": "Replacing Jackson mapping with Serde" + }, + { + "type": "io.micronaut.aws.alexa.httpserver.controllers.SkillController", + "member": "Constructor io.micronaut.aws.alexa.httpserver.controllers.SkillController(com.fasterxml.jackson.databind.ObjectMapper,io.micronaut.aws.alexa.httpserver.services.RequestEnvelopeVerificationService,io.micronaut.aws.alexa.httpserver.services.RequestEnvelopeService)", + "reason": "Replacing Jackson mapping with Serde" + }, + { + "type": "io.micronaut.aws.secretsmanager.SecretsManagerKeyValueFetcher", + "member": "Class io.micronaut.aws.secretsmanager.SecretsManagerKeyValueFetcher", + "reason": "Replacing Jackson mapping with Serde" + }, + { + "type": "io.micronaut.aws.secretsmanager.SecretsManagerKeyValueFetcher", + "member": "Constructor io.micronaut.aws.secretsmanager.SecretsManagerKeyValueFetcher(software.amazon.awssdk.services.secretsmanager.SecretsManagerClient,com.fasterxml.jackson.databind.ObjectMapper)", + "reason": "Replacing Jackson mapping with Serde" + }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Class io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.objectMapper()", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.readerFor(java.lang.Class)", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.writerFor(java.lang.Class)", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Class io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.objectMapper()", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.readerFor(java.lang.Class)", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.writerFor(java.lang.Class)", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerContext", "member": "Class io.micronaut.function.aws.proxy.MicronautLambdaContainerContext", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerContext", "member": "Method io.micronaut.function.aws.proxy.MicronautLambdaContainerContext.getJsonCodec()", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerContext", "member": "Method io.micronaut.function.aws.proxy.MicronautLambdaContainerContext.getObjectMapper()", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler", "member": "Class io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.objectMapper()", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.readerFor(java.lang.Class)", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" }, { "type": "io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler", "member": "Method io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.writerFor(java.lang.Class)", - "reason": "Removal of Jackson for Serde" + "reason": "Replacing Jackson mapping with Serde" } ] \ No newline at end of file diff --git a/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/AbstractLambdaContainerHandler.java b/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/AbstractLambdaContainerHandler.java index c830efe7f1..e9562c954a 100644 --- a/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/AbstractLambdaContainerHandler.java +++ b/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/AbstractLambdaContainerHandler.java @@ -15,7 +15,6 @@ */ package io.micronaut.function.aws.proxy; - import com.amazonaws.serverless.exceptions.ContainerInitializationException; import com.amazonaws.serverless.proxy.LogFormatter; import com.amazonaws.serverless.proxy.internal.SecurityUtils; @@ -26,8 +25,6 @@ import com.amazonaws.serverless.proxy.SecurityContextWriter; import com.amazonaws.services.lambda.runtime.Context; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; import io.micronaut.json.JsonMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -234,12 +231,6 @@ public void proxyStream(InputStream input, OutputStream output, Context context) ResponseType resp = proxy(request, context); writerFor(responseTypeClass).writeValue(output, resp); - } catch (JsonParseException e) { - log.error("Error while parsing request object stream", e); - objectMapper().writeValue(output, exceptionHandler.handle(e)); - } catch (JsonMappingException e) { - log.error("Error while mapping object to RequestType class", e); - objectMapper().writeValue(output, exceptionHandler.handle(e)); } finally { output.flush(); output.close(); diff --git a/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyExceptionHandler.java b/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyExceptionHandler.java index 43d4a550a5..a8b49e424d 100644 --- a/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyExceptionHandler.java +++ b/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyExceptionHandler.java @@ -95,11 +95,8 @@ public void handle(Throwable ex, OutputStream stream) throws IOException { * @return The error json */ protected String getErrorJson(String message) { - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - environment - .getObjectMapper() - .writeValue(baos, new ErrorModel(message)); - return baos.toString(StandardCharsets.UTF_8.name()); + try { + return new String(environment.getObjectMapper().writeValueAsBytes(new ErrorModel(message)), StandardCharsets.UTF_8.name()); } catch (IOException e) { LOG.error("Could not produce error JSON", e); return "{ \"message\": \"" + message + "\" }"; diff --git a/function-aws-custom-runtime/src/main/java/io/micronaut/function/aws/runtime/AbstractMicronautLambdaRuntime.java b/function-aws-custom-runtime/src/main/java/io/micronaut/function/aws/runtime/AbstractMicronautLambdaRuntime.java index 31e1ff9cbf..570e04aa39 100644 --- a/function-aws-custom-runtime/src/main/java/io/micronaut/function/aws/runtime/AbstractMicronautLambdaRuntime.java +++ b/function-aws-custom-runtime/src/main/java/io/micronaut/function/aws/runtime/AbstractMicronautLambdaRuntime.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2020 original authors + * Copyright 2017-2022 original authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +21,6 @@ import com.amazonaws.services.lambda.runtime.RequestStreamHandler; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; import io.micronaut.core.annotation.NonNull; import io.micronaut.core.annotation.Nullable; import io.micronaut.context.ApplicationContext; @@ -50,9 +47,11 @@ import io.micronaut.http.client.HttpClient; import io.micronaut.context.env.CommandLinePropertySource; import io.micronaut.logging.LogLevel; +import io.micronaut.serde.ObjectMapper; import io.micronaut.serde.annotation.SerdeImport; import java.io.Closeable; +import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.MalformedURLException; @@ -308,12 +307,11 @@ protected APIGatewayProxyResponseEvent respond(HttpStatus status, String body, S * * @param request Request obtained from the Runtime API * @return if the request and the handler type are the same, just return the request, if the request is of type {@link APIGatewayProxyRequestEvent} it attempts to build an object of type HandlerRequestType with the body of the request, else returns {@code null} - * @throws JsonProcessingException if underlying request body contains invalid content - * @throws JsonMappingException if the request body JSON structure does not match structure + * @throws IOException if underlying request body contains invalid content or the request body JSON structure does not match structure * expected for result type (or has other mismatch issues) */ @Nullable - protected HandlerRequestType createHandlerRequest(RequestType request) throws JsonProcessingException, JsonMappingException { + protected HandlerRequestType createHandlerRequest(RequestType request) throws IOException { if (requestType == handlerRequestType) { return (HandlerRequestType) request; } else if (request instanceof APIGatewayProxyRequestEvent) { @@ -452,8 +450,8 @@ protected String serializeAsJsonString(Object value) { ObjectMapper objectMapper = applicationContext.getBean(ObjectMapper.class); try { return objectMapper.writeValueAsString(value); - } catch (JsonProcessingException e) { - return null; + } catch (IOException e) { + e.printStackTrace(); } } } @@ -466,12 +464,11 @@ protected String serializeAsJsonString(Object value) { * @param valueType Class Type to be read into * @param Type to be read into * @return a new Class build from the JSON String - * @throws JsonProcessingException if underlying input contains invalid content - * @throws JsonMappingException if the input JSON structure does not match structure + * @throws IOException if underlying input contains invalid content, or does not match structure * expected for result type (or has other mismatch issues) */ @Nullable - protected T valueFromContent(String content, Class valueType) throws JsonProcessingException, JsonMappingException { + protected T valueFromContent(String content, Class valueType) throws IOException { if (content == null) { return null; }