-
Notifications
You must be signed in to change notification settings - Fork 822
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
FIX Respect explicit casting before casting arrays #11271
Merged
emteknetnz
merged 1 commit into
silverstripe:6
from
creative-commoners:pulls/6/fix-casting-bug
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,10 +385,11 @@ public function setCustomisedObj(ViewableData $object) | |
* for a field on this object. This helper will be a subclass of DBField. | ||
* | ||
* @param string $field | ||
* @return string Casting helper As a constructor pattern, and may include arguments. | ||
* @param bool $useFallback If true, fall back on the default casting helper if there isn't an explicit one. | ||
* @return string|null Casting helper As a constructor pattern, and may include arguments. | ||
* @throws Exception | ||
*/ | ||
public function castingHelper($field) | ||
public function castingHelper($field, bool $useFallback = true) | ||
{ | ||
// Get casting if it has been configured. | ||
// DB fields and PHP methods are all case insensitive so we normalise casing before checking. | ||
|
@@ -399,20 +400,41 @@ public function castingHelper($field) | |
} | ||
|
||
// If no specific cast is declared, fall back to failover. | ||
// Note that if there is a failover, the default_cast will always | ||
$failover = $this->getFailover(); | ||
if ($failover) { | ||
$cast = $failover->castingHelper($field, $useFallback); | ||
if ($cast) { | ||
return $cast; | ||
} | ||
} | ||
|
||
if ($useFallback) { | ||
return $this->defaultCastingHelper($field); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Return the default "casting helper" for use when no explicit casting helper is defined. | ||
* This helper will be a subclass of DBField. See castingHelper() | ||
*/ | ||
protected function defaultCastingHelper(string $field): string | ||
{ | ||
// If there is a failover, the default_cast will always | ||
// be drawn from this object instead of the top level object. | ||
$failover = $this->getFailover(); | ||
if ($failover) { | ||
$cast = $failover->castingHelper($field); | ||
$cast = $failover->defaultCastingHelper($field); | ||
if ($cast) { | ||
return $cast; | ||
} | ||
} | ||
|
||
// Fall back to default_cast | ||
// Fall back to raw default_cast | ||
$default = $this->config()->get('default_cast'); | ||
if (empty($default)) { | ||
throw new Exception("No default_cast"); | ||
throw new Exception('No default_cast'); | ||
} | ||
return $default; | ||
} | ||
|
@@ -559,15 +581,25 @@ public function obj($fieldName, $arguments = [], $cache = false, $cacheName = nu | |
$value = $this->$fieldName; | ||
} | ||
|
||
// Try to cast object if we have an explicit cast set | ||
if (!is_object($value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping this condition here (mirroring the below) because if a method returns an explicit object (usually We could choose to change that, but if so it should be handled in a separate card with an issue for that purpose. |
||
$castingHelper = $this->castingHelper($fieldName, false); | ||
if ($castingHelper !== null) { | ||
$valueObject = Injector::inst()->create($castingHelper, $fieldName); | ||
$valueObject->setValue($value, $this); | ||
$value = $valueObject; | ||
} | ||
} | ||
|
||
// Wrap list arrays in ViewableData so templates can handle them | ||
if (is_array($value) && array_is_list($value)) { | ||
$value = ArrayList::create($value); | ||
} | ||
|
||
// Cast object | ||
// Fallback on default casting | ||
if (!is_object($value)) { | ||
// Force cast | ||
$castingHelper = $this->castingHelper($fieldName); | ||
$castingHelper = $this->defaultCastingHelper($fieldName); | ||
$valueObject = Injector::inst()->create($castingHelper, $fieldName); | ||
$valueObject->setValue($value, $this); | ||
$value = $valueObject; | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opted to add an extra param here instead of just never including defaults from this method because this reduces upgrade pains - any usage of this method outside of the
obj()
method should always fall back on the default casting.