Skip to content

Make fields from com.amazonaws.serverless.proxy.model.AwsProxyRequest optional to set #1495

Open
@Vadym79

Description

@Vadym79

When using AWS Serverless Java Container with Spring Boot 3 and Lambda SnapStart with priming with I tested this type of priming implementation WebRequestPriming

Unfortunately I have to set so many fields of the com.amazonaws.serverless.proxy.model.AwsProxyRequest with dummy values and even create dummy objects like AwsProxyRequestContext and ApiGatewayRequestIdentity (see the method getAwsProxyRequest) in order not to get errors like:

at
com.amazonaws.serverless.exceptions.InvalidRequestEventException: The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:48)

or

java.lang.NullPointerException: Cannot invoke "com.amazonaws.serverless.proxy.model.ApiGatewayRequestIdentity.getAccessKey()" because the return value of "com.amazonaws.serverless.proxy.model.AwsProxyRequestContext.getIdentity()" is null

Here is the complete code I had to write in order to make it work:

private static AwsProxyRequest getAwsProxyRequest () {
	final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
	awsProxyRequest.setHttpMethod("GET");
	awsProxyRequest.setPath("/products/0");
	//awsProxyRequest.setResource("/products/{id}");
	awsProxyRequest.setPathParameters(Map.of("id","0"));
	final AwsProxyRequestContext awsProxyRequestContext = new AwsProxyRequestContext();
	final ApiGatewayRequestIdentity apiGatewayRequestIdentity= new ApiGatewayRequestIdentity();
	apiGatewayRequestIdentity.setApiKey("blabla");
	awsProxyRequestContext.setIdentity(apiGatewayRequestIdentity);
	awsProxyRequest.setRequestContext(awsProxyRequestContext);
	return awsProxyRequest;		
}

For my use case this will be enough to do the priming like this:

  final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
  awsProxyRequest.setHttpMethod("GET");
  awsProxyRequest.setPathParameters(Map.of("id","0"));

Others, that priming Post request would only like to set the body like this:

  final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
  awsProxyRequest.setHttpMethod("POST");
  awsProxyRequest.setBody("bla");

Many field should be optional to set to easily construct such objects or please provide static factory methods for it to save on the boilerplate code.

Compare it with how easy it is to implement the same type of SnapStart priming with

  1. Spring Cloud Function AWS Adapter

see my implementation of the method getAPIGatewayProxyRequestEventAsJson here AmazonAPIGatewayPrimingResource

as I only do

    final APIGatewayProxyRequestEvent proxyRequestEvent = new APIGatewayProxyRequestEvent ();
	proxyRequestEvent.setHttpMethod("GET");
	proxyRequestEvent.setPathParameters(Map.of("id","0"));
  1. Quarkus 3, see my implementation of the method getAwsProxyRequest here AmazonAPIGatewayPrimingResource

as I only do the same as above:

    final APIGatewayProxyRequestEvent proxyRequestEvent = new APIGatewayProxyRequestEvent ();
	proxyRequestEvent.setHttpMethod("GET");
	proxyRequestEvent.setPathParameters(Map.of("id","0"));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions