Magento – “Class mixed does not exist” with only one array param in the body in Magento 2

apimagento2

I have a custom Magento 2 PUT request that looks like:

<route method="PUT" url="/V1/items">
    <service class="Vendor\Items\Api\ItemsManagementInterface" method="replaceItems" />
        <resources>
            <resource ref="self" />
    </resources>
 </route>

Class interface:

/**
 * Description
 *
 * @param mixed $items
 * @return string
 */
public function replaceItems($items);

The request works with this JSON body payload:

{
  "items": [
    "url1",
    "url2"
  ],
  randomParam: true
}

Instead, an error is trown if the payload is just:

{
  "items": [
    "url1",
    "url2"
  ]
}

The full error returned from Magento REST Api:

{
"message": "Class mixed does not exist",
"code": -1,
"trace": "#0 vendor/magento/framework/Reflection/MethodsMap.php(155): ReflectionClass->__construct('mixed')
#1 vendor/magento/framework/Reflection/MethodsMap.php(106): Magento\Framework\Reflection\MethodsMap->getMethodMapViaReflection('mixed')
#2 vendor/magento/module-webapi/Controller/Rest/ParamsOverrider.php(203): Magento\Framework\Reflection\MethodsMap->getMethodsMap('mixed')
#3 vendor/magento/module-webapi/Controller/Rest/ParamsOverrider.php(147): Magento\Webapi\Controller\Rest\ParamsOverrider->isPropertyDeclaredInDataObject('GV\\Products\\Api…', 'replaceItems…', 'items')
#4 vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php(93): Magento\Webapi\Controller\Rest\ParamsOverrider->overrideRequestBodyIdWithPathParam(Array, Array, 'Vendor\\Items\\Api…', 'replaceItems…')
#5 vendor/magento/module-webapi/Controller/Rest.php(322): Magento\Webapi\Controller\Rest\InputParamsResolver->resolve()
#6 vendor/magento/module-webapi/Controller/Rest.php(239): Magento\Webapi\Controller\Rest->processApiRequest()
#7 vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#8 vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)
#9 vendor/magento/framework/Interception/Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#10 generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(39): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#11 vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#12 generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#13 vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http\Interceptor->launch()
#14 index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#15 {main}"
}

I have already recompiled the DI and flushed the cache.

Also tried in the class interface: mixed[], string[], etc..


Goal: being able to make a request passing only a single array parameter, without the need to also pass a second param.

Best Answer

Class interface:

/**
 * Description
 *
 * @param string[] $items
 * @return array
 */
public function replaceItems($items);

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

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 []

@return array|int|string|bool|float Scalar or array of scalars 

Reference

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

Related Topic