Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Bug ] NelmioApiDoc: Serialization groups are ignored for collectionOperations #728

Closed
karser opened this issue Sep 5, 2016 · 12 comments
Closed
Assignees
Labels
Projects

Comments

@karser
Copy link
Contributor

karser commented Sep 5, 2016

Probably there is related closed issue but I can reproduce this using v2.0.0-beta.3 as well as master.
There is a bug somewhere in ApiPlatformParser that provides parsed attributes for NelmioApiDoc.
It should provide denormalization_context from collectionOperations (e.g "registration"), in fact, it returns global (e.g "write")

#src/UserBundle/Resources/config/api_resources.yml
user:
    class: UserBundle\Entity\User
    attributes:
        normalization_context: { groups: ['read'] }
        denormalization_context: { groups: ['write'] }
    collectionOperations:
        register:
            path: '/security/register'
            method: 'POST'
            normalization_context: { groups: ['read'] }
            denormalization_context: { groups: ['registration'] }
#src/UserBundle/Resources/config/serialization.yml
UserBundle\Entity\User:
    attributes:
        id:
            groups: ['login', 'read']
        accessToken:
            groups: ['login']
        username:
            groups: ['login', 'registration', 'read']
        email:
            groups: ['login', 'registration', 'read']
        plainPassword:
            groups: ['registration']
        roles:
            groups: ['login', 'user_read']
@soyuka
Copy link
Member

soyuka commented Sep 6, 2016

serialization.yml is not being read by api-platform, or I'm missing some informations.

@karser
Copy link
Contributor Author

karser commented Sep 6, 2016

@soyuka serialization.yml is Symfony serializer config and it used by ApiPlatform, see the docs

@soyuka
Copy link
Member

soyuka commented Sep 6, 2016

Indeed, my bad. They show up as they should in the SerializerPropertyMetadataFactory.

@dunglas dunglas added the bug label Sep 8, 2016
@dunglas
Copy link
Member

dunglas commented Sep 8, 2016

@karser can you test if #740 fix your problem (I think it does).

Thanks.

@karser
Copy link
Contributor Author

karser commented Sep 8, 2016

No it doesn't, but I think I realized the nature of its behavior.
I thought it uses UNION when it's filtering fields by common(global) and specific groups, but actually it uses INTERSECTION.
How is this supposed to work, @dunglas ?

@teohhanhui
Copy link
Contributor

teohhanhui commented Sep 8, 2016

@karser The normalization/denorrmalization context specified for a specific operation should be the only one in use, with fallback to the one declared at resource level. If you see a different behaviour, it's a bug.

Your assumption in the first comment is correct.

@teohhanhui
Copy link
Contributor

teohhanhui commented Sep 8, 2016

Referring to your example, when you do POST /security/register, the only writable fields should be:

  • username
  • email
  • plainPassword

and the returned fields are:

  • id
  • username
  • email

@Simperfit
Copy link
Contributor

I was looking into this,

@teohhanhui it does work properly when posting a ressource, correct me if i'm wrong but :

When using something dummy but similar

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Dummy
 * @ApiResource(attributes={
 *     "normalization_context"={"groups"={"dummy_out"}},
 *     "denormalization_context"={"groups"={"dummy_in"}}
 *     },
 *      collectionOperations={
 *          "get"={"method"="GET"},
 *          "post"={"method"="POST"},
 *          "customDummy"={
 *              "path"="/custom/dummy",
 *              "method"="POST",
 *              "normalization_context"={"groups"={"dummy_out"}},
 *              "denormalization_context"={"groups"={"custom_dummy"}}
 *     }
 *   }
 * )
 * @ORM\Table(name="dummy")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DummyRepository")
 */
class Dummy
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"dummy_out"})
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="string", type="string", length=255)
     * @Groups({"dummy_out", "custom_dummy"})
     */
    private $string;

    /**
     * @var int
     *
     * @ORM\Column(name="gerard", type="integer")
     * @Groups({"custom_dummy"})
     */
    private $gerard;

    /**
     * @var int
     *
     * @ORM\Column(name="test", type="integer", nullable=true)
     * @Groups({"dummy_in"})
     */
    private $test;

    /**
     * @return int
     */
    public function getTest()
    {
        return $this->test;
    }

    /**
     * @param int $test
     */
    public function setTest($test)
    {
        $this->test = $test;
    }
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set string
     *
     * @param string $string
     *
     * @return Dummy
     */
    public function setString($string)
    {
        $this->string = $string;

        return $this;
    }

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

    /**
     * Set gerard
     *
     * @param integer $gerard
     *
     * @return Dummy
     */
    public function setGerard($gerard)
    {
        $this->gerard = $gerard;

        return $this;
    }

    /**
     * Get gerard
     *
     * @return int
     */
    public function getGerard()
    {
        return $this->gerard;
    }
}

@karser correct me if i'm wrong but we are talking about the parameter return by NelmioApiDoc / Swagger Doc ?

@karser
Copy link
Contributor Author

karser commented Sep 12, 2016

@Simperfit right, this issue is related to docs parameters only.

@soyuka
Copy link
Member

soyuka commented Oct 20, 2016

@Simperfit is this fixed?

@Simperfit
Copy link
Contributor

@soyuka I don't think this is totally fixed. Because of nelmio I don't think we can go further than what i've done.

magarzon pushed a commit to magarzon/core that referenced this issue Feb 12, 2017
@Simperfit
Copy link
Contributor

I don't think we can do further with this

@Simperfit Simperfit moved this from Testing to Done in 2.1.0 Jun 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
2.1.0
Done
Development

No branches or pull requests

5 participants