Skip to content

VersionedRecordExtension does not support 0 as a starting value #3894

Open
@akiesler

Description

@akiesler

Describe the bug

The VersionedRecordExtension says it will increment a version and verify that the version in DynamoDB is equal to the previous value when updating. However, the documentation is unclear that the starting value must be null and cannot be 0 which would be a reasonable starting version. Using a value of 0 results in the conditional check failing when attempting to put the item.

Expected Behavior

I would expect the extension to treat 0 the same as null acting as a clear starting point for the versioning. This would result in an update expression where the version attribute is checked not to exist before updating.

Current Behavior

The DynamoDB client throws a ConditionalUpdateFailedException because the the version field does not exist in the record.

Reproduction Steps

A simple test case modified from the SDK:

   @Test
    public void beforeWrite_initialVersionDueToExplicitZero_expressionAndTransformedItemIsCorrect() {
        FakeItem fakeItem = createUniqueFakeItem();

        Map<String, AttributeValue> inputMap =
            new HashMap<>(FakeItem.getTableSchema().itemToMap(fakeItem, true));
        inputMap.put("version", AttributeValue.builder().n("0").build());

        Map<String, AttributeValue> fakeItemWithInitialVersion =
            new HashMap<>(FakeItem.getTableSchema().itemToMap(fakeItem, true));
        fakeItemWithInitialVersion.put("version", AttributeValue.builder().n("1").build());

        WriteModification result =
            versionedRecordExtension.beforeWrite(DefaultDynamoDbExtensionContext
                                                     .builder()
                                                     .items(inputMap)
                                                     .tableMetadata(FakeItem.getTableMetadata())
                                                     .operationContext(PRIMARY_CONTEXT).build());

        assertThat(result.transformedItem(), is(fakeItemWithInitialVersion));
        assertThat(result.additionalConditionalExpression(),
                   is(Expression.builder()
                                .expression("attribute_not_exists(#AMZN_MAPPED_version)")
                                .expressionNames(singletonMap("#AMZN_MAPPED_version", "version"))
                                .build()));
    }

Possible Solution

Update the check in beforeWrite for the VersionedRecordExtension to include a check for 0.
If there is concern that this may break backwards compatibility it could be added behind a feature flag in the extension itself.

Additional Information/Context

No response

AWS Java SDK version used

2.20.43

JDK version used

openjdk version "1.8.0_362"

Operating System and version

macOS 12.6.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationThis is a problem with documentation.dynamodb-enhancedp2This is a standard priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions