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

Handler event path and resource don't match pattern returned from lambda #1029

Closed
skinofstars opened this issue Jun 21, 2020 · 4 comments · Fixed by #1137
Closed

Handler event path and resource don't match pattern returned from lambda #1029

skinofstars opened this issue Jun 21, 2020 · 4 comments · Fixed by #1137

Comments

@skinofstars
Copy link

skinofstars commented Jun 21, 2020

Bug Report - Handler event path and resource don't match pattern returned from lambda

The serverless-offline handler event object is returning the prefixed /<stage> on the event.resource, when it should not.

The serverless-offline handler event object is not returning the prefixed /<stage> on the event.path, when it should.

There are also similar issues in the event.requestContext.path and event.requestContext.resourcePath.

Current Behavior

Currently, the event.path and event.resource are returned as follows:

In serverless-offline :

{
  path: "/example-route",
  resource: "/development/example-route",
  requestContext: {
    path: "/example-route",
    resourcePath: "/development/example-route",    
  },
  ...
}

In lambda - with default auto generated domain:

{
  path: "/example-route",
  resource: "/example-route",
  requestContext: {
    path: "/production/example-route",
    resourcePath: "/example-route"
  },
  ...
}

In lambda - with custom domain and path map:

{
  path: "/v1/example-route",
  resource: "/example-route", 
  requestContext: {
    path: "/v1/example-route"
    resourcePath: "/example-route"
  },
  ...
}

Sample Code

  • file: serverless.yml
service: my-service

plugins:
  - serverless-offline

provider:
  runtime: nodejs12.x
  stage: dev

functions:
  exampleRoute:
    events:
      - http:
          method: get
          path: example-route
    handler: handler.exampleRouteHandler
  • file: handler.js
exports.exampleRouteHandler = async function (event, context) {
  console.log('EVENT: \n' + JSON.stringify(event, null, 2));
  return context.logStreamName;
};

Expected behavior/code

I'd expect the event.path, event.resource, event.requestContext.path and requestContext.resourcePath to match at least one of the results from lambda.

Environment

  • serverless version: 1.72.0
  • serverless-offline version: 6.4.0

Additional context/Screenshots

Possibly related to #917

@parthpatwala
Copy link

I am using version 6.3.2 and still facing this issue. Getting the actual resource path in lambda '/example-route' like this and getting '/dev/example-route' while using offline.

@TrejGun
Copy link

TrejGun commented Aug 25, 2020

same here

@frozenbonito
Copy link
Contributor

This issue has not been resolved yet.
I investigated the difference in behavior between the actual AWS environment and serverless-offline (b74f49d).

serverless.yml

service: myservice

plugins:
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  region: ap-northeast-1
  stage: dev

functions:
  handler:
    handler: index.handler
    events:
      # simple path
      - http:
          method: get
          path: /route
      # path parameter
      - http:
          method: get
          path: /{id}/param
      # greedy path parameter
      - http:
          method: get
          path: /greedy/{proxy+}

index.js

'use strict'

const { stringify } = JSON

exports.handler = (event, context, callback) => {
  const { path, resource, requestContext } = event
  const {
    path: requestContextPath,
    resourcePath: requestContextResourcePath,
  } = requestContext

  const response = {
    path,
    resource,
    requestContext: {
      path: requestContextPath,
      resourcePath: requestContextResourcePath,
    },
  }

  callback(null, {
    statusCode: 200,
    body: stringify(response, null, 2),
  })
}

AWS

simple path

$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/route
{
  "path": "/route",
  "resource": "/route",
  "requestContext": {
    "path": "/dev/route",
    "resourcePath": "/route"
  }
}

with path parameter

$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/1/param
{
  "path": "/1/param",
  "resource": "/{id}/param",
  "requestContext": {
    "path": "/dev/1/param",
    "resourcePath": "/{id}/param"
  }
}

with greedy path parameter

$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/greedy/abc
{
  "path": "/greedy/abc",
  "resource": "/greedy/{proxy+}",
  "requestContext": {
    "path": "/dev/greedy/abc",
    "resourcePath": "/greedy/{proxy+}"
  }
}

with custom domain and base path mapping (base path is base)

$ curl https://mycustomdomain/base/route
{
  "path": "/base/route",
  "resource": "/route",
  "requestContext": {
    "path": "/base/route",
    "resourcePath": "/route"
  }
}

serverless-offline

simple path

  • requestContext.path should be /dev/route
  • requestContext.resourcePath should be /route
$ curl localhost:3000/dev/route
{
  "path": "/route",
  "resource": "/route",
  "requestContext": {
    "path": "/route",
    "resourcePath": "/dev/route"
  }
}

with path parameter

$ curl localhost:3000/dev/1/param
{
  "path": "/1/param",
  "resource": "/1/param",
  "requestContext": {
    "path": "/1/param",
    "resourcePath": "/dev/{id}/param"
  }
}

with greedy path parameter

  • resource should be /greedy/{proxy+}
  • requestContext.path should be /dev/greedy/abc
  • requestContext.resourcePath should be /greedy/{proxy+}
$ sls offline
$ curl localhost:3000/dev/greedy/abc
{
  "path": "/greedy/abc",
  "resource": "/greedy/abc",
  "requestContext": {
    "path": "/greedy/abc",
    "resourcePath": "/dev/greedy/{proxy*}"
  }
}

with --noPrependStageInUrl and --prefix base options (equivalent to custom domain and base path mapping)

  • resource should be /route
  • requestContext.resourcePath should be /route
$ curl localhost:3000/base/route
{
  "path": "/base/route",
  "resource": "/base/route",
  "requestContext": {
    "path": "/base/route",
    "resourcePath": "/base/route"
  }
}

@pkit
Copy link

pkit commented Sep 14, 2023

I'm not sure why this one is closed?

Resource path is still completely incorrect: /dev/bla/bla instead of /bla/bla

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

Successfully merging a pull request may close this issue.

6 participants