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

SpringBootLambdaContainerHandler doesn't set the contextPath #84

Closed
agibalov opened this issue Dec 20, 2017 · 5 comments
Closed

SpringBootLambdaContainerHandler doesn't set the contextPath #84

agibalov opened this issue Dec 20, 2017 · 5 comments
Assignees
Milestone

Comments

@agibalov
Copy link

agibalov commented Dec 20, 2017

Spring provides a nice mechanism to build URLs by referencing controller classes and their handler methods:

URI personUri = fromMethodCall(on(PersonsController.class)
  .getPerson(123))
  .build().toUri();

Internally, this mechanism relies on the current ServletRequest and namely its getContextPath() method.

I see that while my AwsProxyRequest has these details:

  • getPath(): /hello
  • getRequestContext().getPath(): /Dummy/hello
  • getResource(): /{proxy+}

SpringBootLambdaContainerHandler creates the ServletRequest object like this:

  • getPathInfo(): /hello
  • getPathTranslated(): null
  • getContextPath():
  • getServletPath(): /hello
  • getServletContext().getContextPath(): /

As a result, when I build the URL, the API Gateway's "stage" part is lost ("Dummy" is a stage) and instead of http://apigw/Dummy/hello, I get http://apigw/hello.

We're using aws-serverless-java-container-spring:0.8 with spring-boot-starter-web:1.5.3.RELEASE.

@sapessi
Copy link
Collaborator

sapessi commented Dec 20, 2017

Thanks for the feedback @loki2302 - I believe older versions of the library set the content path from the stage. We removed this because it was causing issues in other places. I'll test again to figure out what was happening, if possible I'll add the stage back in the context path.

@sapessi sapessi self-assigned this Jan 8, 2018
@sapessi sapessi added this to the Release 0.9 milestone Jan 8, 2018
@sapessi
Copy link
Collaborator

sapessi commented Jan 11, 2018

Hey @agibalov, I went back to explore this. The reason why I stayed away from including the stage name in the path is that stages don't necessarily map to servlets. Further, if you configure a custom domain name in API Gateway and point it directly to a stage the base path goes away. One change I'm contemplating is:

  1. Include the stage name as the context path
  2. Include the actual request path only in the PathInfo

With these changes, you will need to set the strip base path and base path properties in the ContainerConfig object to make it work with custom domain names - from the Lambda function I have no information on what the endpoint uri is.

@sapessi
Copy link
Collaborator

sapessi commented Jan 11, 2018

I've looked a bit deeper into this. There is no easy way for me to claim that the context path is the stage (see custom domain name issue above). Instead, I'm relying on the ContainerConfig object to tell me whether the stage should be included or not in the context path, same story for the base path you can strip. The getContextPath() method changes to:

@Override
public String getContextPath() {
    if (config.isUseStageAsServletContext()) {
        String contextPath = cleanUri(request.getRequestContext().getStage());
        if (config.getServiceBasePath() != null) {
            contextPath += cleanUri(config.getServiceBasePath());
        }

        return contextPath;
    } else {
        return "" + (config.getServiceBasePath() != null ? cleanUri(config.getServiceBasePath()) : "");
    }
}

To get the desired effect, you can initialize the container handler with:

SpringLambdaContainerHandler.getContainerConfig().setUseStageAsServletContext(true);

AwsProxyResponse output = handler.proxy(request, lambdaContext);

sapessi added a commit that referenced this issue Jan 11, 2018
…Mapper to speed up initialization and address #91. Added unit tests to make sure #90 is resolved and stays resolved.
@sapessi
Copy link
Collaborator

sapessi commented Jan 12, 2018

Closing this issue as it should be resolved in the latest commit

@sapessi sapessi closed this as completed Jan 12, 2018
@agibalov
Copy link
Author

@sapessi I've just upgraded to 0.9 and removed all the custom code I had to write to make Spring MVC URL builder functionality work in 0.8. Thanks!

sapessi added a commit that referenced this issue Jan 31, 2018
… bug introduced with the fixes for #84. Added unit tests
sapessi added a commit that referenced this issue Jan 31, 2018
… bug introduced with the fixes for #84. Added unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants