Wednesday, 19 December 2018

How to expose a property which depends on a serialization group from API-Platform to react-admin?

I use Changing the Serialization Context on a Per-item Basis in my application to apply the admin:read group when the user is an admin.

The normalizer has this configuration:

class UserAttributeNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface
{
    public function normalize($object, $format = null, array $context = [])
    {
        if ($this->userIsAdmin()) {
            $context['groups'][] = 'admin:read';
        }

        return $this->passOn($object, $format, $context);
    }

    private function userIsAdmin(): bool
    {
        return $this->authChecker->isGranted('ROLE_ADMIN');
    }
}

I want to show this property to the admin:

abstract class User implements UserInterface
{
    /**
     * @Groups({"admin:read"})
     */
    public function getUpdatedAt(): \DateTime
    {
        return $this->updatedAt;
    }
}

The data is returned successfully.

But the documentation generated by API-Platform on …/api/docs.jsonld does not expose this property: the property is not readable:

{
    "@type": "hydra:SupportedProperty",
    "hydra:property": {
        "@id": "#User/createdAt",
        "@type": "rdf:Property",
        "rdfs:label": "createdAt",
        "domain": "#User",
        "range": "xmls:dateTime"
    },
    "hydra:title": "createdAt",
    "hydra:required": false,
    "hydra:readable": false,
    "hydra:writable": false
},

I think that it prevents showing the value in the administration: the column is not shown in admin.

How can I add this property to the documentation and ultimately to react-admin?

I tried any configuration I could think of:

/**
 * @Groups({"admin:read"})
 * @ApiProperty(
 *     fetchable=true,
 *     readable=true,
 *     jsonldContext={"readable": "true"},
 *     swaggerContext={"readable": "true"}
 * )
 */
public function getUpdatedAt(): \DateTime
{
    return $this->updatedAt;
}

Update: I have the same problem when I try to expose a property instead of a method.



from How to expose a property which depends on a serialization group from API-Platform to react-admin?

No comments:

Post a Comment