magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.5k stars 9.3k forks source link

Retrieval of completed sales with rest API throws error #1578

Closed bigappleinsider closed 8 years ago

bigappleinsider commented 9 years ago

Do you have any insight into this issue? Is there something I'm missing?

http://magento.stackexchange.com/questions/75913/how-to-get-completed-sales-with-rest-api-in-magento-2

It works for processing status, however I get an error for completed or closed

curl -X GET -H "Authorization: Bearer ufkm720fkvqix2v0dhhs8xvpp3g4lmcc" -H "Cache-Control: no-cache" -H "Postman-Token: 366814d0-e058-7b89-c896-5670fb4fadb9" 'http://magento2.app/index.php/rest/V1/orders?criteria%5Bfilter_groups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bfield%5D=status&criteria%5Bfilter_groups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bvalue%5D=processing'

result: {"items":[]}

curl -X GET -H "Authorization: Bearer ufkm720fkvqix2v0dhhs8xvpp3g4lmcc" -H "Cache-Control: no-cache" -H "Postman-Token: 366814d0-e058-7b89-c896-5670fb4fadb9" 'http://magento2.app/index.php/rest/V1/orders?criteria%5Bfilter_groups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bfield%5D=status&criteria%5Bfilter_groups%5D%5B0%5D%5Bfilters%5D%5B0%5D%5Bvalue%5D=closed'
{"message":"Internal Error. Details are available in Magento log file. Report ID: webapi-55b7959277fcb"}

The log is as following:

[2015-07-28 14:45:38] main.CRITICAL: exception 'Exception' with message 'Report ID: webapi-55b7959277fcb; 
Message: Notice: Array to string conversion in /home/vagrant/laravel-root/magento2/lib/internal/Magento/Framework/Reflection/TypeCaster.php on line 34' in /home/vagrant/laravel-root/magento2/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php:194
Stack trace:
#0 /home/vagrant/laravel-root/magento2/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php(139): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(Exception))
#1 /home/vagrant/laravel-root/magento2/app/code/Magento/Webapi/Controller/Rest.php(191): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(Exception))
#2 /home/vagrant/laravel-root/magento2/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#3 /home/vagrant/laravel-root/magento2/lib/internal/Magento/Framework/App/Http.php(115): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#4 /home/vagrant/laravel-root/magento2/lib/internal/Magento/Framework/App/Bootstrap.php(257): Magento\Framework\App\Http->launch()
#5 /home/vagrant/laravel-root/magento2/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#6 {main} [] []

Magento 2 Rest API: http://devdocs.magento.com/guides/v2.0/rest/restApi/restCollection/Magento/Sales/Service/V1/OrderList/orderList.html

Token-based authentication: http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html

Thank you

vpelipenko commented 9 years ago

Internal ticket: MAGETWO-40824

mikhaill commented 9 years ago

+1 for this issue.

Additional detail to help you replicate the situation is that there must be results for the query to return. So for example if you ask for order status pending and there is 0 orders, the result come back correctly with an empty array. If you ask for status pending and there are 1+ orders, you get the error described in the SO post.

bigappleinsider commented 9 years ago

@vpelipenko @mikhaill

In TypeCaster.php method castValueToType($value, $type) throws the error because of incorrect arguments are passed in $type string, while $value is array. It's trying to cast array into a string. I traced it down to another calling method in lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php - buildOutputDataArray($dataObject, $dataObjectType)

Return type is being determined by $returnType = $this->methodsMapProcessor->getMethodReturnType($dataObjectType, $methodName);

Args that caused the issue: [dataObjectType] => \Magento\Sales\Api\Data\OrderItemInterface, [methodName] => getProductOptions

app/code/Magento/Sales/Api/Data/OrderItemInterface.php and app/code/Magento/Sales/Model/Order/Item.php

Return annotations for getProductOptions method should be @return \Magento\Sales\Api\Data\OrderItemInterface[]

Cache folder will need to be cleared for the fix to take affect rm -rfv var/cache/*

okorshenko commented 9 years ago

Internal ticket: MAGETWO-40951

adragus-inviqa commented 9 years ago

Dunno if something has changed, but getProductOptions returns something like this:

Array
(
    [info_buyRequest] => Array
        (
            [uenc] => aHR0cDovL3VrLmdyYXplLmRldi9wcm9kdWN0LTEuaHRtbA,,
            [product] => 1
            [selected_configurable_option] =>
            [related_product] =>
            [qty] => 1
        )

    [giftcard_lifetime] =>
    [giftcard_is_redeemable] => 0
    [giftcard_email_template] =>
    [giftcard_type] =>
)

And that method's phpdoc says it returns string[] (https://github.com/magento/magento2/blob/3e845470375b6903d79516276f3c1ffa00c9a8f3/app/code/Magento/Sales/Api/Data/OrderItemInterface.php#L859). Because of that, the object processor thinks that the returned array contains nothing but strings (https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php#L110) and it tries to convert every element in that array to string. But, as you can see, the value at key info_buyRequest cannot be just casted into a string, as that's an array.

The fix for me was to simply change the phpdoc of \Magento\Sales\Api\Data\OrderItemInterface::getProductOptions() to @return array, instead of @return string[].

So I'm not sure what you're saying, @bigappleinsider.

bigappleinsider commented 9 years ago

@adragus-inviqa awesome, can you push this fix?

NadiyaS commented 8 years ago

Hi @bigappleinsider , @mikhaill , this bug was fixed. Described API request returns correct list of orders with correct statuses. searchCriteria also works correctly. @adragus-inviqa , getProductOptions() now returns \Magento\Catalog\Api\Data\ProductOptionInterface and phpdocs says the same. This bug is closed as resolved. Thank you all for your contribution and we are sorry for delay.

srinivasanithin commented 7 years ago

@NadiyaS : Any ETA on this release? It is keep our Magento2 integration with ERP down.

myorkgitis commented 7 years ago

+1 Any update on when this will be released?