Repository navigation
#443 support null values - #444
mumenthalers wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.
Tip: disable this comment in your organization's Code Review settings.
|
coverage: 95.537% (+0.04%) from 95.498% — #443-support-null-values into next |
|
@claude review |
| if (Array.isArray(obj) || obj instanceof Set) { | ||
| const returnArr: unknown[] = [] | ||
| for (const i of obj) { | ||
| const item: unknown = deepFilter(i, filterFn) | ||
| if (item !== undefined) { | ||
| returnArr.push(item) | ||
| } | ||
| }) | ||
|
|
||
| return returnArr | ||
| } else if (obj instanceof Set) { | ||
| const returnArr: any[] = [] | ||
| Array.from(obj).forEach((i) => { | ||
| const item = deepFilter(i, filterFn) | ||
| if (item !== null) { | ||
| returnArr.push(item) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| return returnArr.length ? new Set(returnArr) : null | ||
| return Array.isArray(obj) ? returnArr : new Set(returnArr) | ||
| } else if (isPlainObject(obj)) { | ||
| const returnObj: Record<string, any> = {} | ||
|
|
||
| for (const key in obj) { | ||
| if (obj.hasOwnProperty(key)) { | ||
| const value = obj[key] | ||
| const item = deepFilter(value, filterFn) | ||
| if (item !== null) { | ||
| returnObj[key] = item | ||
| } | ||
| const returnObj: Record<string, unknown> = {} | ||
|
|
||
| for (const [key, value] of Object.entries(<Record<string, unknown>>obj)) { | ||
| const item: unknown = deepFilter(value, filterFn) | ||
| if (item !== undefined) { | ||
| returnObj[key] = item | ||
| } | ||
| } | ||
|
|
||
| return returnObj | ||
| } else { | ||
| if (filterFn(obj)) { | ||
| return obj | ||
| } else { | ||
| return null | ||
| } | ||
| return filterFn(obj) ? obj : undefined |
There was a problem hiding this comment.
🔴 in() conditions and add/removeFromSet updates that include a null in their value list now throw, where the base silently dropped the null. deepFilter now keeps nulls nested inside arrays and Sets. The IN builder and the set mapper reject them as NULL. Fix: strip null items from list-valued operands that only accept scalars, so nulls are dropped from IN lists and add/removeFromSet values as before. Top-level set(null)/eq(null) must keep their NULL value.
Why this was flagged
Trigger: a caller writes attribute('status').in(['a', null]), or an update add('tags', ['x', null]) / removeFromSet(...) with a list holding null. This happens when the list is built from optional fields. Base: deepFilter (condition-expression-builder.ts:97, update-expression-builder.ts:40) returned null for null leaves and dropped them, so IN got ['a'] and add got Set(['x']), and the request succeeded. Now deepFilter keeps null (line 73 returns undefined only for filtered items). In buildInConditionExpression, toDbOne(null) gives {NULL:true}, and validateAttributeType('IN condition', ..., 'S','N','B') at condition-expression-builder.ts:169 throws "Type NULL ... is not allowed for IN condition". For add/removeFromSet, update-expression-builder.ts:95 builds new Set(['x', null]). collectionToDb then throws "null values are not allowed in a SS set" (collection.mapper.ts:88), or detectCollectionTypeFromValue throws on the mixed set. The migration note only covers top-level set(null)/equals(null), not these list operands.
Verification: Trigger: a caller passes a list that contains null to in(...), add(...) or removeFromSet(...). deepFilter now marks removed items with undefined, so nested nulls are kept. In buildInConditionExpression, line 169 runs validateAttributeType('IN condition', mappedValue, 'S', 'N', 'B'), which throws. v2-v3-migration.md does not describe this, so calls that worked on the base now throw.
| if (propertyValue === null) { | ||
| if (propertyMetadata && propertyMetadata.key) { | ||
| throw new Error(`${propertyMetadata.name.toString()} is null but is a ${propertyMetadata.key.type} key`) | ||
| } | ||
| if (isIndexKey(propertyMetadata)) { | ||
| // index key attributes must not be NULL -> omit the attribute (item won't be part of the sparse index) | ||
| return null | ||
| } |
There was a problem hiding this comment.
🔴 Saving an item that embeds another @ Model class whose key property is null now throws, where the base skipped the null field. Key rules from toDbOne at mapper.ts:137-144 also run inside nested objects and list items. That is because toDb recurses through ObjectMapper and CollectionMapper with the nested class. Fix: enforce key and index-key rules (null throws, null omitted, the S/N/B check at mapper.ts:89-90) only for the top-level table item, and map nested nulls to { NULL: true } like other properties.
Why this was flagged
The trigger is a model property typed with another @ Model class that has a @ PartitionKey or @ SortKey, or a @ CollectionProperty({ itemType }) of such a class, where the nested key value is null. Whether user models nest keyed classes this way cannot be enumerated from this checkout. Write path: put, batchWrite, transactPut or update().set(obj) call toDbOne, then ObjectMapper.toDb (object.mapper.ts:21), then toDb(value, NestedClass). For the nested key property, toDbOne(null, keyMeta) reaches mapper.ts:138-139 and throws " is null but is a HASH key". On the base, toDb skipped null values (propertyValue === undefined || propertyValue === null), so the item was written. Sibling effects come from the same per-property rules. A nested GSI/LSI key that is null is dropped (mapper.ts:141-143) and reads back as undefined rather than null. The index-key S/N/B check at mapper.ts:89-90 also runs on nested attributes, where DynamoDB applies no key constraint. No safeguard tells a nested call apart from a top-level one, because toDb has no depth or context flag.
Verification: On the base, toDb skipped every null property at every level, so the nested null key was dropped and the write went through. The diff narrows that check to if (propertyValue === undefined) (mapper.ts ~65), so the null now reaches toDbOne, whose new block (mapper.ts:137-140) throws for a key without checking for the top-level table item.
There was a problem hiding this comment.
it would mean such an item is a key but allows null (in typescript) - this would make zero sense. so it's an edge case where stuff would be used unsafe.
- map `null` to `{ NULL: true }` and `NULL` back to `null` on all levels
- ignore design type `Object` (emitted for `any`, interfaces and unions like `string | null`) and derive the type from the value instead
- `null` values of GSI/LSI key properties are omitted, index key values must be string number or binary
- throw for `null` values in SS/NS/BS sets
BREAKING CHANGE:
- `null` values are persisted as `NULL` instead of being omitted, `NULL` attributes are mapped to `null`
- `null` values of GSI/LSI key properties are no longer omitted.
Index keys can't be NULL in DynamoDB, model them as optional (`gsiKey?: string`) and use `undefined` (put) or `remove()` (update) instead
- properties with design type `Object` are no longer mapped with the object mapper
closes #427, closes #361
2681593 to
88a94a8
Compare
What to look out for:
|
closes #443,