aws / aws-sdk-java-v2

The official AWS SDK for Java - Version 2
Apache License 2.0
2.2k stars 853 forks source link

lastEvaluatedKey in QueryResponse returns null instead of empty (Dynamo) #360

Closed Saheb closed 5 years ago

Saheb commented 6 years ago

QueryResponse in package software.amazon.awssdk.services.dynamodb.model. A method in that class lastEvaluatedKey() returns null when it is the last page and no further data can be retrieved. The return type is Map<String, AttributeValue>; the documentation says it should be empty and doesn't mention it would be null.

    /**
     * <p>
     * The primary key of the item where the operation stopped, inclusive of the previous result set. Use this value to
     * start a new operation, excluding this value in the new request.
     * </p>
     * <p>
     * If <code>LastEvaluatedKey</code> is empty, then the "last page" of results has been processed and there is no
     * more data to be retrieved.
     * </p>
     * <p>
     * If <code>LastEvaluatedKey</code> is not empty, it does not necessarily mean that there is more data in the result
     * set. The only way to know when you have reached the end of the result set is when <code>LastEvaluatedKey</code>
     * is empty.
     * </p>
     * <p>
     * Attempts to modify the collection returned by this method will result in an UnsupportedOperationException.
     * </p>
     * 
     * @return The primary key of the item where the operation stopped, inclusive of the previous result set. Use this
     *         value to start a new operation, excluding this value in the new request.</p>
     *         <p>
     *         If <code>LastEvaluatedKey</code> is empty, then the "last page" of results has been processed and there
     *         is no more data to be retrieved.
     *         </p>
     *         <p>
     *         If <code>LastEvaluatedKey</code> is not empty, it does not necessarily mean that there is more data in
     *         the result set. The only way to know when you have reached the end of the result set is when
     *         <code>LastEvaluatedKey</code> is empty.
     */
    public Map<String, AttributeValue> lastEvaluatedKey() {
        return lastEvaluatedKey;
    }

Expected Behavior

As a caller of that method, I should be able to call method isEmpty on the returned value to check the existence of the lastKey, but that'd result in NPE which should never happen.

Current Behavior

NullPointerException

Possible Solution

Either update the documentation to say it'd return null in case the lastKey doesn't exist or initialize the lastEvaluatedKey with empty Map and ensure it is never null

Steps to Reproduce (for bugs)

Context

Your Environment

millems commented 5 years ago

Tracking in #867