micronaut-projects / micronaut-aws

Projects specific to integrating Micronaut and Amazon Web Services (AWS)
Apache License 2.0
87 stars 80 forks source link

Cant not deserialize request to HttpRequest<String> when deployed in lambda with API Gateway #1184

Open drew-corporate-creations opened 3 years ago

drew-corporate-creations commented 3 years ago

Expected Behavior

This works locally using the netty server. However it does not work when deployed in lambda with API Gateway.

Using a post method in a controller:

    @Post("/webhooks")
    public Object webhook(HttpRequest<String> request) throws Exception {

        //Never gets here

       log.info("Received request");

        String body = request.getBody().get();

        log.info("Request body: {}", body);

        return null;
}

My goal is to get the body of the request as a string. Expected behavior is a 200 response with logging lines showing the request body.

Actual Behaviour

Below is the stack trace in CloudWatch for when I post with a payload of: {"Some": "Test"}

[18:21:08.231] ERROR c.a.s.p.AwsProxyExceptionHandler - Called exception handler for:
io.micronaut.http.codec.CodecException: Error decoding JSON stream for type [B]: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{
    "Some": "Test"
}"; line: 1, column: 1]
    at io.micronaut.jackson.codec.JacksonMediaTypeCodec.decode(JacksonMediaTypeCodec.java:224)
    at io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.decodeRequestBody(MicronautLambdaContainerHandler.java:479)
    at io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.executeRoute(MicronautLambdaContainerHandler.java:516)
    at io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.lambda$null$2(MicronautLambdaContainerHandler.java:322)
    at io.reactivex.internal.operators.flowable.FlowableDefer.subscribeActual(FlowableDefer.java:35)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:57)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorNext.subscribeActual(FlowableOnErrorNext.java:40)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.blockingFirst(Flowable.java:5698)
    at io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.lambda$handleRequest$9(MicronautLambdaContainerHandler.java:367)
    at io.micronaut.http.context.ServerRequestContext.with(ServerRequestContext.java:68)
    at io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.handleRequest(MicronautLambdaContainerHandler.java:303)
    at io.micronaut.function.aws.proxy.MicronautLambdaContainerHandler.handleRequest(MicronautLambdaContainerHandler.java:80)
    at io.micronaut.function.aws.proxy.AbstractLambdaContainerHandler.proxy(AbstractLambdaContainerHandler.java:205)
    at io.micronaut.function.aws.proxy.MicronautLambdaHandler.handleRequest(MicronautLambdaHandler.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:263)
    at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:180)
    at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:902)
    at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:340)
    at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:63)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:150)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{
    "Some": "Test"
}"; line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1601)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1375)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1280)
    at com.fasterxml.jackson.databind.DeserializationContext.extractScalarFromObject(DeserializationContext.java:872)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3516)
    at io.micronaut.jackson.codec.JacksonMediaTypeCodec.decode(JacksonMediaTypeCodec.java:221)
    ... 29 common frames omitted

Steps To Reproduce

To reproduce, create a micronaut controller with a post method that takes an (HttpRequest) OR (@Body String body). Deploy this to aws with API Gateway in front using proxy+.

Environment Information

Java 8 Micronaut 2.5.4

Example Application

No response

Version

2.5.4

sdelamo commented 3 years ago

can you provide an example app which reproduces?

drew-corporate-creations commented 3 years ago

can you provide an example app which reproduces?

https://github.com/drew-corporate-creations/demo

I Created a demo app using micronaut launch, added 2 post methods to the controller, then added a cloud formation file and deployment script. To deploy it, run ./deploy.sh. 4 params are required to deploy it (you'll see them in the file). After deployment, grab the url from API Gateway. Make a POST to the 2 endpoints using {"Some": "Test"} as the body.

https:///Stage/doesNotWork1 https:///Stage/doesNotWork2

Check the CloudWatch logs and you will see the error.

sdelamo commented 3 years ago

are you creating the API Gateway with Cloud formation?

drew-corporate-creations commented 3 years ago

Yes. In the sample project there is a cloud formation template. I used AWS SAM (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html) and cloud formation cli to deploy it. There's a deployment script in the sample project as well. However you'll get the same result if you don't use cloud formation.

I realize in my previous post it should say

https://\<api-gateway-url>/Stage/doesNotWork1 https://\<api-gateway-url>/Stage/doesNotWork2

amitdusane commented 2 years ago

Any update on this issue?