ApiPlatform Serialization Groups don’t include all properties with the same serialization group

I’m using Symfony5 and ApiPlatform

I have an entity User that have an Address (entity), so when I POST a User I want in the same time to POST an Address entity.

So I added user:write group to $address field, and to every other field I want to POST in Address entity.

User.php :

/**
 * @ApiResource(
 *     attributes={
 *          "normalization_context"={"groups"={"user:read", "user:list"}},
 *          "denormalization_context"={"groups"={"user:write"}}
 *     },
 *    collectionOperations={
 *        "get"={
 *              "method"="GET",
 *              "security"="is_granted('ROLE_ADMIN')",
 *              "normalization_context"={"groups"={"user:list"}}
 *          },
 *        "post"={
 *              "method"="POST",
 *              "security_post_denormalize"="is_granted('POST', object)",
 *              "validation_groups"={"Default", "create"},
 *              "normalization_context"={"groups"={"user:write"}},
 *        }
 *    },
 *    itemOperations={
 *        "get"={
 *          "method"="GET",
 *          "security"="is_granted('ROLE_ADMIN') or object == user",
 *          "normalization_context"={"groups"={"user:read"}}
 *        }
 *    }
 * )
 * @ORMEntity(repositoryClass=UserRepository::class)
 */
class User
{

    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @Groups({"user:read", "user:list"})
     */
    protected $id;

    /**
     * @ORMColumn(type="string", length=180, unique=true)
     * @Groups({"specialist:write", "user:read", "user:list", "user:write", "specialist:list", "specialist:read", "transaction:list"})
     */
    protected $email;

    /**
     * @ORMOneToOne(targetEntity=Address::class, cascade={"persist", "remove"})
     * @Groups({"specialist:read", "client:read", "user:write", "user:list", "user:read"})
     */
    private $address;
}

Address.php :


/**
 * @ApiResource(
 *     attributes={
 *          "normalization_context"={"groups"={"addr:read", "addr:list"}},
 *          "denormalization_context"={"groups"={"addr:write"}}
 *     },
 *     collectionOperations={
 *          "get"={
 *              "method"="GET",
 *              "security"="is_granted('ROLE_ADMIN')",
 *              "normalization_context"={"groups"={"addr:list"}}
 *          },
 *          "post"={
 *              "method"="POST",
 *              "security_post_denormalize"="is_granted('POST', object)",
 *              "validation_groups"={"Default", "create"},
 *              "normalization_context"={"groups"={"addr:write"}}
 *          },
 *     },
 *     itemOperations={
 *          "get"={
 *              "method"="GET",
 *              "security"="is_granted('GET', object)",
 *              "normalization_context"={"groups"={"addr:read"}}
 *          }
 *     }
 * )
 * @ORMHasLifecycleCallbacks()
 */
class Address
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @Groups({"addr:read", "addr:list", "client:read", "client:list", "specialist:read", "user:read", "user:list"})
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     * @Groups({"addr:read", "addr:write", "addr:list", "user:write", "user:read", "user:list", "client:read", "client:list", "specialist:read", "specialist:list"})
     * @AssertNotBlank(
     *     groups={"create", "putValidation"},
     *     message="address.address.not_blank"
     * )
     * @AssertLength(
     *     groups={"create", "putValidation"},
     *     min = "5",
     *     max = 255,
     *     maxMessage = "address.address.length",
     *     allowEmptyString = false
     * )
     */
    private $address;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @Groups({"addr.read", "addr:write", "addr:list", "user:write", "user:read", "user:list", "client:read", "client:list", "specialist:read"})
     */
    private $additionalAddress;
}

Now on the swagger documentation, here is what I got :

{
  "email": "string",
  "roles": [
    "string"
  ],
  "password": "string",
  "firstName": "string",
  "lastName": "string",
  "title": "string",
  "phone": "string",
  "mobile": "string",
  "birthdate": "2020-11-25T13:48:04.854Z"
}

Whithout having the possiblity to POST an address.

Does anyone know why the group user:write is not taken on $address property and Address entity ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *