Skip to content

fix: answer a wrong-typed key on GetItem, DeleteItem, UpdateItem and TransactGetItems with the schema message - #826

Open
LeeroyHannigan wants to merge 4 commits into
mainfrom
fix/701-key-schema-mismatch-message
Open

LeeroyHannigan wants to merge 4 commits into
mainfrom
fix/701-key-schema-mismatch-message

Conversation

@LeeroyHannigan

Copy link
Copy Markdown
Collaborator

What

GetItem, DeleteItem and UpdateItem now answer a key element of the wrong type with ValidationException: The provided key element does not match the schema. A TransactGetItems member with such a key cancels the transaction with a ValidationError reason that carries the same message. PutItem keeps One or more parameter values were invalid: Type mismatch for key <name> expected: <type> actual: <type>.

The fix has two parts:

  • validate_get_item, validate_delete_item and validate_update_item in crates/core/src/validation/mod.rs apply remap_key_type_mismatch. The batch and transaction key checks already use this remap.
  • transact_get_items on SQLite, PostgreSQL, MongoDB and Cassandra checks each key with validate_batch_key_only in place of validate_key_only. The TransactWriteItems members already use that check on SQLite and PostgreSQL.

BatchGetItem keys, BatchWriteItem delete requests and ConditionCheck members do not change. #701 lists them as not measured. No page under docs/ describes these messages, so no document changes.

Why

On main, all five operations answer a wrong-typed key with the PutItem message. #701 has the Amazon DynamoDB answers, measured in us-east-1 on 31 October with each request sent twice. PutItem is the only one of the five that matches the service today.

Closes #701

Testing done

  • New tests in crates/core/src/validation/key_schema_mismatch_tests.rs cover GetItem, DeleteItem and UpdateItem, and pin the PutItem message. Without the change, the three operation tests fail and the PutItem test passes. With the remap applied to PutItem as well, the PutItem test fails.
  • a_wrong_typed_key_cancels_with_the_schema_message in crates/storage-sqlite/src/data/transactions_tests.rs sends a wrong-typed key through transact_get_items. It fails without the change. The PostgreSQL, MongoDB and Cassandra edits are the same one-line change, and no new test covers them.
  • cargo +1.97.0 fmt --all -- --check: clean.
  • cargo +1.97.0 clippy --locked --all-targets -- -D warnings on extenddb-core, extenddb-storage-sqlite, extenddb-storage-postgres, extenddb-storage-mongodb and extenddb-storage-cassandra: clean.
  • cargo +1.88.0 test --locked on the same five crates: extenddb-core 769 passed, extenddb-storage-sqlite 236 passed, and the PostgreSQL, MongoDB and Cassandra unit tests passed. The tests that need a live PostgreSQL, MongoDB or Cassandra server did not run.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

…Item with the schema message

GetItem, DeleteItem and UpdateItem now answer a key element whose type
does not match the table's key schema with ValidationException "The
provided key element does not match the schema", which is the service's
answer. PutItem keeps "One or more parameter values were invalid: Type
mismatch for key <name> expected: <type> actual: <type>".

The three validators apply the remap that the batch and transaction key
checks already use. The new tests cover each operation and pin the
PutItem message.

Refs #701
…chema message

A TransactGetItems member whose key element has the wrong type now
cancels the transaction with a ValidationError reason carrying "The
provided key element does not match the schema", which is the service's
answer. The SQLite, PostgreSQL, MongoDB and Cassandra backends each
validated these keys with the PutItem wording; they now use the key check
the transaction write members already use on SQLite and PostgreSQL.

The new SQLite test sends a wrong-typed key through transact_get_items.

Refs #701
crates/core/src/validation/mod.rs keeps the restorable_item re-export from main and the key_schema_mismatch_tests module from this branch.

@hitesh96db hitesh96db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required before approval (inline comments): (1) a wrong-named key still gets the old text on the four operations this PR changes, (2) TransactGetItems reports every bad member where DynamoDB reports the first, (3) the MongoDB and Cassandra TransactWriteItems members still answer the old text, which makes the new doc comment false, (4) one wire test that asserts the message, because nothing fails today if the PostgreSQL, MongoDB or Cassandra line is reverted. I will approve once those are done. No request answers worse than main: across 557 request shapes on DynamoDB and three SQLite builds (PR head, main and the merge), the PR fixes 59 cells and regresses none, and the merge into current main answers the same as the head. Each shape went to DynamoDB twice with identical answers.

Optional, same on main, so not conditions:

  • A wrong-typed key nested past 32 levels now answers the schema text. DynamoDB answers the nesting text first (Nesting Levels have exceeded supported limits: Attributes in the item have nested levels beyond supported limit, with the prefix 1 validation error detected: on DeleteItem and UpdateItem).
  • TransactGetItems [Get {"pk":{"S":""}}] answers TransactionCanceledException with the empty-string reason. DynamoDB answers a top-level ValidationException (One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: pk).
  • UpdateItem checks the key before a malformed number in ExpressionAttributeValues. DynamoDB answers 1 validation error detected: The parameter cannot be converted to a numeric value: abc first. #1385 edits the same lines of validate_update_item, so the two PRs will conflict.
  • A Scan with an empty-string hash in ExclusiveStartKey returns data with 200, and DynamoDB refuses it.

The branch is 64 commits behind main. The head alone sends message where DynamoDB sends Message on TransactionCanceledException, 143 responses in my grid. main fixed this in #774, and the merge answers like DynamoDB.

Not measured: PostgreSQL, MongoDB and Cassandra servers (the unit tests of those crates build and pass), tables with N or B keys, local and global index ExclusiveStartKey, PartiQL.

/// message.
/// Real `DynamoDB` uses that message for a wrong-typed key element on
/// `GetItem`, `DeleteItem`, `UpdateItem`, `TransactGetItems`, and the batch and
/// transaction operations. `PutItem` keeps the "Type mismatch" message.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remap rewrites only text that contains Type mismatch for key , so it fixes the wrong-type subset of "a key that does not match the schema" and nothing else. The issue asks for the schema text on a key that does not match the table's key schema.

A key with the right number of attributes and the wrong names still answers Missing the key pk in the item on GetItem, DeleteItem, UpdateItem and as the TransactGetItems reason. Measured in us-east-1 on 2026-11-30, twice, identical: GetItem {"TableName":"<hash table>","Key":{"other":{"S":"x"}}} gives DynamoDB ValidationException: The provided key element does not match the schema and this PR One or more parameter values were invalid: Missing the key pk in the item. The same holds for {"sk": N, "x": S} on a composite table, and DynamoDB gives the schema text for BatchGetItem, BatchWriteItem delete and the TransactWriteItems ConditionCheck, Delete and Update members as well. main answers the same as this PR, so it is not a regression. Extend the remap to the Missing the key text for these callers, or narrow the title and the body to the wrong-type case and file the rest.

The doc comment on line 1541 says DynamoDB uses this text for "the batch and transaction operations". On MongoDB (crates/storage-mongodb/src/data_engine.rs lines 2751, 2843 and 3021) and Cassandra (crates/storage-cassandra/src/data/transactions.rs lines 726, 827 and 921) the TransactWriteItems Delete, Update and ConditionCheck members still call validate_key_only with no remap. resolve_key_faults changes the code to ValidationError and keeps the message, so these two backends answer Type mismatch for key pk expected: S actual: N as the reason, where DynamoDB, SQLite and PostgreSQL answer the schema text (measured on DynamoDB for all three members, SQLite on the PR head). I traced MongoDB and Cassandra by reading and did not run them, and no test in either crate sends a wrong-typed member key. The issue lists the ConditionCheck member as not measured. Apply the remap at those six sites, since the PR already edits the transaction file of both backends.

let mut any_failed = false;
for op in ops {
match validation::validate_key_only(
match validation::validate_batch_key_only(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TransactGetItems gives a ValidationError reason to every failing member, and the size pass over all members runs in the engine before the type check. DynamoDB reports only the first failing member, with None for the others, and checks type before size inside a member. Measured in us-east-1 on 2026-11-30, twice, identical:

  • 100 members, each with pk N: DynamoDB gives 1 ValidationError (member 0) and 99 None. This PR gives 100 ValidationError.
  • [Get pk N "1", Get pk S of 3000 bytes] on a hash-only table: DynamoDB gives [{"Code":"ValidationError","Message":"The provided key element does not match the schema"},{"Code":"None"}]. This PR gives [{"Code":"None"},{"Code":"ValidationError","Message":"One or more parameter values were invalid: Size of hashkey has exceeded the maximum size limit of2048 bytes"}].
  • [Get pk B of 3000 bytes]: DynamoDB's reason is the schema text. This PR's reason is the hashkey size text.

TransactWriteItems already keeps only the first bad member through CancellationReason::resolve_key_faults (crates/core/src/types/transaction.rs). The TransactGetItems lines edited here build CancellationReason::validation_error for each failing member and never call it. The PR's claim that a bad member cancels with the schema reason holds for a single bad member whose key is inside the size limit. Call resolve_key_faults, run the type check ahead of the size check, or say in the body that it covers that case. main answers the same, so this is not a regression.

let mut any_failed = false;
for (key_info, key) in ops {
match validation::validate_key_only(
match validation::validate_batch_key_only(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing fails if this line is reverted. With validate_batch_key_only( put back to validate_key_only( at this line, cargo test --locked --no-fail-fast -p extenddb-storage-mongodb passes (12 binaries, 283 passed). The same revert in crates/storage-postgres/src/data/transactions.rs:47 (452 passed) and in crates/storage-cassandra/src/data/transactions.rs:239 (454 passed) leaves each crate green. The only backend test that sends a bad key to transact_get_items sends a missing sort key and asserts reasons[0].code == "ValidationError".

The wire tests that exist assert only the code. tests/test_item_operations.py::TestGetItem::test_get_wrong_key_type asserts Code == "ValidationException", and tests/test_transaction_operations.py::test_transact_get_wrong_key_type asserts the reason code. Those two plus test_transact_write_{put,delete,update}_wrong_key_type and test_put_wrong_key_type give 6 passed on a SQLite server built from this head and 6 passed on one built from main, which answers the old text on all of them. CI runs this wire suite on all four backends, so one Python test that asserts the message pins all four changes. Measured expectations (DynamoDB, twice each, identical): GetItem, DeleteItem and UpdateItem with pk N 1 on a hash table, and with sk N 1 on a composite table, answer ValidationException: The provided key element does not match the schema. DeleteItem with ReturnValues ALL_OLD answers the same. PutItem keeps One or more parameter values were invalid: Type mismatch for key pk expected: S actual: N. TransactGetItems with one bad member answers TransactionCanceledException with reasons [ValidationError "The provided key element does not match the schema"], and with a good first member and a bad sk N 1 second member [None, ValidationError "<schema text>"]. Also pin a Binary key (pk B) and a sk N case, which change with this PR and have no test.

@LJMart11 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 173a56a. 3 findings: 2 Important, 1 Docs.

The change maps a wrong-typed key on GetItem, DeleteItem, UpdateItem and TransactGetItems to The provided key element does not match the schema, and keeps PutItem's Type mismatch text. On SQLite, a pk sent as N against an S key answered the schema message on all three single-item operations and as the TransactGetItems and TransactWriteItems Update reason; DynamoDB answered the same text for the three single-item operations. The PR merged with the current main tip without conflicts. cargo test -p extenddb-core -p extenddb-storage-sqlite passed (1263 and 336 tests in the two largest binaries, 0 failed) under 1.94.0, and reverting the SQLite call site fails the new SQLite test.

  • Important crates/core/src/validation/mod.rs:1195 — A key with the right count but the wrong attribute name still answers Missing the key pk in the item on GetItem, DeleteItem, UpdateItem and TransactGetItems, where DynamoDB answers the schema message.
  • Important crates/storage-postgres/src/data/transactions.rs:47 — Putting validate_key_only back at this call site (and on MongoDB and Cassandra) leaves every test green; only the SQLite call site is pinned.
  • Docs crates/core/src/validation/mod.rs:1602 — The new doc says the schema message applies to the transaction operations, but MongoDB and Cassandra TransactWriteItems members still call validate_key_only.

Docs — outside the diff

crates/core/src/validation/mod.rs:1602
Docs
The doc comment at mod.rs:1602-1608 says DynamoDB uses this message for 'the batch and transaction operations' and the remap applies to them. crates/storage-mongodb/src/data_engine.rs:2751,2843,3021 and crates/storage-cassandra/src/data/transactions.rs:725,826,920 still call validate_key_only for TransactWriteItems members, so those backends answer Type mismatch for key. Fix: narrow the comment, or switch those call sites to validate_batch_key_only.

Evidence: grep of validate_key_only callers on the head.

) -> Result<(), DynamoDbError> {
validate_table_name(&input.table_name, limits)?;
validate_key_only(&input.key, key_schema, attr_defs)?;
validate_key_only(&input.key, key_schema, attr_defs).map_err(remap_key_type_mismatch)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important
The remap at mod.rs:1195, :1227 and :1249 only rewrites text containing Type mismatch for key , so a key with the right attribute count and a wrong name still falls through validate_key_only (mod.rs:1553-1558) to One or more parameter values were invalid: Missing the key pk in the item. #701 asks for the schema message on a key that does not match the schema. On the head (SQLite), Key {"other":{"S":"x"}} on a table keyed on pk (S) answers that Missing-key text on GetItem, DeleteItem, UpdateItem, and as the TransactGetItems and TransactWriteItems Update ValidationError reason. DynamoDB (us-east-1, table noarev-826-key) answered ValidationException: The provided key element does not match the schema to GetItem, DeleteItem and UpdateItem for the same key; I did not get a TransactGetItems answer from the service (the CLI call printed no error line).

Fix: map the missing-key branch to the schema message in the same remap for these operations (keep PutItem's text), and add cases to key_schema_mismatch_tests.rs plus one in transactions_tests.rs for a wrong-named key.

Evidence: head run through extenddb-pytest --backend sqlite: wrongname-get ('ValidationException', 'One or more parameter values were invalid: Missing the key pk in the item'); the same message for delete, update and the TransactGetItems reason. Wrong-typed pk N 1 now answers the schema text on all of them, matching the service.

let mut any_failed = false;
for op in ops {
match validation::validate_key_only(
match validation::validate_batch_key_only(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important
The change at transactions.rs:47 (PostgreSQL), data_engine.rs:2291 (MongoDB) and transactions.rs:238 (Cassandra) has no test that fails if it is reverted. With validate_batch_key_only replaced by validate_key_only at all three, cargo test --lib -p extenddb-storage-postgres -p extenddb-storage-mongodb -p extenddb-storage-cassandra passes (MongoDB 192 tests shown; the live-server tests are ignored without a server). The same revert on SQLite fails a_wrong_typed_key_cancels_with_the_schema_message at transactions_tests.rs:81. Mutation survived; this guards the message a client reads on three of four backends.

Fix: add one wire test (pytest in tests/, run by the existing backend jobs) that sends a wrong-typed key to TransactGetItems and asserts the reason message, so each backend's call site is pinned. No migration.

Evidence: reverting crates/storage-sqlite/src/data/transactions.rs:35 fails the SQLite test; the three other reverts pass.

This branch has not been deployed

No deployments
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.

GetItem, DeleteItem, UpdateItem and TransactGetItems answer a wrong-typed key with the PutItem message

3 participants