schmittjoh / serializer

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

Minor Issue from Triggering Autoload Before Expanding Class Names #1512

Closed rossriley closed 9 months ago

rossriley commented 10 months ago
Q A
Bug report? yes (possibly)
Feature request? no
BC Break report? no
RFC? no

Appreciate this is probably an edge case but others may have run into it too. If you use an interactive shell to interact with deserialization via the DocBlockTypeResolver the class will try to autoload before it has been properly expanded via the use statements within the class.

Steps required to reproduce the problem

With this simplified version of the setup:

<?php

namespace ExampleAPI\Response;

use ExampleAPI\Models\Product;

class ProductCollection
{
    /** @var Product[] */
    public array $data;
}

Expected Result

After deserializing it would be expected that the resolved type would include the use statement but the autoloader in PsySH will scan the project for other classes named Product and will possibly select the wrong class.

Possible change

The logic to resolve is here: https://github.com/schmittjoh/serializer/blob/master/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php#L250

The first few lines use class_exists('Product') rather than expanding out from the use statement first. My question is, whilst most people won't run into this problem wouldn't it make sense to expand the class from the use statements first and call class_exists('ExampleAPI\Models\Product') first and then fallback to the non namespaced class if the first is not successful?

Happy to PR if you agree.

scyzoryck commented 10 months ago

Hi! Looking at your explanation - it looks like a bug (edgecase but still bug). Feel free to create an Pull Request with the fix. :) let me know if you'd need any help with preparing the fix.

Best scyzoryck

goetas commented 9 months ago

Fixed in https://github.com/schmittjoh/serializer/pull/1513