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

DynamoDB client validate fails on non 'S' types #3793

Closed
pasolid opened this issue Jul 24, 2023 · 2 comments
Closed

DynamoDB client validate fails on non 'S' types #3793

pasolid opened this issue Jul 24, 2023 · 2 comments
Assignees
Labels
dynamodb response-requested Waiting on additional information or feedback.

Comments

@pasolid
Copy link

pasolid commented Jul 24, 2023

Describe the bug

I have a simple item I want to persist in dynamoDB that consists of numeric and boolean types.
When I reach put_item function it fails on validation.

The problem is that the validation is not consistent. According to the documentation the attributes should be provided in this way:

aws dynamodb get-item \
    --table-name ProductCatalog \
    --key '{"Id":{"N":"1"}}' 

when non-string values are also sent as strings. But when I do it I get error of invalid type.

Expected Behavior

Unified behavior according to the documentation

Current Behavior

If I try to put_item that looks like this - w/o wrapping numeric and boolean values in the double quotes:

{
    "tracking_id": {
        "S": "b73f1ed9-6a34-4057-b83d-c1f594242cc1"
    },
    "expiredAt": {
        "N": 1690211745
    },
    "is_valid": {
        "BOOL": true
    },
    "value": {
        "N": 6
    }
}

Then the error is:

{
    "level": "ERROR",
    "location": "add_item:31",
    "message": "Failed to persist item in requests, error: Parameter validation failed:\nInvalid type for parameter Item.expiredAt.N, value: 1690211710, type: <class 'int'>, valid types: <class 'str'>\nInvalid type for parameter Item.value.N, value: 6, type: <class 'int'>, valid types: <class 'str'>",
    "timestamp": "2023-07-24 15:10:11,120+0000",
    "service": "service_undefined",
    "exception": "Traceback (most recent call last):\n  File \"/var/task/src/repositories/request_repo.py\", line 27, in add_item\n    client.put_item(TableName=settings.DYN_DB_TABLE, Item=json.loads(json.dumps(item)))\n  File \"/var/runtime/botocore/client.py\", line 530, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n  File \"/var/runtime/botocore/client.py\", line 928, in _make_api_call\n    headers=additional_headers,\n  File \"/var/runtime/botocore/client.py\", line 992, in _convert_to_request_dict\n    api_params, operation_model\n  File \"/var/runtime/botocore/validate.py\", line 381, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\nbotocore.exceptions.ParamValidationError: Parameter validation failed:\nInvalid type for parameter Item.expiredAt.N, value: 1690211710, type: <class 'int'>, valid types: <class 'str'>\nInvalid type for parameter Item.value.N, value: 6, type: <class 'int'>, valid types: <class 'str'>",
    "exception_name": "ParamValidationError",
    "xray_trace_id": "1-64be9452-07a8185ad40a6e96150c3d28"
}

But if instead I try toput_item that looks like - wrapping numeric and boolean values in the double quotes:

{
    "tracking_id": {
        "S": "b73f1ed9-6a34-4057-b83d-c1f594242cc1"
    },
    "expiredAt": {
        "N": "1690211642"
    },
    "is_valid": {
        "BOOL": "True"
    },
    "value": {
        "N": "6"
    }
}

Then I get

{
    "level": "ERROR",
    "location": "add_item:31",
    "message": "Failed to persist item in requests, error: Parameter validation failed:\nInvalid type for parameter Item.is_odd.BOOL, value: True, type: <class "str">, valid types: <class "bool">",
    "timestamp": "2023-07-24 15:06:15,240+0000",
    "service": "service_undefined",
    "exception": "Traceback (most recent call last):\n  File \"/var/task/src/repositories/request_repo.py\", line 27, in add_item\n    client.put_item(TableName=settings.DYN_DB_TABLE, Item=json.loads(json.dumps(item)))\n  File \"/var/runtime/botocore/client.py\", line 530, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n  File \"/var/runtime/botocore/client.py\", line 928, in _make_api_call\n    headers=additional_headers,\n  File \"/var/runtime/botocore/client.py\", line 992, in _convert_to_request_dict\n    api_params, operation_model\n  File \"/var/runtime/botocore/validate.py\", line 381, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\nbotocore.exceptions.ParamValidationError: Parameter validation failed:\nInvalid type for parameter Item.is_odd.BOOL, value: True, type: <class "str">, valid types: <class "bool">",
    "exception_name": "ParamValidationError",
    "xray_trace_id": "1-64be9366-62a8562a90e58f622630a60b"
}

Reproduction Steps

Provided in current behavior

Possible Solution

No response

Additional Information/Context

No response

SDK version used

boto3 v.1.28.5

Environment details (OS name and version, etc.)

Mac Ventura 13.4.1

@pasolid pasolid added bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels Jul 24, 2023
@tim-finnigan tim-finnigan self-assigned this Jul 26, 2023
@tim-finnigan
Copy link
Contributor

Hi @pasolid thanks for reaching out. You referenced the AWS CLI dynamodb get-item command, and for CLI issues we recommend opening those here: https://github.com/aws/aws-cli/issues

Please note in that documentation it says:

Numbers are sent across the network to DynamoDB as strings, to maximize compatibility across languages and libraries. However, DynamoDB treats them as number type attributes for mathematical operations.

The error you referenced (An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema) suggests an issue with your schema. If you run the describe-table command it should look something like this:

image

And I can confirm that the command you shared works as expected:

image


Regarding the put_item command that you referenced, the documentation notes that the Bool value should be a boolean and not a string:

image

@tim-finnigan tim-finnigan added response-requested Waiting on additional information or feedback. dynamodb and removed bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels Jul 26, 2023
@pasolid
Copy link
Author

pasolid commented Jul 26, 2023

@tim-finnigan You are correct.

I found out this by myself a few hours later but forgot to close the issue.
I'm new to it so it was confusing.

@pasolid pasolid closed this as completed Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamodb response-requested Waiting on additional information or feedback.
Projects
None yet
Development

No branches or pull requests

2 participants