10000 Fix: Inconsistent accessor attribute name conversion by pandiselvamm · Pull Request #54578 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Fix: Inconsistent accessor attribute name conversion #54578

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,7 @@ protected function mutateAttributeForArray($key, $value)
{
if ($this->isClassCastable($key)) {
$value = $this->getClassCastableAttributeValue($key, $value);
} elseif (isset(static::$getAttributeMutatorCache[get_class($this)][$key]) &&
static::$getAttributeMutatorCache[get_class($this)][$key] === true) {
} elseif ($this->hasAttributeGetMutator($key)) {
$value = $this->mutateAttributeMarkedAttribute($key, $value);

$value = $value instanceof DateTimeInterface
Expand Down
35 changes: 35 additions & 0 deletions tests/Database/DatabaseConcernsHasAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ public function testUnsettingCachedAttribute()

$this->assertFalse($instance->cachedAttributeIsset('cacheableProperty'));
}

public function testSnakeCaseMutateAttributeMarkedAttribute()
{
$instance = new HasAttributesWithSnakeCaseConsistentCheck();
$this->assertEquals('foo1Bar', $instance->getAttribute('foo1Bar'));
$this->assertEquals('foo1Bar', $instance->getAttribute('foo1bar'));
$this->assertEquals('foo1Bar', $instance->getAttribute('foo_1_bar'));
$this->assertEquals('foo1Bar', $instance->getAttribute('foo1_bar'));
$this->assertEquals('foo1Bar', $instance->getAttribute('foo_1bar'));
}

public function testSnakeCaseMutateAttribute()
{
$instance = new HasAttributesWithSnakeCaseConsistentCheck();
$this->assertEquals('foo2Bar', $instance->getAttribute('foo2Bar'));
Copy link
Contributor
@jackbayliss jackbayliss Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these tests are correct, as getAttribute was working properly before the change you've made. You can test it in Tinker ie before & after.

I believe it should be testing ->append('foo_2_bar')->toArray() etc, ie the same scenario as the issue, as it's altered mutateAttributeForArray which is called by attributesToArray

(ie the test should be testing the scenario of the issue to verify it's fixed :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure I will make a test for those too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jackbayliss , tested with multiple scenarios including toArray() , its is working fine as excpected

$this->assertEquals('foo2Bar', $instance->getAttribute('foo2bar'));
$this->assertEquals('foo2Bar', $instance->getAttribute('foo_2_bar'));
$this->assertEquals('foo2Bar', $instance->getAttribute('foo2_bar'));
$this->assertEquals('foo2Bar', $instance->getAttribute('foo_2bar'));
}
}

class HasAttributesWithoutConstructor
Expand Down Expand Up @@ -118,3 +138,18 @@ public function cachedAttributeIsset($attribute): bool
return isset($this->attributeCastCache[$attribute]);
}
}

class HasAttributesWithSnakeCaseConsistentCheck extends Model
{
public function foo1Bar(): Attribute
{
return Attribute::make(
get: fn () => 'foo1Bar'
);
}

public function getFoo2BarAttribute(): string
{
return 'foo2Bar';
}
}
Loading
0