saintsystems / odata-client-php

OData Client Library for PHP
MIT License
141 stars 103 forks source link

ODataResponse assumes "value" key will be an array #154

Open n8rowley opened 4 months ago

n8rowley commented 4 months ago

I have an API end point that accepts a POST request and performs an action. The action is to convert a quote document to an order document. Once the action has completed, a 200 response is sent back with the following body:

{
    "value": "{{uniqueIDofCreatedOrder}}"
}

Odata Client throws and error because in the function getResponseAsObject on the ODataResponse class assumes that if the response has the key value the value will be an array.

public function getResponseAsObject($returnType)
    {
        $class = $returnType;
        $result = $this->getBody();

        //If more than one object is returned
        if (array_key_exists(Constants::ODATA_VALUE, $result)) {
            $objArray = array();
            foreach ($result[Constants::ODATA_VALUE] as $obj) {
                $objArray[] = new $class($obj);
            }
            return $objArray;
        } else {
            return [new $class($result)];
        }
    }

It gives a the unique string to the foreach loop and the loop fails. Note: Constants::ODATA_VALUE is set to 'value'.

I've tried to look for the authoritative specification that defines OData responses but I can't tell what is the official documentation. Does anyone know where to find that and whether it says anything about the possible data types for certain keys in OData responses? If value should be guaranteed to be an array, I'll reach out to the creator of the API. Otherwise, I'll work on a PR to make this function more flexible.