nilportugues / symfony-jsonapi

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

Choosing which properties to serialize? #8

Closed joshuataylor closed 8 years ago

joshuataylor commented 8 years ago

With the symfony serializer (or JMS), you can either use groups or config to dictate which properties should be serialized.

Can we do the same thing here?

nilportugues commented 8 years ago

Yes. See the example of the documentation:

# app/config/serializer/acme_domain_dummy_post.yml

mapping:
  class: Acme\Domain\Dummy\Post
  alias: Message
  aliased_properties:
    author: author
    title: headline
    content: body
  hide_properties: []
  id_properties:
    - postId
  urls:
    self: get_post ## @Route name
    comments: get_post_comments ## @Route name
  relationships:
    author:
      related: get_post_author ## @Route name
      self: get_post_author_relationship  ## @Route name
joshuataylor commented 8 years ago

Are hidden properties still processed by the serializer, just hidden from the final result?

It seems that when I pass through a simple entity (id, createdAt, updatedAt, title) it works fine.

But when I do something a little more complicated (ie: Post, which has Comments, which has something, which has something), even with the yml files configured for all the fields it refuses to work and returns a 502.

nilportugues commented 8 years ago

This might be the cyclical reference Doctrine entities have. Hidden properties are not transformed but are initially read at the time being.

I'm working on this. I know I can fix it, but it's hard because of the Doctrine Proxies. Even the people at Doctrine are working on fixing this as it's a major concern for many. Symfony 2 managed to work around this with their serialized, so I must investigate and apply the fix.

Meanwhile you should use the main approach, which is mapping data to a new object and serialize that, as other Symfony packages are doing. I'm sorry =\

joshuataylor commented 8 years ago

All good, this is an amazing library :).

nilportugues commented 8 years ago

I'm still hoping to add more features which will make your life even easier :+1:

hectorh30 commented 8 years ago

Hi there, sorry for commenting on a closed issue.

Could this be the reason I'm getting Error: Maximum function nesting level of '1000' reached, aborting! when serializing a Doctrine entity? As far as I could dig it had something to do with serializing a doctrine's PersistenCollection, but it looks complex.

Could it be possible to integrate the library with Symfony's Serializer component?

Finally, do you have any links to packages using thea approach of a new object? I could use an example.

Thanks in advance, and keep up the good work! :smile:

nilportugues commented 8 years ago

Ohh Doctrine...

On the mean time, you've have to create an Adapter/Transformer and build an object that does not share the problems of Doctrine cyclic class dependencies...

I'm working on a solution for this

hectorh30 commented 8 years ago

Got it, thanks!