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.

1
  • have you tried the solution which is in answer ? Jun 23, 2018 at 17:32

2 Answers 2

1

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

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

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

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

Your Answer

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

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