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

Queue Fails to Create Payload with Invalid Characters in Data #12194

Closed
jamesmirvine opened this issue Feb 7, 2016 · 9 comments
Closed

Queue Fails to Create Payload with Invalid Characters in Data #12194

jamesmirvine opened this issue Feb 7, 2016 · 9 comments
Labels

Comments

@jamesmirvine
Copy link
Contributor

I have an eloquent Model with a 16 bit UUID attribute. When represented as a UTF-8 string, it contains malformed characters, which is expected. I use the attribute getter/setters to transform the UUID into the 36 character readable string, but when passed into the closure of a Mail::queue it causes json_encode to fail inside of \Illuminate\Queue\Queue::createPayload. This results in a ReflectionException.

Relevant json_encode error message:
json_last_error_msg => "Malformed UTF-8 characters, possibly incorrectly encoded"

Hiding the attribute in the Model doesn't having any affect on the serialized data string, since the entire object is serialized including the $original array.

I can get around this by passing just the subset of the Model data that is needed for the mail, but perhaps there's a better way of handling this in the framework?

@GrahamCampbell
Copy link
Member

Ping @taylorotwell.

@lucasmichot
Copy link
Contributor

@JamesGuthrie
Have you tried to output your model as JSON using \Illuminate\Database\Eloquent\Model::toJson ?
This should return an error too.
Do you have the same error with \Illuminate\Database\Eloquent\Model::toJson(JSON_UNESCAPED_UNICODE) ?

@jamesmirvine
Copy link
Contributor Author

The result of \Illuminate\Database\Eloquent\Model::toJson, with or without the JSON_UNESCAPED_UNICODE option, is the same and has no error:

{"id":2,"uuid":{}}

The class which handles the UUID doesn't have any public fields, which is why it's empty here. The attribute methods, within the Model, for the UUID are:

use Rhumsaa\Uuid\Uuid;
...
public function getUuidAttribute($value)
{
    return Uuid::fromBytes($value);
}

public function setUuidAttribute($value)
{
    $this->attributes['uuid'] = $uuid->getBytes();
}

And this is the library referenced: Rhumsaa\Uuid

@lucasmichot
Copy link
Contributor

I don't understand why the UUID is not just stored as a string

// Generate a version 1 (time-based) UUID object
$uuid1 = Uuid::uuid1();
echo $uuid1->toString() . "\n"; // i.e. e4eaaaf2-d142-11e1-b3e4-080027620cdd

// Generate a version 3 (name-based and hashed with MD5) UUID object
$uuid3 = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net');
echo $uuid3->toString() . "\n"; // i.e. 11a38b9a-b3da-360f-9353-a5a725514269

// Generate a version 4 (random) UUID object
$uuid4 = Uuid::uuid4();
echo $uuid4->toString() . "\n"; // i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a

// Generate a version 5 (name-based and hashed with SHA1) UUID object
$uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net');
echo $uuid5->toString() . "\n"; // i.e. c4a760a8-dbcf-5254-a0d9-6a4474bd1b62

@lucasmichot
Copy link
Contributor

Ping @jamesmirvine ?

@jamesmirvine
Copy link
Contributor Author

For certain scenarios, storing the UUID as a string could be a viable workaround. But in general, the issue remains that stored byte-strings inside of Eloquent Models will cause an exception when passed to the queue.

@silasrm
Copy link

silasrm commented May 14, 2017

What is a solution @jamesmirvine ?

@jamesmirvine
Copy link
Contributor Author

The simplest way is to create a new object with only those fields which are required.
If you need a byte-string field, then you'll have to convert the field from byte-string to string manually before queuing. Basically, create a JSON friendly object to pass into the queue.

@silasrm
Copy link

silasrm commented May 15, 2017

Thank's for answer me. I'm doing the same. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants