Skip to content

Commit 26d973d

Browse files
authored
[Tables] Fix issue handling empty nextRowKey (Azure#20916)
* Fix issue handling undefined nextRowKey * Address PR comments * Add changelog * remove export * Address comments
1 parent dccf667 commit 26d973d

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

sdk/tables/data-tables/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Bugs Fixed
1010

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

1314
### Other Changes

sdk/tables/data-tables/src/utils/continuationToken.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33

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

6-
export interface ContinuationToken {
6+
interface ContinuationToken {
77
nextPartitionKey: string;
8-
nextRowKey: string;
8+
nextRowKey?: string;
99
}
1010

1111
/**
1212
* Encodes the nextPartitionKey and nextRowKey into a single continuation token
1313
*/
1414
export function encodeContinuationToken(
15-
nextPartitionKey: string = "",
16-
nextRowKey: string = ""
15+
nextPartitionKey?: string,
16+
nextRowKey?: string
1717
): string | undefined {
18-
if (!nextPartitionKey && !nextRowKey) {
18+
if (!nextPartitionKey) {
1919
return undefined;
2020
}
2121

22-
const continuationToken = JSON.stringify({
22+
const continuationToken = {
2323
nextPartitionKey,
24-
nextRowKey,
25-
});
24+
// Only add nextRowKey if the value is not null, undefined or empty string.
25+
...(nextRowKey && { nextRowKey }),
26+
};
2627

27-
return base64Encode(continuationToken);
28+
return base64Encode(JSON.stringify(continuationToken));
2829
}
2930

3031
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import {
5+
decodeContinuationToken,
6+
encodeContinuationToken,
7+
} from "../../src/utils/continuationToken";
8+
9+
import { assert } from "chai";
10+
11+
describe("continuation token utils", () => {
12+
it("should encode nextPartitionKey and nextRowKey", () => {
13+
const encoded = encodeContinuationToken("foo", "bar");
14+
assert.equal(encoded, "eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIiwibmV4dFJvd0tleSI6ImJhciJ9");
15+
});
16+
17+
it("should not encode nextRowKey if it is empty string", () => {
18+
const encoded = encodeContinuationToken("foo", "");
19+
assert.deepEqual(decodeContinuationToken(encoded!), { nextPartitionKey: "foo" });
20+
});
21+
22+
it("should encode nextPartitionKey and undefined nextRowKey", () => {
23+
const encoded = encodeContinuationToken("foo");
24+
assert.equal(encoded, "eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIn0=");
25+
});
26+
27+
it("should return undefined if nextPartitionKey and nextRowKey are empty", () => {
28+
const encoded = encodeContinuationToken();
29+
assert.equal(encoded, undefined);
30+
});
31+
32+
it("should decode nextPartitionKey and nextRowKey", () => {
33+
const decoded = decodeContinuationToken(
34+
"eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIiwibmV4dFJvd0tleSI6ImJhciJ9"
35+
);
36+
assert.deepEqual(decoded, { nextPartitionKey: "foo", nextRowKey: "bar" });
37+
});
38+
39+
it("should decode nextPartitionKey and undefined nextRowKey", () => {
40+
const decoded = decodeContinuationToken("eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIn0=");
41+
assert.deepEqual(decoded, { nextPartitionKey: "foo" } as any);
42+
});
43+
});

0 commit comments

Comments
 (0)