Skip to content

Date handling in functions serializer #683

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

Closed
abrgr opened this issue Apr 13, 2018 · 6 comments · Fixed by #4887
Closed

Date handling in functions serializer #683

abrgr opened this issue Apr 13, 2018 · 6 comments · Fixed by #4887
Assignees

Comments

@abrgr
Copy link

abrgr commented Apr 13, 2018

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Firebase SDK version: 4.12.1
  • Firebase Product: functions

[REQUIRED] Describe the problem

Javascript dates passed as arguments to firebase functions are converted to empty objects. The code in https://github.com/firebase/firebase-js-sdk/blob/master/packages/functions/src/serializer.ts does not account for dates as distinct from objects. I believe the more idiomatic method would either be to serialize dates as iso8601 and expect that functions convert to a javascript date themselves or to make use of the '@type' properties used in decode (though, obviously, this would require some backend changes). Also, despite the comment to the contrary, the decode method does not appear to handle dates.

Steps to reproduce:

Invoke a function via the callable interface.

const func = functions.httpsCallable('someFunc');
func({
  date: new Date() // this will appear as { date: {} } in someFunc
}).then(({ returnedDate }) => {
  // notice that returnedDate is a string
});
@google-oss-bot
Copy link
Contributor

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

@milesingrams
Copy link

Dealing with the same issue. Ive had to implement my own serialize/deserialize shim for dates for all my cloud functions. Interesting that the serializer.ts file specifically says its built to handle dates but has no functionality for dates at all. Either the documentation should change to reflect the current state or the function should include date handling. Furthermore the cloud functions should also handle serializing and deserializing firebase classes such as Timestamps and GeoPoints as it probably is not uncommon to be sending those documents in and out of cloud functions.

@mohshraim
Copy link

any chance for this??

@bklimt
Copy link
Contributor

bklimt commented Mar 11, 2019

Hi. We hear you. We are still looking into the best way to support this. Thank you for your feedback and your patience.

@milesingrams
Copy link

Glad you’re looking into it! I know serialization/deserialization comes with a lot of caveats.
Perhaps you can use a reserved property that specifies the instance type of a complex object. For example:
{
_instanceType: “Date”,
milliseconds: 1234567890
}
{
_instanceType: “firebase.firestore.Timestamp”,
seconds: 1234567890,
nanoseconds: 1234567890
}
Of course this would break if someone already has a property with the key _instanceType. Another option is to send a separate object alongside the data object that has a map of paths with the types to deserialize into. The benefit being that there are no reserved properties.

And finally you can just leave it up to the user and provide a way to extend the serialize/deserialize functions arbitrarily which would allow the user to easily use any class type they please.

I’m sure you’ve already dug into these options but thought I might as well write em out.

Thanks!

@mmtftr
Copy link

mmtftr commented Feb 4, 2021

This is still an issue, and I've just experienced it. This should be a pretty standard thing to support, what is its progress?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants