Skip to content
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

[5.4] Unencodable casted data in models is silently discarded #17804

Merged
merged 4 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Database\Eloquent\JsonEncodingException;

trait HasAttributes
{
Expand Down Expand Up @@ -527,6 +528,9 @@ public function setAttribute($key, $value)

if ($this->isJsonCastable($key) && ! is_null($value)) {
$value = $this->asJson($value);
if (false === $value) {
throw JsonEncodingException::forAttribute($key, json_last_error_msg());
}
}

// If this attribute contains a JSON ->, we'll set the proper value in the
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Eloquent/JsonEncodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ public static function forModel($model, $message)
{
return new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
}

/**
* Create a new JSON encoding exception for an attribute.
*
* @param mixed $key
* @param string $message
* @return static
* @internal param mixed $key
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure @internal is necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lucasmichot Agreed. It slipped by me as I was renaming the parameter. I'll correct in a second.

*/
public static function forAttribute($key, $message)
{
return new static('Error encoding value of attribute ['.$key.'] to JSON: '.$message);
}
}
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,22 @@ public function testModelAttributeCastingPreservesNull()
$this->assertNull($array['timestampAttribute']);
}

/**
* @expectedException \Illuminate\Database\Eloquent\JsonEncodingException
* @expectedExceptionMessage Error encoding value of attribute
*/
public function testModelAttributeCastingFailsOnUnencodableData()
{
$model = new EloquentModelCastingStub;
$model->objectAttribute = ['foo' => "b\xF8r"];
$obj = new StdClass;
$obj->foo = "b\xF8r";
$model->arrayAttribute = $obj;
$model->jsonAttribute = ['foo' => "b\xF8r"];

$model->getAttributes();
}

public function testUpdatingNonExistentModelFails()
{
$model = new EloquentModelStub;
Expand Down