schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 586 forks source link

Serialization of union type will return bool(true) instead of int(1) #1573

Open fabianerni opened 1 day ago

fabianerni commented 1 day ago
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

Steps required to reproduce the problem

  1. Create an entity with a (virtual) property with a union (return) type definition (ex: function getValue(): int|string|bool|float)
  2. Serialize the entity/property to JSON and return int(1) from getValue(). Expect int(1) in the serialized data
  3. Serializer will detect a boolean value instead of an int and serialize bool(true)

Expected Result

Actual Result

Related Code

Problem

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).

scyzoryck commented 21 hours ago

@fabianerni thanks for report! The proposal for changes makes sense - could you please make pull request with the changes please? I can help in case of issues.

Best, Marcin.