-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix incorrect behaviour when comparing castable attributes in originalIsEquivalent
method
#3439
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
base: 5.x
Are you sure you want to change the base?
Changes from 4 commits
c6147c9
74d2cb5
f027089
40ecfd0
8e808a3
13d48d4
7358dcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
use function is_string; | ||
use function ltrim; | ||
use function method_exists; | ||
use function serialize; | ||
use function sprintf; | ||
use function str_contains; | ||
use function str_starts_with; | ||
|
@@ -377,6 +378,17 @@ public function originalIsEquivalent($key) | |
$this->castAttribute($key, $original); | ||
} | ||
|
||
if ($this->isClassCastable($key)) { | ||
$attribute = $this->castAttribute($key, $attribute); | ||
$original = $this->castAttribute($key, $original); | ||
|
||
if ($attribute === $original) { | ||
return true; | ||
} | ||
|
||
return serialize($attribute) === serialize($original); | ||
|
||
} | ||
|
||
return is_numeric($attribute) && is_numeric($original) | ||
&& strcmp((string) $attribute, (string) $original) === 0; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Models; | ||
|
||
class Options | ||
{ | ||
private string $option1; | ||
private string $option2; | ||
guram-vashakidze marked this conversation as resolved.
Show resolved
Hide resolved
guram-vashakidze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function setOption1(string $option1): self | ||
{ | ||
$this->option1 = $option1; | ||
return $this; | ||
} | ||
|
||
public function setOption2(string $option2): self | ||
{ | ||
$this->option2 = $option2; | ||
return $this; | ||
} | ||
|
||
public function serialize(): object | ||
{ | ||
$result = []; | ||
if (isset($this->option1)) { | ||
$result['option1'] = $this->option1; | ||
} | ||
|
||
if (isset($this->option2)) { | ||
$result['option2'] = $this->option2; | ||
} | ||
|
||
return (object) $result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Tests\Models; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class OptionsCast implements CastsAttributes | ||
{ | ||
public function get(Model $model, string $key, mixed $value, array $attributes): Options | ||
{ | ||
$attributes = new Options(); | ||
if (! empty($value['option1'])) { | ||
$attributes->setOption1($value['option1']); | ||
} | ||
|
||
if (! empty($value['option2'])) { | ||
$attributes->setOption2($value['option2']); | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* @param Model $model | ||
* @param string $key | ||
* @param Options|null $value | ||
* @param array $attributes | ||
* | ||
* @return null[]|object[] | ||
*/ | ||
public function set(Model $model, string $key, mixed $value, array $attributes): array | ||
{ | ||
return [ | ||
$key => $value?->serialize(), | ||
]; | ||
} | ||
} |
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.
This comparison should use
===
for scalars and==
for objects. For example, anything that casts to a BSON object instance (e.g. a UUID stored as a BSON binary) will never pass this check, even if they are equivalent.There are also some instances of comparisons that would produce the same BSON result (e.g. involving
MongoDB\BSON\Int64
, but I think we can ignore those in this check.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.
answer in next thread