DynamoDBStateRepository: Backwards compatibility with versions < 4 #1120
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR ensures that Togglz 4 is able to work with DynamoDB tables which were previously managed by Togglz 3 or lower.
Problem
As part of Release 4.0.0-M3, this commit upgraded the DynamoDBStateRepository from AWS SDK version 1 to version 2. Before the upgrade, the toggle feature state was stored in DynamoDB using
com.amazonaws.services.dynamodbv2.document.Item.withJSON:As a result of using

withJSON, the DynamoDB client unpacked the JSON string and turned it into a map. Here is an example using Togglz 2.9.8:After the upgrade, the DynamoDBStateRepository instead stores the toggle feature state as a (JSON) string with
AttributeValue.builder().s(json).build():Here is an example of the same feature using Togglz 4.3.0:

The change breaks backwards compatibility. Togglz 4 cannot read features from a table which was managed by Togglz 3 or lower. The object mapper will throw a NullPointerException.
Proposed solution
The main goal of the proposed solution is to be compatible with Togglz 3 and lower and with existing releases of Togglz 4.
To achieve this, the read method
DynamoDBStateRepository.getFeatureStateis adapted to understand both representations: The string representation of version 4 and the map representations of versions 3 and lower.The write method
DynamoDBStateRepository.setFeatureStateremains unchanged and still stores feature state as a (JSON) string.As a result, a feature is automatically "migrated" from the old map representation to the new string representation when it is changed with
setFeatureState. Until all features have been changed at least one, a DynamoDB table can contain some features with the old representation and some features with the new representation.