Skip to content
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

Custom prototype objects cannot be stored #476

Closed
Karasuni opened this issue Jan 31, 2018 · 4 comments
Closed

Custom prototype objects cannot be stored #476

Karasuni opened this issue Jan 31, 2018 · 4 comments

Comments

@Karasuni
Copy link

AngularFireStoreCollection.add() has a hard validation for the type object. I notice that the validator does not accept any objects with a 'custom' prototype:

/**
* Returns true iff it's a non-null object without a custom prototype
* (i.e. excludes Array, Date, etc.).
*/
export function isPlainObject(input: AnyJs) {
return (
typeof input === 'object' &&
input !== null &&
Object.getPrototypeOf(input) === Object.prototype
);
}

My question is what the reasoning is behind this hard check against the Object prototype?

When you're using custom objects as we do in Typescript the action fails even though the type is defined as add(data: T): Promise<DocumentReference>; - a type definition that implies any Object can be used.

Related to #311.

@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.

@google-oss-bot
Copy link
Contributor

Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information.

@wilhuff
Copy link
Contributor

wilhuff commented Mar 6, 2018

Our reasoning here essentially in two parts.

We can't read data back into custom objects and since we can't reconstitute your object at read time we don't want to half-implement object mapping.

The second part is that even if we did support custom objects for the obvious cases, inevitably people will be dissatisfied by what we've done. For example:

  • Should we serialize all properties of the immediate object or traverse the prototype chain?
  • Should we provide some mechanism for customizing the names properties?
  • Should we serialize properties from Object.defineProperty?
  • Should we support classes that implement toJSON? Should date-like strings that come out of that be dates or strings? Should try to parse them to find out?

I realize you "just" want to store custom objects but really this opens a kind of Pandora's box that creates just as many problems as it solves.

@samtstern
Copy link
Contributor

Thanks @wilhuff for the answer. Closing this issue as we consider this decision final.

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

No branches or pull requests

4 participants