Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException if calling lambda without http headers #232

Closed
dhawalkp opened this issue Jan 20, 2019 · 5 comments
Closed

NullPointerException if calling lambda without http headers #232

dhawalkp opened this issue Jan 20, 2019 · 5 comments
Labels
Milestone

Comments

@dhawalkp
Copy link

dhawalkp commented Jan 20, 2019

Hi Stefano, As per our conversation over the chime, here is the Issue for the NPE

  • Framework version: 1.3
  • Implementations: Spring Boot

Scenario

I was recently trying out to develop a microservice using Java based lambda using spring boot. After following all the instruction, my requests continuously failed giving NullPointerException while testing the lambda endpoint using Test console of API Gateway. The API Gateway is using Lambda Proxy integration to route the endpoint params to lambda.

Expected behavior

It shouldnt give NPE

Actual behavior

Giving below exception

java.lang.NullPointerException
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:39)
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209)
at com.amazon.sa.SpringLambdaHandler.handleRequest(SpringLambdaHandler.java:29)
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$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
END RequestId: c6ca76ec-db10-454a-8ffe-449b84c3ac74

Steps to reproduce

  1. create a lambda spring boot handler as per the given instructions in this guide.
  2. Expose this function to API Gateway using Lambda Proxy Integration
  3. Test the API from API Gateway console
  4. Keep the Headers empty as default
  5. Get above NPE

Full log output

After reviewing your code of AwsProxyHttpServletRequestReader.java, I think as a best practice, we should write NPE free code as line below in highlighted in proned to NPE in case the Content-type header is not sent in the request. Although very rare to be null but still to have check on NPE in the code and avoid failing the requests.

if (request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE) != null) {
            String contentType = request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
            // put single as we always expect to have one and only one content type in a request.
            request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, getContentTypeWithCharset(contentType, config));
        }
@huksley
Copy link

huksley commented Feb 11, 2019

Any updates on this?
Same problem occurs if one adds to CloudFormation template event to run function periodically:

        CheckWebsiteScheduledEvent:
          Type: Schedule
          Properties:
            Schedule: rate(5 minutes)

UPDATE: Actually, in my case it is a little bit differnt, created separate issue: #237

@dhawalkp
Copy link
Author

@sapessi I can fix it if you like and you can review the code. Would that be fine?

@sapessi
Copy link
Collaborator

sapessi commented Feb 12, 2019

The issues are not related. Working to replicate and push a fix for this now. It will go out as a patch release.

@sapessi
Copy link
Collaborator

sapessi commented Feb 26, 2019

Closing since version 1.3.1 is out.

@sapessi sapessi closed this as completed Feb 26, 2019
@dhawalkp
Copy link
Author

@sapessi Thanks very much!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants