{"action":"created","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/19545","repository_url":"https://api.github.com/repos/symfony/symfony","labels_url":"https://api.github.com/repos/symfony/symfony/issues/19545/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/19545/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/19545/events","html_url":"https://github.com/symfony/symfony/issues/19545","id":169584097,"number":19545,"title":"Allow arrays being denormalized in AbstractObjectNormalizer ","user":{"login":"hensoko","id":13552930,"avatar_url":"https://avatars.githubusercontent.com/u/13552930?v=3","gravatar_id":"","url":"https://api.github.com/users/hensoko","html_url":"https://github.com/hensoko","followers_url":"https://api.github.com/users/hensoko/followers","following_url":"https://api.github.com/users/hensoko/following{/other_user}","gists_url":"https://api.github.com/users/hensoko/gists{/gist_id}","starred_url":"https://api.github.com/users/hensoko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hensoko/subscriptions","organizations_url":"https://api.github.com/users/hensoko/orgs","repos_url":"https://api.github.com/users/hensoko/repos","events_url":"https://api.github.com/users/hensoko/events{/privacy}","received_events_url":"https://api.github.com/users/hensoko/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/symfony/symfony/labels/Feature","name":"Feature","color":"02e10c"},{"url":"https://api.github.com/repos/symfony/symfony/labels/Serializer","name":"Serializer","color":"DDDDDD"},{"url":"https://api.github.com/repos/symfony/symfony/labels/Status:%20Needs%20Review","name":"Status: Needs Review","color":"ededed"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2016-08-05T11:06:35Z","updated_at":"2016-08-08T11:26:23Z","closed_at":null,"body":"Heyho,\r\n\r\nI currently work on a problem when using the ObjectNormalizer with an multidimensional array as input. My array looks like this:\r\n\r\n```php\r\n[\r\n    'base_property1' => 'value123',\r\n    'nested_resources' => [\r\n        [\r\n            'nested_property' => 'value1234'\r\n        ],\r\n        [\r\n            'nested_property' => 'value2345'\r\n        ],\r\n    ]\r\n]\r\n```\r\n\r\nI defined the following classes the data of this array should be passed to:\r\n\r\n```php\r\nclass Resource {\r\n    /**\r\n     * @var string\r\n     */\r\n    private $baseProperty1;\r\n\r\n    /**\r\n     * @var nestedResource[]|ArrayCollection\r\n     */\r\n    private $nestedResources;\r\n\r\n    public function getBaseProperty1() {\r\n        return $this->baseProperty1;\r\n    }\r\n\r\n    public function setBaseProperty1($baseProperty1) {\r\n        $this->baseProperty1 = $baseProperty1;\r\n        return $this;\r\n    }\r\n\r\n    public function getNestedResources() {\r\n        return $this->nestedResources;\r\n    }\r\n\r\n    public function setNestedResources($nestedResources) {\r\n        $this->nestedResources = $nestedResources;\r\n        return $this;\r\n    }\r\n}\r\n\r\nclass NestedResource {\r\n     /**\r\n     * @var string\r\n     */\r\n    private $nestedProperty;\r\n\r\n    public function getNestedProperty() {\r\n        return $this->nestedProperty;\r\n    }\r\n\r\n    public function setNestedProperty($nestedProperty) {\r\n        $this->nestedProperty = $nestedProperty;\r\n        return $this;\r\n    }\r\n}\r\n```\r\n\r\nWhen using the Serializer-Component of Symfony to denormalize the input-array I expected to get the following result:\r\n\r\n```php\r\n   Resource {\r\n        -baseProperty1: \"value123\"\r\n        -nestedResources: array:2 [\r\n            0 => NestedResource {\r\n                -nestedProperty: 'value1234'\r\n            }\r\n            2 => NestedResource {\r\n                -nestedProperty: 'value2345'\r\n            }\r\n        ]\r\n   }\r\n```\r\n\r\nbut instead I get the following:\r\n\r\n```php\r\n   Resource {\r\n        -baseProperty1: \"value123\"\r\n        -nestedResources: array:2 [\r\n            0 => array [\r\n                nestedProperty => 'value1234'\r\n            }\r\n            1 => array [\r\n                nestedProperty => 'value2345'\r\n            }\r\n        ]\r\n   }\r\n```\r\n\r\nI defined the following services to get a serializer-service using the ReflectionExtrator:\r\n\r\n```yaml\r\nservices:\r\n    name_converter.camel_case_to_sneak_case_name:\r\n        class: Symfony\\Component\\Serializer\\NameConverter\\CamelCaseToSnakeCaseNameConverter\r\n\r\n    property_accessor.reflection:\r\n        class: Symfony\\Component\\PropertyInfo\\Extractor\\ReflectionExtractor\r\n\r\n    denormalizer.array:\r\n        class: Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer\r\n\r\n    normalizer.object:\r\n        class: Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer\r\n        arguments:\r\n            -\r\n            - \"@name_converter.camel_case_to_sneak_case_name\"\r\n            -\r\n            - \"@property_accessor.reflection\"\r\n\r\n    encoder.json:\r\n        class: Symfony\\Component\\Serializer\\Encoder\\JsonEncoder\r\n\r\n    serializer:\r\n        class: Symfony\\Component\\Serializer\\Serializer\r\n        arguments:\r\n            - [\"@normalizer.object\", \"@denormalizer.array\"]\r\n            - [\"@encoder.json\"]\r\n```\r\n\r\nI tested it with latest Symfony 3.1.3. I think the issue occures because of a missing check for a collection in class `AbstractObjectNormalizer` in line [255 to 263](https://github.com/symfony/serializer/blob/v3.1.3/Normalizer/AbstractObjectNormalizer.php#L255-L263). It is only checked for an object, but if `$type` is an collection this is not honored. I get a correct result if I add the following check just before line 255, but I think this is not a nice solution because of the need to add brackets after the class-name:\r\n\r\n```php\r\nif ($type->isCollection() && $type->getCollectionValueType()->getBuiltinType() === Type::BUILTIN_TYPE_OBJECT) {\r\n    $class = $type->getCollectionValueType()->getClassName() . '[]';\r\n    $builtinType = $type->getCollectionValueType()->getBuiltinType();\r\n}\r\n```\r\n\r\nI get the same result with PhpDocExtractor."},"comment":{"url":"https://api.github.com/repos/symfony/symfony/issues/comments/238209271","html_url":"https://github.com/symfony/symfony/issues/19545#issuecomment-238209271","issue_url":"https://api.github.com/repos/symfony/symfony/issues/19545","id":238209271,"user":{"login":"hensoko","id":13552930,"avatar_url":"https://avatars.githubusercontent.com/u/13552930?v=3","gravatar_id":"","url":"https://api.github.com/users/hensoko","html_url":"https://github.com/hensoko","followers_url":"https://api.github.com/users/hensoko/followers","following_url":"https://api.github.com/users/hensoko/following{/other_user}","gists_url":"https://api.github.com/users/hensoko/gists{/gist_id}","starred_url":"https://api.github.com/users/hensoko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hensoko/subscriptions","organizations_url":"https://api.github.com/users/hensoko/orgs","repos_url":"https://api.github.com/users/hensoko/repos","events_url":"https://api.github.com/users/hensoko/events{/privacy}","received_events_url":"https://api.github.com/users/hensoko/received_events","type":"User","site_admin":false},"created_at":"2016-08-08T11:26:23Z","updated_at":"2016-08-08T11:26:23Z","body":"Something like this:\r\n\r\n```php\r\n  -premises: array:4 [▼\r\n    0 => array:35 [▶]\r\n    1 => array:35 [▶]\r\n    2 => array:35 [▶]\r\n    3 => array:35 [▶]\r\n  ]\r\n```\r\n\r\nBut what I expected was this:\r\n```php\r\n  -premises: array:4 [▼\r\n    0 => Premise {▶}\r\n    1 => Premise {▶}\r\n    2 => Premise {▶}\r\n    3 => Premise {▶}\r\n  ]\r\n```\r\n\r\nWhen I do a dump of `$type`-variable in `AbstractObjectNormalizer` Line 255 I get the following result:\r\n\r\n```php\r\nType {#877 ▼\r\n  -builtinType: \"array\"\r\n  -nullable: false\r\n  -class: null\r\n  -collection: true\r\n  -collectionKeyType: Type {#875 ▼\r\n    -builtinType: \"int\"\r\n    -nullable: false\r\n    -class: null\r\n    -collection: false\r\n    -collectionKeyType: null\r\n    -collectionValueType: null\r\n  }\r\n  -collectionValueType: Type {#876 ▼\r\n    -builtinType: \"object\"\r\n    -nullable: false\r\n    -class: \"AppBundle\\Resource\\Premise\"\r\n    -collection: false\r\n    -collectionKeyType: null\r\n    -collectionValueType: null\r\n  }\r\n}\r\n```\r\n\r\nSo I think the class-type is correctly figured-out by the property type extractor but data being an array of classes is not handled properlt."}}
{"actor":{"display_login":"hensoko"}}