10

anybody knows how to troubleshoot this?

enter image description here

3 Answers 3

3

When ever you are using Rest API in Magento 2. You have to follow these steps :

  1. Create an interface file Api/TestApiManagementInterface.php and define all the api methods you want.
  2. Create Model file Model/Api/TestApiManagement.php to implement all the methods defined in TestApiManagementInterface.php.
  3. Create a etc/webapi.xml and defines the routes and their permissions.
  4. Create etc/di.xml and add preferences for you interface and model.

While creating interface and model, we define parameters DocBlock annotation and return DocBlock annotations in both file and it should match in bothe the file. Example given below :

  1. Interface :
    interface LoginInterface {
  /**
     * Login email.
     *
     * @param string $email
     * @param string $password
     * @return string
     */
  public function login($email,$password);
}
  1. Model :
 class Login implements LoginInterface {

 /**
 * Returns greeting message to user
 * @author : Arman Khan
 *
 * @api
 * @param string $email Users email.
 * @param string $password Users password.
 * @return string Greeting message with users response.
 */   
 public function login($email,$password)  { 
    //..............
 }

If you are miss matching parameters data type in DocBlock annotation of either interface or model you can face this error "Class “array” does not exist"

OR

If you are passing wrong parameter's data type in postman or API call you will face this 'Class “array” does not exist issue.

So check parameter's data type every where and it should be same.

Check screenshot for the same error I faced as it entered wrong data type in interface annotation

Screenshot for same error

I got this error when i changed datatype of parameter from string to array in interface :

    interface LoginInterface {
  /**
     * Login email.
     *
     * @param string $email
     * @param array $password
     * @return string
     */
  public function login($email,$password);
}

I hope this will help you to figure out your issue.

20

If you've added proper docblock and still getting this error, replace @param array to @param string[] or @param mixed[] as according to magento devdocs.

Valid scalar types include: mixed (or anyType), bool (or boolean), str (or string), integer (or int), float, and double.

Also note that you must always use fully qualified class name or a fully qualified interface name in docblock.

Reference :

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/service-contracts/service-to-web-service.html

1
  • 2
    The tip here about using fully qualified interface names in the docblocks fixed the issue for me.
    – Tyler V.
    Commented Nov 12, 2021 at 5:20
0

For me, it worked after setting same types on getter and setter function as below,

/**
* Get Address
*
* @return string[]
*/
public function getAddress();

/**
* Set Address
*
* @param string|string[] $address
* @return $this
*/
public function setAddress($address);

Reference from getStreet() and setStreet() from this class

Magento\Quote\Api\Data\AddressInterface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.