Skip to content

Commit 634f2ec

Browse files
authored
fix: Use for … of loop instead of for … in (#1209)
1 parent 8112c9f commit 634f2ec

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/common/src/converter/payload-converter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ export class SearchAttributePayloadConverter implements PayloadConverter {
268268
const firstValue = values[0];
269269
const firstType = typeof firstValue;
270270
if (firstType === 'object') {
271-
for (const idx in values) {
272-
const value = values[idx];
271+
for (const [idx, value] of values.entries()) {
273272
if (!(value instanceof Date)) {
274273
throw new ValueError(
275274
`SearchAttribute values must arrays of strings, numbers, booleans, or Dates. The value ${value} at index ${idx} is of type ${typeof value}`
@@ -281,8 +280,7 @@ export class SearchAttributePayloadConverter implements PayloadConverter {
281280
throw new ValueError(`SearchAttribute array values must be: string | number | boolean | Date`);
282281
}
283282

284-
for (const idx in values) {
285-
const value = values[idx];
283+
for (const [idx, value] of values.entries()) {
286284
if (typeof value !== firstType) {
287285
throw new ValueError(
288286
`All SearchAttribute array values must be of the same type. The first value ${firstValue} of type ${firstType} doesn't match value ${value} of type ${typeof value} at index ${idx}`

packages/test/src/test-payload-converter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
METADATA_ENCODING_KEY,
1111
METADATA_MESSAGE_TYPE_KEY,
1212
PayloadConverterError,
13+
SearchAttributePayloadConverter,
1314
UndefinedPayloadConverter,
1415
ValueError,
1516
} from '@temporalio/common';
@@ -171,6 +172,21 @@ test('ProtobufJSONPayloadConverter converts binary', (t) => {
171172
t.deepEqual(testInstance.data, instance.data);
172173
});
173174

175+
test(`SearchAttributePayloadConverter doesn't fail if Array.prototype contains enumerable properties`, (t) => {
176+
try {
177+
const converter = new SearchAttributePayloadConverter();
178+
Object.defineProperty(Array.prototype, 'foo', {
179+
configurable: true,
180+
value: 123,
181+
enumerable: true,
182+
});
183+
converter.toPayload(['test']);
184+
t.pass();
185+
} finally {
186+
delete (Array.prototype as any).foo;
187+
}
188+
});
189+
174190
if (RUN_INTEGRATION_TESTS) {
175191
test('Worker throws decoding proto JSON without WorkerOptions.dataConverter', async (t) => {
176192
let markErrorThrown: () => void;

0 commit comments

Comments
 (0)