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

Feature request: Allow deserialization of objects with read-only fields. #185

Open
ascheucher-shopify-partner opened this issue Jun 24, 2024 · 0 comments

Comments

@ascheucher-shopify-partner
Copy link

ascheucher-shopify-partner commented Jun 24, 2024

I am using 'ts-money' and it freezes the objects as last line of its constructor:

This is its simplified constructor:

    constructor(amount, currency) {
        this.amount = amount;
        this.currency = currency.code;
        Object.freeze(this);
    }

As there are no serializr decorations on the Money object, I created the following schema for it:

import { Currencies, Money, type Currency } from 'ts-money'

createModelSchema(
  Money,
  {
    amount: primitive(),
    currency: primitive()
  },
  (context: Context): Money => new Money(context.json.amount, context.json.currency)
);

But when I serialize, then I get the following error:

 FAIL  src/util/serializr.test.ts > get a first impression of serializr > test a map with Money objects
TypeError: Cannot assign to read only property 'amount' of object '[object Object]'
 ❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:215:51
 ❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/Context.ts:64:17
 ❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/utils/utils.ts:16:26
 ❯ onAfterDeserialize node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:201:9
 ❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:223:17
 ❯ Object.deserializer node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/types/primitive.ts:25:25
 ❯ deserializeProp node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:217:17
 ❯ callbackDeserialize node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:253:17
 ❯ onBeforeDeserialize node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:175:9
 ❯ _loop_2 node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:256:9

Would be great if serializr would support the creation of immutable objects.

Would also be great to state the fact, it doesn't support immutable objects in the document. Have wasted several hours now.

Edit: I just have realized, I can use a before deserialization handler to prevent the error to happen and still get the full object tree. Maybe this request ist obsolete now, but I hae the feeling it's a bit hacky to misuse the handler in this case.

Maybe it would be enough to check after the call of factory method, whether the returned object is frozen or not.
If not continue as normal, if frozen, just call it a day.

Thanks for considering.

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

No branches or pull requests

1 participant