cerialize icon indicating copy to clipboard operation
cerialize copied to clipboard

autoserializeAs not working anymore.

Open briosheje opened this issue 5 years ago • 5 comments

After updating to typescript 3.1.6 or anything higher, autoserializeAs is randomly not working.

Scenario:

image

Enumerated key in debugger (metadataArray): image

It sounds like some properties are not enumerated anymore, and it sounds like it happens almost randomly, in my current scenario.

Before updating, all the autoserialized properties were enumerated.

Is there any fix for this?

briosheje avatar Jun 24 '19 13:06 briosheje

Okay, I found out that the issue seems to be related to either compilation issues, either runtime / whatever reason. NO, I'm sorry, this doesn't seem to fix the issue.

Basically, at this line: https://github.com/weichx/cerialize/blob/f6f1166908f7dcb26389cff2c5b78e42d08735b4/src/serialize.ts#L313

It checks whether the provided element exists. If it doesn't, it **silently fails**, leaving no way to be aware that an issue occurred.

To solve the issue, I've currently replaced all the autoserialize's first parameter with an anonymous function returning the class type.

So, instead of

@autoserializeAs(HResAttivitaRisorse, 'proponenteRecord') public proponenteRecord: HResAttivitaRisorse;

I had to do:

@autoserializeAs(() => HResAttivitaRisorse, 'proponenteRecord') public proponenteRecord: HResAttivitaRisorse;

This seems to temporarely solve the issue.

EDIT: The issue is that there is a nested deserialized property that deserializes its parent class. Adding an extra autoserialize against these properties fixes the issue:

@autoserialize @autoserializeAs(HResAttivitaRisorse) public proponenteRecord: HResAttivitaRisorse;

There doens't seems to be any other reasonable way to do that, right now. Basically, the first autoserialize allows the metadata to be aware that the key exists. The autoserializeAs handles the rest of the things.

briosheje avatar Jun 24 '19 13:06 briosheje

To be honest I haven't worked on the web in a couple years now and am not at all following changes to Typescript. If the language changed in a way that broke the library I'd recommend someone A. submit a PR or b. lock down the typescript version (shitty, I know)

On Mon, Jun 24, 2019 at 3:52 PM Marco Iamonte [email protected] wrote:

Okay, I found out that the issue seems to be related to either compilation issues, either runtime / whatever reason.

Basically, at this line:

https://github.com/weichx/cerialize/blob/f6f1166908f7dcb26389cff2c5b78e42d08735b4/src/serialize.ts#L313

It checks whether the provided element exists. If it doesn't, it silently fails, leaving no way to be aware that an issue occurred.

To solve the issue, I've currently replaced all the autoserialize's first parameter with an anonymous function returning the class type.

So, instead of

@autoserializeAs(HResAttivitaRisorse, 'proponenteRecord') public proponenteRecord: HResAttivitaRisorse;

I had to do:

@autoserializeAs(() => HResAttivitaRisorse, 'proponenteRecord') public proponenteRecord: HResAttivitaRisorse;

This seems to temporarely solve the issue.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/weichx/cerialize/issues/96?email_source=notifications&email_token=AAKNPQMUVTA4H7XUBK5JCETP4DGSVA5CNFSM4H26TJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYM7YVY#issuecomment-505019479, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKNPQMKAL75B7A5SK3MLA3P4DGSVANCNFSM4H26TJNA .

weichx avatar Jun 24 '19 14:06 weichx

Your issue might also be a race condition issue where the type you used as an argument doesn't yet exist in the system by the time you are loading the class with the annotations on it, which would explain why passing a function works since the function gets resolved later in the bootstrap process

On Mon, Jun 24, 2019 at 4:39 PM Matt Weichselbaum < [email protected]> wrote:

To be honest I haven't worked on the web in a couple years now and am not at all following changes to Typescript. If the language changed in a way that broke the library I'd recommend someone A. submit a PR or b. lock down the typescript version (shitty, I know)

On Mon, Jun 24, 2019 at 3:52 PM Marco Iamonte [email protected] wrote:

Okay, I found out that the issue seems to be related to either compilation issues, either runtime / whatever reason.

Basically, at this line:

https://github.com/weichx/cerialize/blob/f6f1166908f7dcb26389cff2c5b78e42d08735b4/src/serialize.ts#L313

It checks whether the provided element exists. If it doesn't, it silently fails, leaving no way to be aware that an issue occurred.

To solve the issue, I've currently replaced all the autoserialize's first parameter with an anonymous function returning the class type.

So, instead of

@autoserializeAs(HResAttivitaRisorse, 'proponenteRecord') public proponenteRecord: HResAttivitaRisorse;

I had to do:

@autoserializeAs(() => HResAttivitaRisorse, 'proponenteRecord') public proponenteRecord: HResAttivitaRisorse;

This seems to temporarely solve the issue.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/weichx/cerialize/issues/96?email_source=notifications&email_token=AAKNPQMUVTA4H7XUBK5JCETP4DGSVA5CNFSM4H26TJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYM7YVY#issuecomment-505019479, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKNPQMKAL75B7A5SK3MLA3P4DGSVANCNFSM4H26TJNA .

weichx avatar Jun 24 '19 14:06 weichx

@weichx I'm not exactly sure that's a typescript issue, it sounds like it is, as you mentioned, an issue related to a race instead.

In a nutshell, adding an extra @autoserialize at least seems to be serializing the property which is currently mandatory in my case. Since I'm slighly more into the topic now, I will see whether I can submit a PR and eventually fix the issue myself, I'm really enjoying the library you've made so far and I'm pretty sure the issue can be easily fixed.

Thanks for the fast answer anyway, I'll see if I can fix the issue :)

briosheje avatar Jun 24 '19 14:06 briosheje

Glad you're enjoying it and thanks for taking a look at it!

On Mon, Jun 24, 2019 at 4:43 PM Marco Iamonte [email protected] wrote:

@weichx https://github.com/weichx I'm not exactly sure that's a typescript issue, it sounds like it is, as you mentioned, an issue related to a race instead.

In a nutshell, adding an extra @autoserialize at least seems to be serializing the property which is currently mandatory in my case. Since I'm slighly more into the topic now, I will see whether I can submit a PR and eventually fix the issue myself, I'm really enjoying the library you've made so far and I'm pretty sure the issue can be easily fixed.

Thanks for the fast answer anyway, I'll see if I can fix the issue :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/weichx/cerialize/issues/96?email_source=notifications&email_token=AAKNPQM44B3SAOQVQZYXOSTP4DMRZA5CNFSM4H26TJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYNFCNY#issuecomment-505041207, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKNPQPRHGF32KVQQWY7ZXLP4DMRZANCNFSM4H26TJNA .

weichx avatar Jun 24 '19 14:06 weichx