nilportugues / symfony-jsonapi

JSON API Transformer Bundle for Symfony 2 and Symfony 3
http://nilportugues.com
MIT License
115 stars 19 forks source link

DateTime object from doctrine goes to relationships on included member #21

Closed franbenz closed 8 years ago

franbenz commented 8 years ago

When a DateTime object belongs to a top level entity it shows up as this

data: [
{
  type: "entidad1",
  id: "1",
  attributes: {
    fecha: {
      date: "2016-06-09 00:00:00",
      timezone_type: 3,
      timezone: "America/Asuncion"
    },...

But when the DateTime object is an attribute from an included entity it goes to the relationships member as this:

...
included: [
  {
    type: "entidad1",
    id: "1",
    attributes: {
      descripcion: "prueba"
    },
    relationships: {
      fecha: {
        data: [ ]
      },
    ...

Is this normal behaviour? Should I enable something for DateTime serializers to work?

nilportugues commented 8 years ago

@franbenz should not be empty at all. I'll try to reproduce the error.

Maybe you can provide a test with this bug as a PR to speed things a bit.

franbenz commented 8 years ago

Thanks for the fix. After this commit the DateTime attribute does not appear in the relationships but neither it appears in the attributes member. Why should some attributes appear and others should not?

nilportugues commented 8 years ago

@franbenz thanks for pointing this out. Looking into it

nilportugues commented 8 years ago

@franbenz this made me build an additional piece of code in the api-transformer library that will solve this problem and many others :+1:

This: https://github.com/nilportugues/php-json-api/blob/582c05ad34195f24ca6939b66ca15737d7c56519/src/JsonApiTransformer.php#L64

franbenz commented 8 years ago

Thanks, it's working. The DateTime is where it belongs now.