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

AbstractVisitor::getElementType() must be of the type array or null, string returned #1027

Closed
iaejean opened this issue Jan 6, 2019 · 5 comments

Comments

@iaejean
Copy link

iaejean commented Jan 6, 2019

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

I'm getting the follow message Return value of JMS\Serializer\AbstractVisitor::getElementType() must be of the type array or null, string returned when I'm trying to serialize an object.

Steps required to reproduce the problem

  1. I have a estructure similar to:
/**
 * @Serializer\Discriminator(
 *     disabled=true,
 *     field="type",
 *     map={
 *         "Company": "Company",
 *         "Branch":     "Branch"
 *     }
 * )
 */
abstract class AbstractModel {}

class Company extend AbstractModel {}

class Branch extend AbstractModel {}

class Link  {

   /**
     * @Serializer\Type("string")
     */
    private $next;

    /**
     * @Serializer\Type("string")
     */
    private $prev;
}

class Pagination
{
    /**
     * @Serializer\Type("Link")
     */
    private $links;

    /**
     * @Serializer\Type("integer")
     */
    private $limit;

    /**
     * @Serializer\Type("integer")
     */
    private $size;

    /**
     * @Serializer\Type("integer")
     */
    private $start;

    /**
     * @Serializer\Type("integer")
     */
    private $total;

    /**
     * @Serializer\Type("ArrayCollection<AbstractModel>")
     */
    private $results;
}

Expected Result

{
    "links": {},
    "limit": 20,
    "size": 1,
    "start": 1,
    "total": 1,
    "results": [
        {
            "id": 1,
            "name": "My Branch",
            "picture": "url",
            "address": "",
            "phone": "",
            "longitude": "",
            "latitude": "",
            "company": {
                "id": 1,
                "name": "My Compay",
                "picture": "url picture",
                "description": "",
                "website": "url website",
                "logo": "url log",
                "address": "",
                "phone": "",
                "latitude": "",
                "longitude": ""
            },
            "category": [
                {
                    "name": "Games",
                    "description": "Games",
                    "restaurant": []
                },
                {
                    "name": "Familiar",
                    "description": "Familiar",
                    "restaurant": []
                }
            ]
        }
    ]
}

Actual Result

Message: Return value of JMS\Serializer\AbstractVisitor::getElementType() must be of the type array or null, string returned

@goetas
Copy link
Collaborator

goetas commented Jan 6, 2019

can you post please the stack trace?

@iaejean
Copy link
Author

iaejean commented Jan 6, 2019

@goetas
If I print the value of $typeArray when crash I get the follow data:

screen shot 2019-01-06 at 2 09 43 pm

Then I've added
a print to get the stack trace

AbstractVisitor at line 39:

    if (isset($typeArray['params'][0]) && !\is_array($typeArray['params'][0])) {
        dd(debug_print_backtrace(30));
    }
#0  JMS\Serializer\AbstractVisitor->getElementType() called at [/my-project/vendor/jms/serializer/src/JsonSerializationVisitor.php:90]
#1  JMS\Serializer\JsonSerializationVisitor->visitArray() called at [/my-project/vendor/jms/serializer/src/Handler/ArrayCollectionHandler.php:81]
#2  JMS\Serializer\Handler\ArrayCollectionHandler->serializeCollection() called at [/my-project/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:192]
#3  JMS\Serializer\GraphNavigator\SerializationGraphNavigator->accept() called at [/my-project/vendor/jms/serializer/src/JsonSerializationVisitor.php:136]
#4  JMS\Serializer\JsonSerializationVisitor->visitProperty() called at [/my-project/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:234]
#5  JMS\Serializer\GraphNavigator\SerializationGraphNavigator->accept() called at [/my-project/vendor/jms/serializer/src/JsonSerializationVisitor.php:93]
#6  JMS\Serializer\JsonSerializationVisitor->visitArray() called at [/my-project/vendor/jms/serializer/src/Handler/ArrayCollectionHandler.php:81]
#7  JMS\Serializer\Handler\ArrayCollectionHandler->serializeCollection() called at [/my-project/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:192]
#8  JMS\Serializer\GraphNavigator\SerializationGraphNavigator->accept() called at [/my-project/vendor/jms/serializer/src/JsonSerializationVisitor.php:136]
#9  JMS\Serializer\JsonSerializationVisitor->visitProperty() called at [/my-project/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:234]
#10 JMS\Serializer\GraphNavigator\SerializationGraphNavigator->accept() called at [/my-project/vendor/jms/serializer/src/JsonSerializationVisitor.php:93]
#11 JMS\Serializer\JsonSerializationVisitor->visitArray() called at [/my-project/vendor/jms/serializer/src/Handler/ArrayCollectionHandler.php:81]
#12 JMS\Serializer\Handler\ArrayCollectionHandler->serializeCollection() called at [/my-project/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:192]
#13 JMS\Serializer\GraphNavigator\SerializationGraphNavigator->accept() called at [/my-project/vendor/jms/serializer/src/JsonSerializationVisitor.php:136]
#14 JMS\Serializer\JsonSerializationVisitor->visitProperty() called at [/my-project/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:234]
#15 JMS\Serializer\GraphNavigator\SerializationGraphNavigator->accept() called at [/my-project/vendor/jms/serializer/src/Serializer.php:249]
#16 JMS\Serializer\Serializer->visit() called at [/my-project/vendor/jms/serializer/src/Serializer.php:162]
#17 JMS\Serializer\Serializer->serialize() called at [/my-project/src/Application/Common/TraitListener.php:86]
#18 App\Application\EventListener\ResponseListener->jsonResponse() called at [/my-project/src/Application/Common/TraitListener.php:69]
#19 App\Application\EventListener\ResponseListener->response() called at [/my-project/src/Application/EventListener/ResponseListener.php:36]
#20 App\Application\EventListener\ResponseListener->onKernelResponse() called at [/my-project/vendor/symfony/event-dispatcher/Debug/WrappedListener.php:111]
#21 Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke() called at [/my-project/vendor/symfony/event-dispatcher/EventDispatcher.php:212]
#22 Symfony\Component\EventDispatcher\EventDispatcher->doDispatch() called at [/my-project/vendor/symfony/event-dispatcher/EventDispatcher.php:44]
#23 Symfony\Component\EventDispatcher\EventDispatcher->dispatch() called at [/my-project/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:142]
#24 Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch() called at [/my-project/vendor/symfony/http-kernel/HttpKernel.php:189]
#25 Symfony\Component\HttpKernel\HttpKernel->filterResponse() called at [/my-project/vendor/symfony/http-kernel/HttpKernel.php:171]
#26 Symfony\Component\HttpKernel\HttpKernel->handleRaw() called at [/my-project/vendor/symfony/http-kernel/HttpKernel.php:67]
#27 Symfony\Component\HttpKernel\HttpKernel->handle() called at [/my-project/vendor/symfony/http-kernel/Kernel.php:198]
#28 Symfony\Component\HttpKernel\Kernel->handle() called at [/my-project/public/index.php:37]

@iaejean
Copy link
Author

iaejean commented Jan 9, 2019

Basically I just validate if the element isn't array return null, it looks like is working fine

@goetas
Copy link
Collaborator

goetas commented Jan 11, 2019

Something is wrong with your type.

You have:

array(
  'name' => 'array',
  'params' => array(
    0 => 'Branch'     
  )
)

but should be

array(
  'name' => 'array',
  'params' => array(
    0 => array(
       'name' => 'Branch',
       'params' => array()
    )
  )
)

Are you using some custom handler where you define the type manually?

@iaejean
Copy link
Author

iaejean commented Jan 12, 2019

Not, there isn't any custom handler

@iaejean iaejean closed this as completed Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants