aws-amplify / aws-amplify/amplify-cli

AdminQueries lambda - limit not converted to number

Open
#12,735 3 comments 3 reactions 0 assignees View on GitHub
api-rest auth bug functions good first issue p2
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### How did you install the Amplify CLI?

npm

### If applicable, what version of Node.js are you using?

_No response_

### Amplify CLI Version

12

### What operating system are you using?

Ubuntu

### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

### Describe the bug

When amplify creates the admin queries a lambda is created to handle the calls.
When calling the listUsers endpoint with limit=20 (for example) the function fails status code 500 is returned.

```
2023-06-05T15:00:09.904+02:00 INFO Attempting to list users
2023-06-05T15:00:10.343+02:00 INFO SerializationException: STRING_VALUE cannot be converted to Integer at throwDefaultError (/var/runtime/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22) at deserializeAws_json1_1ListUsersCommandError (/var/runtime/node_modules/@aws-sdk/client-cognito-identity-provider/dist-cjs/protocols/Aws_json1_1.js:5075:51) at .... at async /var/task/app.js:170:18 { '$fault': 'client', '$metadata': { httpStatusCode: 400, requestId: 'e0e25dc8-b21d-48f6-bd2a-abd49f9d2f74', extendedRequestId: undefined, cfId: undefined, attempts: 1, totalRetryDelay: 0 }, __type: 'SerializationException' }
2023-06-05T15:00:10.421+02:00 ERROR STRING_VALUE cannot be converted to Integer
```

The amplify generated (wrong) code of the Lambda (app.js):
```
app.get('/listUsers', async (req, res, next) => {
try {
let response;
if (req.query.token) {
response = await listUsers(req.query.limit || 25, req.query.token);
} else if (req.query.limit) {
response = await listUsers((Limit = req.query.limit));
} else {
response = await listUsers();
}
res.status(200).json(response);
} catch (err) {
next(err);
}
});
```

I hot fixed the code like this:
```
app.get('/listUsers', async (req, res, next) => {
try {
let response;
let limit = Number(req.query?.limit || 25);
if (req.query.token) {
response = await listUsers(limit, req.query.token);
} else if (req.query.limit) {
response = await listUsers((Limit = limit));
} else {
response = await listUsers();
}
res.status(200).json(response);
} catch (err) {
next(err);
}
});
```

### Expected behavior

It should accept the limit parameter and pass it as a number.

### Reproduction steps

Add auth, add admin queries.
Run a list users query with limit param.

### Project Identifier

_No response_

### Log output

```
# Put your logs below this line

```

### Additional information

_No response_

### Before submitting, please confirm:

- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.

Contributor guide

Open the contributing guide

Research direction

Start with the generated Lambda code in app.js and the /listUsers endpoint shown in the report. Reproduce the request after adding auth and admin queries, then verify that a numeric limit is accepted and the endpoint no longer returns a 500 response.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript
Domain
api, authentication, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.