nilportugues / laravel5-jsonapi

Laravel 5 JSON API Transformer Package
http://nilportugues.com
MIT License
310 stars 70 forks source link

Problem with collections #84

Closed vanitom closed 8 years ago

vanitom commented 8 years ago

Hello,

I tested this package. Using the example, I detected an error with respect to JSONAPI specifications.

With the following query /employees/10, this is what I get:

{
  "data": {
    "type": "employee",
    "id": "10",
    "attributes": {
      "address": "343 Friary Road",
      "business_phone": "0118 9843212",
      "city": "Manchester",
      [...]
      "web_page": null,
      "zip_postal_code": "M3 3DL"
    },
    "links": {
      "self": {
        "href": "http://api.dev-forms/employees/10"
      },
      "employee_orders": {
        "href": "http://api.dev-forms/employees/10/orders"
      }
    },
    "relationships": {
      "latest_orders": {
        "data": [
          {
            "type": "order",
            "id": "82"
          }
        ]
      }
    }
  },
  "included": [
    {
      "type": "order",
      "id": "82",
      "attributes": {
        "employee_id": "10",
        "customer_id": null,
        [...]
        "status_id": "0"
      },
      "links": {
        "self": {
          "href": "http://api.dev-forms/orders/82"
        },
        "employee": {
          "href": "http://api.dev-forms/employees/10"
        }
      }
    }
  ],
  "links": {
    "employees": {
      "href": "http://api.dev-forms/employees"
    },
    "employee_orders": {
      "href": "http://api.dev-forms/employees/10/orders"
    },
    "self": {
      "href": "http://api.dev-forms/employees/10"
    }
  },
  "jsonapi": {
    "version": "1.0"
  }
}

It's perfect.

Here is what I get with the following query /employees:

{
  "data": [
    {
      "id": 10,
      "company": "Acme Industries",
      "last_name": "Smith",
      "first_name": "Mike",
      "email_address": "mike.smith@mail.com",
      "job_title": "Horticultarlist",
      "business_phone": "0118 9843212",
      "home_phone": null,
      "mobile_phone": null,
      "fax_number": null,
      "address": "343 Friary Road",
      "city": "Manchester",
      "state_province": "Lancs.",
      "zip_postal_code": "M3 3DL",
      "country_region": "United Kingdom",
      "web_page": null,
      "notes": null,
      "attachments": null,
      "full_name": "Mike Smith"
    }
  ],
  "included": [],
  "links": {
    "self": {
      "href": "http://api.dev-forms/employees?page[number]=1&page[size]=10"
    },
    "first": {
      "href": "http://api.dev-forms/employees?page[number]=1&page[size]=10"
    },
    "last": {
      "href": "http://api.dev-forms/employees?page[number]=1&page[size]=10"
    }
  },
  "meta": {
    "page": {
      "total": 1,
      "last": 1,
      "number": 1,
      "size": 10
    }
  },
  "jsonapi": {
    "version": "1.0"
  }
}

This does not match the specifications. We should get the same format as the previous one with the "type", the "attributes", ...

Is this normal?

Best regards

nilportugues commented 8 years ago

@vanitom hmmm mind checking if the model has been mapped? I think that may be the issue.

otherwise I need to investigate further.

Robindfuller commented 8 years ago

I have the same problem after following your instructions.

Seems it's ignoring the config file when loading a collection. I removed my model mapping from the jsonapi.php config file, '/employees/10' broke whereas '/employees' still loaded without th exception.

nilportugues commented 8 years ago

@Robindfuller thanks for the feedback, let me look into it.

Robindfuller commented 8 years ago

I just removed the overriding 'get' method from NilPortugues\Laravel5\JsonApi\Actions\ListResource and it works fine now.

vanitom commented 8 years ago

Thank you ! it works perfectly well now.