Skip to content

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

@fabianerni

Description

@fabianerni
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

  • For serialization, the returned types should be handled strict. So if an int(1) is returned, it should be int(1) and not bool(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 says int should come before bool, bool is sorted before int and will be tested first in UnionHandler::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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Union TypesSupport for Union Types

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions