Skip to content

Commit

Permalink
[Tables] Fix issue handling empty nextRowKey (Azure#20916)
Browse files Browse the repository at this point in the history
* Fix issue handling undefined nextRowKey

* Address PR comments

* Add changelog

* remove export

* Address comments
  • Loading branch information
joheredi authored Mar 18, 2022
1 parent dccf667 commit 26d973d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions sdk/tables/data-tables/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bugs Fixed

- Fix issue when the Service returns an empty nextRowKey. [#20916](https://github.com/Azure/azure-sdk-for-js/pull/20916).
- Fix issue with `getStatistics()` operation consistently failing and added test. [#20398](https://github.com/Azure/azure-sdk-for-js/pull/20398)

### Other Changes
Expand Down
19 changes: 10 additions & 9 deletions sdk/tables/data-tables/src/utils/continuationToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@

import { base64Decode, base64Encode } from "./bufferSerializer";

export interface ContinuationToken {
interface ContinuationToken {
nextPartitionKey: string;
nextRowKey: string;
nextRowKey?: string;
}

/**
* Encodes the nextPartitionKey and nextRowKey into a single continuation token
*/
export function encodeContinuationToken(
nextPartitionKey: string = "",
nextRowKey: string = ""
nextPartitionKey?: string,
nextRowKey?: string
): string | undefined {
if (!nextPartitionKey && !nextRowKey) {
if (!nextPartitionKey) {
return undefined;
}

const continuationToken = JSON.stringify({
const continuationToken = {
nextPartitionKey,
nextRowKey,
});
// Only add nextRowKey if the value is not null, undefined or empty string.
...(nextRowKey && { nextRowKey }),
};

return base64Encode(continuationToken);
return base64Encode(JSON.stringify(continuationToken));
}

/**
Expand Down
43 changes: 43 additions & 0 deletions sdk/tables/data-tables/test/internal/continuationToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
decodeContinuationToken,
encodeContinuationToken,
} from "../../src/utils/continuationToken";

import { assert } from "chai";

describe("continuation token utils", () => {
it("should encode nextPartitionKey and nextRowKey", () => {
const encoded = encodeContinuationToken("foo", "bar");
assert.equal(encoded, "eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIiwibmV4dFJvd0tleSI6ImJhciJ9");
});

it("should not encode nextRowKey if it is empty string", () => {
const encoded = encodeContinuationToken("foo", "");
assert.deepEqual(decodeContinuationToken(encoded!), { nextPartitionKey: "foo" });
});

it("should encode nextPartitionKey and undefined nextRowKey", () => {
const encoded = encodeContinuationToken("foo");
assert.equal(encoded, "eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIn0=");
});

it("should return undefined if nextPartitionKey and nextRowKey are empty", () => {
const encoded = encodeContinuationToken();
assert.equal(encoded, undefined);
});

it("should decode nextPartitionKey and nextRowKey", () => {
const decoded = decodeContinuationToken(
"eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIiwibmV4dFJvd0tleSI6ImJhciJ9"
);
assert.deepEqual(decoded, { nextPartitionKey: "foo", nextRowKey: "bar" });
});

it("should decode nextPartitionKey and undefined nextRowKey", () => {
const decoded = decodeContinuationToken("eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIn0=");
assert.deepEqual(decoded, { nextPartitionKey: "foo" } as any);
});
});

0 comments on commit 26d973d

Please sign in to comment.