1

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.

  • have you tried the solution which is in answer ? – Aditya Shah Jun 23 '18 at 17:32
0

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

  • Which one should I use for a single Array body input? I already tried string[] it does not work – Manuel Di Iorio Jun 25 '18 at 21:45
  • Are you sure your o/p will array ? – Aditya Shah Jun 26 '18 at 4:47
  • This is the request body, yes it is an array of strings: { "items": [ "url1", "url2" ] } – Manuel Di Iorio Jun 26 '18 at 8:15
  • Have you checked my updated answer ? – Aditya Shah Jun 27 '18 at 8:04
  • I am using this $response = ['success' => 'true']; return $resultJson->setData($response); but i got same error. – Pratik Mehta Jul 4 '18 at 11:50
0

In the API function docs DO NOT use any of the following.

@param mixed
@param mixed[]
@return mixed
@return mixed[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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