Skip to content

#443 support null values - #444

Open
mumenthalers wants to merge 1 commit into
nextfrom
#443-support-null-values
Open

mumenthalers wants to merge 1 commit into
nextfrom
#443-support-null-values

Conversation

@mumenthalers

Copy link
Copy Markdown

closes #443,

@claude claude Bot 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.

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.

@mumenthalers
mumenthalers changed the base branch from master to next November 26, 2026 15:11
@coveralls

coveralls commented Nov 26, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 95.537% (+0.04%) from 95.498% — #443-support-null-values into next

@mumenthalers

Copy link
Copy Markdown
Author

@claude review

@claude claude Bot 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.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/mapper/mapper.ts Resolved
Comment on lines +51 to +73
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Comment thread src/mapper/mapper.ts Outdated
Comment on lines +137 to +144
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
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Comment thread src/mapper/mapper.ts Resolved
- 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
@mumenthalers
mumenthalers force-pushed the #443-support-null-values branch from 2681593 to 88a94a8 Compare November 30, 2026 15:29
@mumenthalers

mumenthalers commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

What to look out for:

  • x = null on model properties is now stored as NULL. Use undefined if the attribute should be left out.
  • set(null) in updates writes NULL. Use remove() to delete the attribute.
  • attributeNotExists() is false for a NULL attribute, and attributeExists() is true.
  • === undefined, 'prop' in obj, hasOwnProperty behave differently, because a loaded NULL is now null instead of missing.
  • { ...defaults, ...loaded } overwrites defaults with null.
  • toEqual in tests fails when null is expected to be missing.
  • Old items (attribute missing) and new items (NULL) live side by side, so reads and filters must handle both.
  • Custom mappers are no longer called for null/NULL.
  • @Property() on any, interfaces or X | null without a mapper is now mapped by its runtime value, not the object mapper.
  • null on GSI/LSI keys throws on put, and set(null) gets rejected by DynamoDB. Type them as optional (key?: string).
  • null inside sets (SS/NS/BS) throws.

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.

Support null as attribute

3 participants