Skip to content

WebAPI Error: Class \"\\Vendor\\Module\\...\\FooBar[]\" does not exist #40018

Open
@NuktukDev

Description

@NuktukDev

Summary

I am trying to use the REST Web API to return a collection of a certain interface.

The docs specifically say:

Any parameters or return values of type array can be denoted by following any of the previous types by an empty set of square brackets []

And provide the example:

A return which is an array of objects of type \Magento\Customer\Api\Data\CustomerInterface:
@return \Magento\Customer\Api\Data\CustomerInterface[]

https://developer.adobe.com/commerce/php/development/components/web-api/services/#service-interface-requirements

Vendor\Module\Api\FooRepositoryInterface:

namespace Vendor\Module\Api;

interface FooRepositoryInterface
{
    /**
     * @param string $foo
     * @param string $bar
     * @return \Vendor\Module\Api\Data\FooInterface[]
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getStatus($foo, $bar);
}

Vendor\Module\Api\Data\FooInterface:

<?php
namespace Vendor\Module\Api\Data;

use Magento\Framework\Exception\LocalizedException;

interface FooInterface {

    /**
     * @return ?int
     * @throws LocalizedException
     */
    public function getId();
    
    //...........
}

Vendor\Module\Model\FooRepository:

<?php
namespace Vendor\Module\Model\Api;

use Vendor\Module\Api\FooRepositoryInterface;

use Magento\Framework\Exception\NoSuchEntityException;

class FooRepository implements FooRepositoryInterface
{
  // ..........................
  /**
     * @inheritdoc
     */
    public function getStatus($foo, $bar)
    {
        return $this->FooCollectionFactory->create()
            ->addFieldToFilter('foo', ['eq' => $foo])
            ->addFieldToFilter('bar', ['eq' => $bar]);
     }
     // ............
}

When calling the API it gives me the error:

	"message": "Class \"\\Vendor\\Module\\Api\\Data\\FooInterface[]\" does not exist",
	"code": -1,
	"trace": "#0 \/var\/www\/html\/vendor\/magento\/framework\/Reflection\/MethodsMap.php(166): ReflectionClass->__construct('\\\Vendor\\\\...')\n#1 \/var\/www\/html\/vendor\/magento\/framework\/Reflection\/MethodsMap.php(115): Magento\\Framework\\Reflection\\MethodsMap->getMethodMapViaReflection('\\\\Vendor\\\\...')\n#2 \/var\/www\/html\/vendor\/magento\/framework\/Reflection\/DataObjectProcessor.php(92): Magento\\Framework\\Reflection\\MethodsMap->getMethodsMap('\\\\Vendor\\\\...')\n#3 \/var\/www\/html\/vendor\/magento\/framework\/Webapi\/ServiceOutputProcessor.php(126): Magento\\Framework\\Reflection\\DataObjectProcessor->buildOutputDataArray(Object(Vendor\\Module\\Model\\ResourceModel\\Foo\\Collection\\Interceptor), '\\\\Vendor\\\\...')

Examples

It seems that when it calls ReflectionClass() it puts the [] brackets in, I think it's supposed to be stripped first.

Proposed solution

No response

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Ready for Confirmation

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions