Skip to content

Convert Date to ISO string in functions #4887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-snails-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/functions': patch
---

Fix functions to convert Date objects to an ISO string instead of an empty object.
24 changes: 20 additions & 4 deletions packages-exp/functions-exp/src/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ describe('Serializer', () => {
expect(decode('hello')).to.equal('hello');
});

it('encodes date to ISO string', () => {
expect(encode(new Date(1620666095891))).to.equal(
'2021-05-10T17:01:35.891Z'
);
});

it('decodes date string without modifying it', () => {
expect(decode('2021-05-10T17:01:35.891Z')).to.equal(
'2021-05-10T17:01:35.891Z'
);
});

// TODO(klimt): Make this test more interesting once we have a complex type
// that can be created in JavaScript.
it('encodes array', () => {
Expand Down Expand Up @@ -111,12 +123,14 @@ describe('Serializer', () => {
encode({
foo: 1,
bar: 'hello',
baz: [1, 2, 3]
baz: [1, 2, 3],
date: new Date(1620666095891)
})
).to.deep.equal({
foo: 1,
bar: 'hello',
baz: [1, 2, 3]
baz: [1, 2, 3],
date: '2021-05-10T17:01:35.891Z'
});
});

Expand All @@ -132,12 +146,14 @@ describe('Serializer', () => {
value: '1099511627776',
'@type': 'type.googleapis.com/google.protobuf.Int64Value'
}
]
],
date: '2021-05-10T17:01:35.891Z'
})
).to.deep.equal({
foo: 1,
bar: 'hello',
baz: [1, 2, 1099511627776]
baz: [1, 2, 1099511627776],
date: '2021-05-10T17:01:35.891Z'
});
});

Expand Down
3 changes: 3 additions & 0 deletions packages-exp/functions-exp/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export function encode(data: unknown): unknown {
if (Object.prototype.toString.call(data) === '[object String]') {
return data;
}
if (data instanceof Date) {
return data.toISOString();
}
if (Array.isArray(data)) {
return data.map(x => encode(x));
}
Expand Down
3 changes: 3 additions & 0 deletions packages/functions/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export class Serializer {
if (Object.prototype.toString.call(data) === '[object String]') {
return data;
}
if (data instanceof Date) {
return data.toISOString();
}
if (Array.isArray(data)) {
return data.map(x => this.encode(x));
}
Expand Down
24 changes: 20 additions & 4 deletions packages/functions/test/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ describe('Serializer', () => {
expect(serializer.decode('hello')).to.equal('hello');
});

it('encodes date to ISO string', () => {
expect(serializer.encode(new Date(1620666095891))).to.equal(
'2021-05-10T17:01:35.891Z'
);
});

it('decodes date string without modifying it', () => {
expect(serializer.decode('2021-05-10T17:01:35.891Z')).to.equal(
'2021-05-10T17:01:35.891Z'
);
});

// TODO(klimt): Make this test more interesting once we have a complex type
// that can be created in JavaScript.
it('encodes array', () => {
Expand Down Expand Up @@ -117,12 +129,14 @@ describe('Serializer', () => {
serializer.encode({
foo: 1,
bar: 'hello',
baz: [1, 2, 3]
baz: [1, 2, 3],
date: new Date(1620666095891)
})
).to.deep.equal({
foo: 1,
bar: 'hello',
baz: [1, 2, 3]
baz: [1, 2, 3],
date: '2021-05-10T17:01:35.891Z'
});
});

Expand All @@ -138,12 +152,14 @@ describe('Serializer', () => {
value: '1099511627776',
'@type': 'type.googleapis.com/google.protobuf.Int64Value'
}
]
],
date: '2021-05-10T17:01:35.891Z'
})
).to.deep.equal({
foo: 1,
bar: 'hello',
baz: [1, 2, 1099511627776]
baz: [1, 2, 1099511627776],
date: '2021-05-10T17:01:35.891Z'
});
});

Expand Down