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

The links are lost when the serialization groups are used #39

Closed
GuillaumeDoury opened this issue Apr 21, 2015 · 2 comments
Closed

The links are lost when the serialization groups are used #39

GuillaumeDoury opened this issue Apr 21, 2015 · 2 comments

Comments

@GuillaumeDoury
Copy link
Contributor

The links are lost when the serialization groups are used :

    /**
     * @FOSRest\View(serializerGroups={"default"})
     */
    public function cgetAction(){

        /** @var Product[] $products */
        $products = $this->get("api_product.product_service")->getAll();

        return $products;
    }

The links are lost due to the ExclusionManager which skip these links, the workaround which works in my projetc is to not skip these relations when exclusion is nullable

// \Hateoas\Serializer\ExclusionManager.php

    private function shouldSkip($object, Exclusion $exclusion = null, SerializationContext $context)
    {
        // Do not skip links when exclusion is null
        if (is_null($exclusion)) {
            return false;
        }

        if (null !== $exclusion
            && null !== $exclusion->getExcludeIf()
            && $this->expressionEvaluator->evaluate($exclusion->getExcludeIf(), $object)
        ) {
            return true;
        }

        if (!$context->getExclusionStrategy()) {
            return false;
        }

        $propertyMetadata = new RelationPropertyMetadata($exclusion);

        return $context->getExclusionStrategy()->shouldSkipProperty($propertyMetadata, $context);
    }

But I do not have enough global understanding to know if it's a good or a bad workaround. Do you have any idea how this problem can be resolved properly ?

@flo-sch
Copy link

flo-sch commented Apr 25, 2015

Just had the same problem with Symfony2 bundle,
But without using FosRESTBundle.

I just forgot to set up the exclusion property...

use Hateoas\Configuration\Annotation as Hateoas;

/**
 * @Hateoas\Relation(
 *      "get",
 *      href = @Hateoas\Route(
 *          "any_route_name",
 *          parameters = {
 *              "id" = "expr(object.getId())"
 *          }
 *      ),
 *      exclusion = @Hateoas\Exclusion(groups = {"list", "detail"})
 *  )
 */
class AnyEntity
{
}

Now, this relation will be serialized for the groups "list" and "detail".
(Not sure about this, but is it the behaviour we expect from an exclusion policy ?)

@GuillaumeDoury
Copy link
Contributor Author

Great, indeed, it makes sense, i missed this part, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants