Api-platform: Using groups, properties with underscore always treated as readonly

Created on 21 Nov 2017  路  4Comments  路  Source: api-platform/api-platform

Hi,

Maybe I don't know something but entities who has properties with underscore ( _ ) are treaded as readonly when groups are used:

/**
 * @ApiResource(attributes={
 *     "normalization_context"={"groups"={"read"}},
 *     "denormalization_context"={"groups"={"write"}}
 * })
 * @ORM\Entity
 * @ORM\Table(name="products")
 */
class Prouct
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Groups({"read"})
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     * @Assert\Length(min=3, max=255)
     * @Groups({"read", "write"})
     */
    protected $name;

    /**
     * @ORM\Column(type="datetime")
     * @Assert\DateTime()
     * @Groups({"read", "write"})
     */
    protected $date_created;

   // ..... other fields

Platform wold treat $date_created as readonly:
Read only

When changed to $dateCreated:

Writeable

Most helpful comment

Mhh this is linked to how the mutator method is spelled as well (ie a property private dateCreated is writable when setDateCreated exists). I think that if both properties are public they will both be writable.

I'm not sure how snake_cased mutators are spellend :p.

All 4 comments

Mhh this is linked to how the mutator method is spelled as well (ie a property private dateCreated is writable when setDateCreated exists). I think that if both properties are public they will both be writable.

I'm not sure how snake_cased mutators are spellend :p.

I think I have a similar case.
When using groups, any properties of an entity do not even show up in the GET response if I use underscore in their name.

This is likely caused by the same problem that I have filed an issue for in the core project.
https://github.com/api-platform/core/issues/1554

Thanks @soyuka for the tip, setting the propertiy public does the thing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

john-dufrene-dev picture john-dufrene-dev  路  3Comments

florent-andre picture florent-andre  路  3Comments

achref-riahi picture achref-riahi  路  3Comments

mikemilano picture mikemilano  路  3Comments

benbender picture benbender  路  4Comments