-
-
Notifications
You must be signed in to change notification settings - Fork 586
Closed
Labels
Union TypesSupport for Union TypesSupport for Union Types
Description
| Q | A |
|---|---|
| Bug report? | yes |
| Feature request? | no |
| BC Break report? | no |
| RFC? | no |
Steps required to reproduce the problem
- Create an entity with a (virtual) property with a union (return) type definition (ex:
function getValue(): int|string|bool|float) - Serialize the entity/property to JSON and return
int(1)fromgetValue(). Expectint(1)in the serialized data - Serializer will detect a boolean value instead of an int and serialize
bool(true)
Expected Result
- For serialization, the returned types should be handled strict. So if an
int(1)is returned, it should beint(1)and notbool(true)
Actual Result
int(1)is detected as primitive type bool and will get serialized as bool
Related Code
UnionHandler::testPrimitive()TypePropertiesDriver::reorderTypes()
Problem
- Despite the doc block on
TypePropertiesDriver::reorderTypes(), which saysintshould come beforebool,boolis sorted beforeintand will be tested first inUnionHandler::testPrimitive(). UnionHandler::testPrimitive()uses dynamic type conversions ((string)and(bool)) to check for the types. Therefore,int(1)will first get checked for bool and the dynamic conversion will result in a positive test.
Bug
Will result in a positive match for bool type for int(1) and int(0)
(string) (bool) $data === (string) $data
https://github.com/schmittjoh/serializer/blob/master/src/Handler/UnionHandler.php#L134
Suggested Solution
(Sorry for not providing a pull request. But I'm not that experienced with the internals of the serializer compontent and such a change might have unexpected results I'm not aware of).
- Change the type order in
TypePropertiesDriver::reorderTypes(), so int is checked before bool - Make the checks in
UnionHandler::testPrimitive()more strict, at least for serialization, since here we can expect exact types coming from the entities
dmaicher
Metadata
Metadata
Assignees
Labels
Union TypesSupport for Union TypesSupport for Union Types