Skip to content
Open
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
10 changes: 5 additions & 5 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ private function normalizeValue(mixed $data): mixed
// Check for Entity instance (use raw values, recursive)
if ($data instanceof self) {
$objectData = $data->toRawArray(false, true);
} elseif ($data instanceof UnitEnum) {
return [
'__class' => $data::class,
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
];
} elseif ($data instanceof JsonSerializable) {
$objectData = $data->jsonSerialize();
} elseif (method_exists($data, 'toArray')) {
Expand All @@ -469,11 +474,6 @@ private function normalizeValue(mixed $data): mixed
'__class' => $data::class,
'__datetime' => $data->format(DATE_RFC3339_EXTENDED),
];
} elseif ($data instanceof UnitEnum) {
return [
'__class' => $data::class,
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
];
} else {
$objectData = get_object_vars($data);

Expand Down
29 changes: 29 additions & 0 deletions tests/_support/Enum/StateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Enum;

/**
* An enum that also defines toArray(), used to test that UnitEnum handling
* takes precedence over toArray() in Entity::normalizeValue().
*/
enum StateEnum: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';

public function toArray(): array
{
return array_column(self::cases(), 'value');
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're returning scalar strings, so this test does not test the reported recursion path.

}
19 changes: 19 additions & 0 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Tests\Support\Entity\Cast\NotExtendsBaseCast;
use Tests\Support\Enum\ColorEnum;
use Tests\Support\Enum\RoleEnum;
use Tests\Support\Enum\StateEnum;
use Tests\Support\Enum\StatusEnum;
use Tests\Support\SomeEntity;

Expand Down Expand Up @@ -1045,6 +1046,24 @@ public function testCastEnumSetWithUnitEnumObject(): void
$this->assertSame(ColorEnum::RED, $entity->color);
}

public function testInjectRawDataWithEnumThatHasToArrayMethod(): void
{
// Regression test for https://github.com/codeigniter4/CodeIgniter4/issues/10136
// Enums implementing toArray() must still be handled by the UnitEnum branch in
// normalizeValue(), so hasChanged() does not incorrectly report a change after
// injectRawData() stores the same enum value.
$entity = new class () extends Entity {};

$entity->injectRawData(['state' => StateEnum::DRAFT]);

// toRawArray() returns raw attributes, so the enum object is returned as-is.
$this->assertSame(StateEnum::DRAFT, $entity->toRawArray()['state']);

// The key assertion: normalizeValue() must treat the enum as a UnitEnum
// (not call toArray() on it), so the original and current normalized forms match.
$this->assertFalse($entity->hasChanged('state'));
}
Comment on lines +1049 to +1065
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove all these comments, and just add in the docblock:

@see https://github.com/codeigniter4/CodeIgniter4/issues/10136


public function testAsArray(): void
{
$entity = $this->getEntity();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bugs Fixed
- **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``stty`` error output (e.g., ``stty: 'standard input': Inappropriate ioctl for device``) to stderr when stdin was not a TTY.
- **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment.
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.
- **Entity:** Fixed a bug where ``Entity::normalizeValue()`` did not handle ``UnitEnum`` before checking for ``toArray()``, causing enums implementing ``toArray()`` to be incorrectly normalized as generic objects instead of enums.
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.

See the repo's
Expand Down
Loading