Skip to content

Use $rawModelDataInput as construct function parameter name #55

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

Conversation

mikewolfxyou
Copy link

@mikewolfxyou mikewolfxyou commented Feb 15, 2022

Use $rawModelDataInput as construct function parameter name in Modelphptpl

  1. The reason:
    Because when Symfony deserializer the serialized object will not find the parameter value in the saved data and use the default parameter value(empty array).
    Then the model construct function will get the error: Typed property must not be accessed before initialization since PHP 7.4, The error description and explanation: stackoverflow

  2. The Symfony serializer:
    see: https://github.com/symfony/symfony/blob/44db49fb8f6d70c4341120a3f8f1b1a1a0dfa014/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php#L380
    and
    https://github.com/symfony/symfony/blob/44db49fb8f6d70c4341120a3f8f1b1a1a0dfa014/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php#L355

  3. How to reproduce the error:

    • Use Symfony Messenger Transport Serialier as a symfony messenger transport serializer
    • After the message has been saved for async handling, when the message handler tries to deserialize the saved message, the error will be raised.

@wol-soft
Copy link
Owner

Hi,

thanks for the report. I'd more likely rename the object property to $modelData to match the current function signature. Would you mind to change the MR? Then I'll merge it.

Cheers

@mikewolfxyou
Copy link
Author

Hi,

thanks for the report. I'd more likely rename the object property to $modelData to match the current function signature. Would you mind to change the MR? Then I'll merge it.

Cheers

yes, I will do it, happy to help :)

@mikewolfxyou mikewolfxyou force-pushed the Use-raw-data-input-as-construct-parameter-name branch from 4392d6f to 37924b4 Compare February 22, 2022 14:29
…phptpl

Because when Symfony deserializer the object will not find the parameter value in the saved data and use the default parameter value(empty array).

Then the model construct function will get error: `Typed property must not be accessed before initialization` since PHP 7.4

see: https://github.com/symfony/symfony/blob/44db49fb8f6d70c4341120a3f8f1b1a1a0dfa014/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php#L380

and

https://github.com/symfony/symfony/blob/44db49fb8f6d70c4341120a3f8f1b1a1a0dfa014/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php#L355
@mikewolfxyou mikewolfxyou force-pushed the Use-raw-data-input-as-construct-parameter-name branch from 37924b4 to 1472e51 Compare February 22, 2022 14:36
@wol-soft
Copy link
Owner

Whoops, there were quite a few more places the variable name is used to access the values I didn't take into account.

I've patched all occurrences in #56 and will merge it and tag a new version.

Thanks again for the detailled report and the help!

@wol-soft wol-soft closed this in #56 Feb 22, 2022
wol-soft added a commit that referenced this pull request Feb 22, 2022
Change all occurrences of `rawModelDataInput` to `modelData` (compare #55)
@wol-soft
Copy link
Owner

@mikewolfxyou can you check if the current master works in the scenario? I've noticed the symfony serializer uses some kind of name normalizer.

The reason for my question: the property is now named $_modelData while the constructor parameter is still named $modelData. The underscore is currently required as the generated models use the underscore to determine between object properties belonging to the represented schema and internal properties.

@mikewolfxyou
Copy link
Author

@mikewolfxyou can you check if the current master works in the scenario? I've noticed the symfony serializer uses some kind of name normalizer.

The reason for my question: the property is now named $_modelData while the constructor parameter is still named $modelData. The underscore is currently required as the generated models use the underscore to determine between object properties belonging to the represented schema and internal properties.

I tested yesterday, with the branch I created: just rename the local variable from _rawModelDataInput to modelData
then

  1. if use setSerialization(true) when generate model, in the serialized JSON, there is NOT rawModelDataInput but modelData for the raw model data, then when deserializ it, work fine ✅
  2. If NOT use setSerialization(true) when generate model, then in the serialized JSON, there is STILL rawModelDataInput for the raw model data. I did some debug, guess maybe that because the all model implement the JSONModelInterface which has a getRawModelDataInput function, when Symfony do the name normalizer work, then the thing happened. 🤞🏼

@wol-soft
Copy link
Owner

After digging deeper into the symfony serializer I've decided to take your first approach to rename the constructor parameter to rawModelDataInput. Everything else seems to cause more harm than it solves due to the name normalizing based on the getter-functions.

Sorry for the back and forth while finding a solution for the issue 😄

I still wasn't able to reproduce the "typed property access" error inside the model. The generated models don't use typed properties independent of the used PHP version (at least currently. I'm planning to use different templates depending on the PHP version to be able to use more modern features inside the generated models).

I've tagged version 0.22.0, which includes the renaming.

@mikewolfxyou
Copy link
Author

After digging deeper into the symfony serializer I've decided to take your first approach to rename the constructor parameter to rawModelDataInput. Everything else seems to cause more harm than it solves due to the name normalizing based on the getter-functions.

Sorry for the back and forth while finding a solution for the issue 😄

I still wasn't able to reproduce the "typed property access" error inside the model. The generated models don't use typed properties independent of the used PHP version (at least currently. I'm planning to use different templates depending on the PHP version to be able to use more modern features inside the generated models).

I've tagged version 0.22.0, which includes the renaming.

Thank you for taking care of it :)

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

Successfully merging this pull request may close these issues.

2 participants