-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Typed property X must not be accessed before initialization #1367
Comments
I agree that unitialized properties should not be serialized by default. |
Good point on that nulls and unitialized properties are different things. But i think a configuration option is still handy; maybe it would be |
Sure but it's not a serializers job to do that validation. So I don't think such option is really necessary. |
I think adding this Option would add Value to this Serializer. Since this Serializer is low level Library and could be used for internal purposes, not fully initialised Objects are possible. |
I personally think that both {
"pre74property": null,
"initialized74property": "present",
"uninitialized74property": null
} and {
"initialized74property": "present"
} are wrong. if |
The only "acceptable" thing that the serializer could do here is to skip always the serialization of uninitialized props (as suggested by @Tobion), but that would force us to use always the reflection accessor with obvious performance penalty compared to the closure accessor. Another option is to have a global config parameter that skips those props, in this way the user explicitly opts-in for the feature (and the related performance penalty) |
This error also affects constructor-promoted attributes, even though it has a default initial value when nullable it will crash. I see the performance penalty, but some users do like the benefit of writing less code, and this possibility to enable it would be nice. https://wiki.php.net/rfc/constructor_promotion
|
The problem @douglasjam described is different from whether uninitialized properties should be serialized. |
The problem @douglasjam described is different from whether uninitialized properties should be serialized. |
May be that the solution implemented in Sf PropertyAccessor could be an interesting option without performance issue. |
This one here would cover the constructor promotion when there's a default value |
Don't know how to solve this bug with nested collections. Constructor is not invoked and property isn't promoted. Default value not assignable (I can't do #[ORM\OneToMany(mappedBy: 'cart', targetEntity: CartItem::class)]
#[Serializer\Accessor(setter: 'setItems')]
private Collection $items;
public function __construct()
{
$this->items = new ArrayCollection();
} |
I'm facing the same issue :/ |
Just for the record, I know I'm late to the party, but we solved this issue with the PreSerialization attribute, and initialize the properties there. (@gremo ) |
This topic has come up in #1336 before, but in the context of "this is an issue with 3.14.0-rc1", not "please make this work".
So here goes: please make this work. I would really like to have the strong safety of typed properties.
Steps required to reproduce the problem
Afaict, this affects all JMS versions in PHP 7.4+.
Expected Result
or, with
setSerializeNull(false)
:Actual Result
There is
ReflectionProperty#isInitialized
, which does exactly whats needed here:Also, there is the workaround of giving them a default value of
null
. But i'd prefer not to do this.The text was updated successfully, but these errors were encountered: