Skip to content

[2.4.3] No resource class found for object of type when using DTO as output #2860

Description

@karser

Hi Folks!

I'm using api-platform for handling custom use cases:

App\UseCase\SampleUseCaseRequest:
    collectionOperations:
        post:
            path: '/sample-path'
            messenger: true
            output: 'App\UseCase\SampleUseCaseResponse'

This configuration worked well with v2.4.2. After upgrading to 2.4.3 (and 2.4.4 as well) I started getting this issue:

{"@context":"\/contexts\/Error","@type":"hydra:Error","hydra:title":"An error occurred","hydra:description":"No resource class found for object of type \u0022App\\UseCase\\SampleUseCaseResponse\u0022.","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"vendor\/api-platform\/core\/src\/Api\/ResourceClassResolver.php","line":54,"args":[]},{"namespace":"ApiPlatform\\Core\\Api","short_class":"ResourceClassResolver","class":"ApiPlatform\\Core\\Api\\ResourceClassResolver","type":"-\u003E","function":"getResourceClass","file":"vendor\/api-platform\/core\/src\/Util\/ResourceClassInfoTrait.php","line":52,"args":[["object","App\\UseCase\\SampleUseCaseResponse"]]},{"namespace":"ApiPlatform\\Core\\Bridge\\Symfony\\Routing","short_class":"IriConverter","class":"ApiPlatform\\Core\\Bridge\\Symfony\\Routing\\IriConverter","type":"-\u003E","function":"getResourceClass","file":"vendor\/api-platform\/core\/src\/Bridge\/Symfony\/Routing\/IriConverter.php","line":120,"args":[["object","App\\UseCase\\SampleUseCaseResponse"],["boolean",true]]},{"namespace":"ApiPlatform\\Core\\Bridge\\Symfony\\Routing","short_class":"IriConverter","class":"ApiPlatform\\Core\\Bridge\\Symfony\\Routing\\IriConverter","type":"-\u003E","function":"getIriFromItem","file":"src\/ApiPlatform\/ParamsAwareIriConverter.php","line":62

So IriConverter::getIriFromItem calls ResourceClassResolver::getResourceClass which throws InvalidArgumentException if the output is not an api resource.
image

As a workaround I decorated IriConverter and added fallback:

class FallbackIriConverter implements IriConverterInterface {
    private $decorated;

    public function __construct(IriConverterInterface $decorated) {
        $this->decorated = $decorated;
    }

    public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
    {
        try {
            return $this->decorated->getIriFromItem($item, $referenceType);
        } catch (InvalidArgumentException $e) {
            return '';
        }
    }

}

I'm wondering is it a bug and how to better approach this issue? Is IRI critically important for the non-resource output?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions