dingo / api

A RESTful API package for the Laravel and Lumen frameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Model Response structure change

vincent-lu opened this issue · comments

Q A
Bug? maybe
New Feature? no
Framework Laravel
Framework version 5.5.45
Package version 2.2.3
PHP version 7.2.18

Actual Behaviour

In Laravel 5.4 when returning a single Eloquent model as response from controller, the response is treated as EloquentModel by Dingo\Api\Http\Response::morph(); however after upgrading to Laravel 5.5 or 5.6, it is treated as an a type of array (array, ArrayObject or Arrayable).

Because the default Json Formatter's formatEloquentModel will put the resulting array inside the singular name of the table; while formatArray will not, this leads to difference in response structure after upgrading from Laravel 5.4 to 5.5.

Here is an example,

In Laravel 5.4, if returning User::find(1), the JSON result is something like:

{
  user: {
    id: 1,
    name: TESTUSER
  }
}

But in Laravel 5.5, the same response from the controller results in:

{
  id: 1,
  name: TESTUSER
}

This might be caused by the change in how Laravel return its originalContent. In Dingo\Api\Http\Response::morph(), while in Laravel 5.4 the $this->content = $this->getOriginalContent() call returns a model object if the controller response is such; but in Laravel 5.5 the same line returns an array.

Expected Behaviour

The JSON structure should remain the same after upgrading Laravel framework.

Steps to Reproduce

Will provide more details as needed.

commented

Hi

I have actually never seen what you describe, in fact there is a unit test in dingo which implies this is not the way it's meant to work.

Are you able to provide a simple test repository with laravel 5.4 and dingo only, with no other customisations to demonstrate this?

In any case, I think the way it does it now should be the correct way to do it.

Thanks for getting back to me @specialtactics. Since we have already updated all our code to accommodate this difference (whatever caused the change) let's close this issue.