0

I don't understand why the annotation do nothing on my GET REST API. I have the JMS Serializer in vendor with all the class.. but when i call my webservice, there are all my properties which appears.. Whereas i did an @ExlusionPolicy("all") and just @Expose on the ID property..

This is my Entity Product :

<?php

namespace GroupeGC\Bundle\ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Jms\Serializer\Annotation as JMS;


/**
 * Product
 *
 * @ORM\Table(name="gc_product")
 * @ORM\Entity
 * @JMS\ExclusionPolicy("all")
 *
 */
class Product
{
 /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @JMS\Type("integer")
 * @JMS\Expose
 */
private $id;


/**
 * @var string
 *
 * @ORM\Column(name="code", type="string", length=255, nullable=false)
 */
private $code;

/**
 * @var string
 *
 * @ORM\Column(name="label", type="string", length=255, nullable=true)
 *
 */
private $label;


/**
 * @var  float
 * @ORM\Column(name="volume", type="float")
 *
 */
private $volume;

 /**
 * @var float
 * @ORM\Column(name="weight", type="float")
 */
private $weight;
 /**
 * Get id
 *
 * @return integer
 */
public function getId()
{

    return $this->id;
}


 /**
* Set code
*
* @param string $code
* @return Product
*/
public function setCode($code)
{
    $this->code = $code;

    return $this;
}

/**
 * Get code
 *
 * @return string
 */
public function getCode()
{
    return $this->code;
}

/**
 * Set label
 *
 * @param string $label
 * @return Product
 */
public function setLabel($label)
{
    $this->label = $label;

    return $this;
}

/**
 * Get label
 *
 * @return string
 */
public function getLabel()
{
    return $this->label;
}


public function getVolume() {
    return $this->volume;
}

public function setVolume($volume) {
    $this->volume = $volume;
    return $this;
}


public function getWeight() {
    return $this->weight;
}

public function setWeight($weight) {
    $this->weight = $weight;
    return $this;
}

But we can see that , normaly, i just should have the id propertie which should appear in my JSON , whereas i have all propertie.. and i don't understand.

EDIT 1 : This is the fos_rest config in app/config :

fos_rest:
    view:
        failed_validation:    HTTP_BAD_REQUEST
        default_engine:       php
        formats:
            json:             true
            xml:              true
    format_listener:
        prefer_extension:     true
    body_listener:
        decoders:
            json:             fos_rest.decoder.json
            xml:              fos_rest.decoder.xml
    routing_loader:
        default_format:       json

fos_js_routing:
    routes_to_expose:         [oro_*]

i don't think there are a problem here ..

Thomas Trabelsi
  • 330
  • 1
  • 8
  • 21

1 Answers1

1

By default, the serializer will retrieve, or set the value via reflection. here. So every property in your entity will be retrieve/set.

/**
 * @JMS\ExclusionPolicy("all")
 * @JMS\AccessType("public_method")
 */

When using the AccessType annotation, you are telling the serializer to use the public methods (such as getX, setX, hasX) to retrieve/set the values.

AlixB
  • 1,208
  • 2
  • 10
  • 19
  • i had @JMS\AccessType("public_method") like you said but alway snothing. i alway have all properties... it's strange – Thomas Trabelsi Jul 16 '14 at 13:48
  • yess i always clear cache before testing. I know that the cache can be a real problem because we search everywhere but here ... it's even not this – Thomas Trabelsi Jul 16 '14 at 13:57
  • It is weird that even with the AccessType, you have this. Can you try to see if the getId is called? (by writing something like a var_dump() or an exit() inside it). – AlixB Jul 16 '14 at 14:00
  • i put a var_dump("test"); in my id getter and it's write well on the screen.. so it's called too .. – Thomas Trabelsi Jul 16 '14 at 14:02
  • I really don't know, :x. One solution could be to use @JMS\Groups to divide your properties into groups. – AlixB Jul 16 '14 at 14:09
  • yess i don'ty try with group again because without this it's not working too ^^ but i going to try – Thomas Trabelsi Jul 16 '14 at 14:11
  • Because this is not the right solution, I will delete it in 5 minutes – AlixB Jul 16 '14 at 14:29
  • but if i want to test with groups, how i call the group that i want. In my getAction of my ProductController return $this->handleGetListRequest($page, $limit); So where i say that the serializer should take the group name that i pet – Thomas Trabelsi Jul 16 '14 at 15:09
  • see the documentation of FOSRestBundle, there is something with serializerGroups={"group1", "group2"} – AlixB Jul 16 '14 at 15:11
  • yes i did it in my entity , i put @Groups({"manage"}) but there is a moment to say to take this name of group – Thomas Trabelsi Jul 16 '14 at 15:13
  • That is what I wrote before.. https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/annotations-reference.md#view + serializerGroups – AlixB Jul 16 '14 at 15:18
  • i added this annotation before my GET Controller : @View(serializerGroups={"manage"}) but it's the same ... i despair .. – Thomas Trabelsi Jul 16 '14 at 15:33
  • are you testing the right method/entity? Did you do the right configuration of FOSRest? – AlixB Jul 16 '14 at 15:38
  • i tested the right method. I think i have the riht configration because i clone from git OroCrm which comprises FOSRestBundle , JMSSerializerBundler and other bundle that i am not used.. – Thomas Trabelsi Jul 16 '14 at 15:46