Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
n8rowley opened this issue May 2, 2024 · 0 comments
Open

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

n8rowley opened this issue May 2, 2024 · 0 comments

Comments

@n8rowley
Copy link
Contributor

n8rowley commented May 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant