Fix: EntityCreator::fill() triggers premature initialization of non-nullable wrapped properties#811
Open
patrikvalentaa wants to merge 1 commit intonextras:mainfrom
Open
Fix: EntityCreator::fill() triggers premature initialization of non-nullable wrapped properties#811patrikvalentaa wants to merge 1 commit intonextras:mainfrom
patrikvalentaa wants to merge 1 commit intonextras:mainfrom
Conversation
…operties in EntityCreator EntityCreator::fill() called $entity->getProperty($key) for every wrapped property to detect IRelationshipCollection. This triggered initProperty() with a null raw value before the user-supplied value from $params was applied, causing NullValueException for non-nullable properties using value wrappers (e.g. DateTimeWrapper on a non-nullable createdAt). Restrict the getProperty() call to collection relationships (ONE_HAS_MANY, MANY_HAS_MANY) — the only ones backed by IRelationshipCollection — using metadata instead of instantiating the wrapper. Non-collection wrapped properties are now initialized directly via setReadOnlyValue() with the supplied value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using
EntityCreator::create()(orfill()) to create a test entity, properties wrapped by non-relationship wrappers (e.g.DateTimeWrapper) get prematurely initialized withnull, which throwsNullValueExceptionfor non-nullable properties — even when a valid value is being passed in$params.Reproduction
Given an entity with a non-nullable
DateTimeImmutableproperty:Calling:
throws:
Root cause
In
EntityCreator::fill(), for every property with a wrapper,$entity->getProperty($key)is called to detect whether the wrapper is anIRelationshipCollection:getProperty()callsinitProperty(), which callssetRawValue(null)on the wrapper since no value has been set yet. For wrappers likeDateTimeWrapper,convertFromRawValue(null)throwsNullValueExceptionfor non-nullable properties — before the actual value from$paramsever reaches the entity.This affects all non-nullable properties using a value wrapper (date/time, custom value wrappers, etc.) when used with
EntityCreator.Fix
Only call
getProperty()when the property is a collection relationship (ONE_HAS_MANYorMANY_HAS_MANY) — those are the only relationships usingIRelationshipCollection. The relationship type is available on$property->relationship->typefrom metadata, so we don't need to instantiate the wrapper to know whether to callset()on it.For all other wrapped properties,
setReadOnlyValue($key, $value)is called directly with the user-supplied value, which goes through normal initialization with the correct value rather thannull.Notes
DateTimeWrapper,EnumWrapper, customImmutableValuePropertyWrappersubclasses), the value supplied in$paramsis now used to initialize the property directly, instead of being preceded by an abortednull-init.