Skip to content

Update APIGatewayProxy types to better support APIGateway integration #7

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zfz7
Copy link
Contributor

@zfz7 zfz7 commented Feb 16, 2025

Hey,

Sorry I was trying to get this in earlier (saw that you just cut a release). But I am getting some serialization errors when integrating with API Gateway. It seems like in my setup API Gateway is providing a deploymentId and has a null version

Missing deploymentId logs:

START RequestId: 10043724-77f3-4c74-b3e0-fae7ffb2b79a Version: $LATEST
[ERROR] | An exception occurred:
Message: Illegal input: Encountered an unknown key 'deploymentId' at offset 2500 at path: $.requestContext
Use 'ignoreUnknownKeys = true' in 'Json {}' builder or '@JsonIgnoreUnknownKeys' annotation to ignore unknown keys.
JSON input: .....me":"api.daniel-eichman.com","deploymentId":"ffyam4","apiId".....
Stack Trace:
EventBodyParseException(cause=io.ktor.serialization.JsonConvertException: Illegal input: Encountered an unknown key 'deploymentId' at offset 2500 at path: $.requestContext
Use 'ignoreUnknownKeys = true' in 'Json {}' builder or '@JsonIgnoreUnknownKeys' annotation to ignore unknown keys.

Null version logs:

START RequestId: 24977765-5edd-44ba-87d1-0c6c1fc05d96 Version: $LATEST
[ERROR] | An exception occurred:
Message: Illegal input: Field 'version' is required for type with serial name 'com.backend.apigateway.APIGatewayProxy', but it was missing at path: $
Stack Trace:
EventBodyParseException(cause=io.ktor.serialization.JsonConvertException: Illegal input: Field 'version' is required for type with serial name 'com.backend.apigateway.APIGatewayProxy', but it was missing at path: $, context=Context(awsRequestId=24977765-5edd-44ba-87d1-0c6c1fc05d96, xrayTracingId=Root=1-67b25f47-4d1cd7ed332b0e2f3725c857;Parent=4276dcbc09f509dd;Sampled=0;Lineage=1:ffc3a1c7:0, deadlineTimeInMs=1739743050945, invokedFunctionArn=arn:aws:lambda:us-west-2:<AWS_ACCOUNT>:function:Tetris-Service-Stack-Beta-TetrisApiLambdaBB313040-X7ZlUgkFQ3O0, invokedFunctionName=Tetris-Service-Stack-Beta-TetrisApiLambdaBB313040-X7ZlUgkFQ3O0, invokedFunctionVersion=$LATEST, memoryLimitMb=128, clientContext=null, cognitoIdentity=null), message=Illegal input: Field 'version' is required for type with serial name 'com.backend.apigateway.APIGatewayProxy', but it was missing at path: $, type=Runtime.InvalidEventBodyError)

###Testing

  • ./gradlew build
  • Deployed to AWS and was able to resolve API requests

@trueangle
Copy link
Owner

Hey @zfz7 thanks for noticing the issue. Not all events from that package were fully tested, so some of them might contain wrong schema description.

It looks like these two fields are not used by lambda whatsoever. Looking at schema example

@trueangle
Copy link
Owner

I updated the schema and added a test. Could you please confirm if it works on your side?

@zfz7
Copy link
Contributor Author

zfz7 commented Feb 23, 2025

Thanks for looking into this, unfortunately it doesn't work quite right. I am still getting the following error

Message: Illegal input: Encountered an unknown key 'deploymentId' at offset 2500 at path: $.requestContex

You may consider

Use 'ignoreUnknownKeys = true' in 'Json 
{}
' builder or '@JsonIgnoreUnknownKeys' annotation to ignore unknown keys.

I also lost access to operationName which does exist in the model, for context I am using this class in for the jvm version for the lambda.

Here is an example request body

{
  resource=/info
  path=/info,
  httpMethod=GET,
  headers={
      accept=*/*
      accept-encoding=gzip,
      deflate,
      br,
      zstd,
      accept-language=en-US,en;q=0.9,
      Host=api.example.com,
      origin=https://example.com,
      priority=u=1,
      i,
      referer=https://example.com/,
      sec-ch-ua="Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133", sec-ch-ua-mobile=?0, sec-ch-ua-platform="macOS",
      sec-fetch-dest=empty,
      sec-fetch-mode=cors,
      sec-fetch-site=same-site,
      User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36,
      X-Amzn-Trace-Id=Root=1-67bb9e8f-3b128a166af28a7273680d03,
      X-Forwarded-For=xxx.xxx.xxx.xxx,
      X-Forwarded-Port=443,
      X-Forwarded-Proto=https
  },
  multiValueHeaders={
     accept=[*/*],
     accept-encoding=[gzip, deflate, br, zstd],
     accept-language=[en-US,en;q=0.9],
     Host=[api.example.com],
     origin=[https://example.com],
     priority=[u=1, i],
     referer=[https://example.com/],
     sec-ch-ua=["Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"],
     sec-ch-ua-mobile=[?0],
     sec-ch-ua-platform=["macOS"],
     sec-fetch-dest=[empty],
     sec-fetch-mode=[cors],
     sec-fetch-site=[same-site],
     User-Agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36],
     X-Amzn-Trace-Id=[Root=1-67bb9e8f-3b128a166af28a7273680d03], X-Forwarded-For=[xxx.xxx.xxx.xxx],
     X-Forwarded-Port=[443],
     X-Forwarded-Proto=[https]
  },
  queryStringParameters=null,
  multiValueQueryStringParameters=null,
  pathParameters=null,
  stageVariables=null,
  requestContext={
      resourceId=7a4bd1
      resourcePath=/info,
      operationName=Info,
      httpMethod=GET,
      extendedRequestId=GdW2iGaGPHcETpg=,
      requestTime=23/Feb/2025:22:17:51 +0000,
      path=/info,
      accountId=<AWS_ACC>,
      protocol=HTTP/1.1,
      stage=Beta,
      domainPrefix=api,
      requestTimeEpoch=1740349071868,
      requestId=b2c0992e-32bd-424d-9480-25abe51b2355,
      identity={
          cognitoIdentityPoolId=null
          accountId=null,
          cognitoIdentityId=null,
          caller=null,
          sourceIp=<xxx.xxx.xxx.xxx>,
          principalOrgId=null,
          accessKey=null,
          cognitoAuthenticationType=null,
          cognitoAuthenticationProvider=null,
          userArn=null,
          userAgent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36,
          user=null
      },
      domainName=api.example.com,
      deploymentId=ffyam4,
      apiId=d8dhmibzph
   },
   body=null,
   isBase64Encoded=false
}

@trueangle
Copy link
Owner

Interesting, deploymentId seems to be not included in APIGatewayProxyRequestEvent.java as well

@zfz7
Copy link
Contributor Author

zfz7 commented Mar 12, 2025

Kotlin X Serialization will throw an exception if it encounters an unknown key. We can also consider setting @JsonIgnoreUnknownKeys

https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-ignore-unknown-keys/

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 this pull request may close these issues.

2 participants