Core: [Question] Add Hydra elements

Created on 17 Aug 2016  路  12Comments  路  Source: api-platform/core

Hi,

I would like to add a new hydra entry (hydra:facet) on my api result. Do you have an example to implement this ?

Example:

{
  "@context": "/contexts/Book",
  "@id": "/books",
  "@type": "hydra:Collection",
  "hydra:facet": [
      {
          "Facets on ma collection"
      }
  ],
  "hydra:member": [
    {
      "@id": "/books/1",
      "@type": "http://schema.org/Book",
      "name": "My awesome book"
    },
    ...
  ],
  "hydra:totalItems": 50,
  ...
}

Thank's

Bertrand

question

All 12 comments

The hydra: prefix represents the vocabulary (in this case, "hydra": "http://www.w3.org/ns/hydra/core#"). I don't see any definition of hydra:facet in the spec?

Anyway, you can anything you need to the output by decorating the api_platform.jsonld.normalizer.item service (assuming you're using JSON-LD).

It would be nice to have a doc explaining in depth how to do that, as this is a custom use case.

@teohhanhui I search a solution to make all facets information in my API (create with API-Platform). I have an customer API return 脿 json with records, facets and many more in one request (Facet informations is not on item), it's a facets collection. Where's possible to make that in my API ?

Just add a facet property without the hydra: prefix. It's allowed by JSON-LD and Hydra specs to have custom properties. They will simply be ignored by a parser that don't know them.

@dunglas Thank's for this precision. I will create a custom parser to generate this part. What's the best solution: Event or Collection parser replacement ?

I would opt for a normalizer/denormalizer decorator.

@Garfield-fr did you achieved what you wanted to do ?

Yes. Thank's

@Garfield-fr can you post how did you do it?

Hi @mcanepa ,

This is my example with version 2.0 of api-platform for added total value on result:

<?php

namespace ApiBundle\Hydra\Serializer;

use ApiBundle\Collection\CollectionInterface;
use ApiBundle\Collection\FineCollection;
use ApiPlatform\Core\JsonLd\Serializer\JsonLdContextTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class FineCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
    use JsonLdContextTrait;

    /**
     * @var NormalizerInterface
     */
    private $normalizer;

    /**
     * @param NormalizerInterface $normalizer
     */
    public function __construct(NormalizerInterface $normalizer)
    {
        $this->normalizer = $normalizer;
    }

    /**
     * {@inheritdoc}
     */
    public function setNormalizer(NormalizerInterface $normalizer)
    {
        if ($this->normalizer instanceof NormalizerAwareInterface) {
            $this->normalizer->setNormalizer($normalizer);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function normalize($object, $format = null, array $context = array())
    {
        $data = $this->normalizer->normalize($object, $format, $context);
        if (isset($context['api_sub_level'])) {
            return $data;
        }

        if ($object instanceof CollectionInterface) {
            $fineCollection = $object->getCollection();
            if ($fineCollection instanceof FineCollection) {
                $data['total'] = $fineCollection->getTotal();
            }
        }

        return $data;
    }

    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, $format = null)
    {
        return $this->normalizer->supportsNormalization($data, $format);
    }
}

Service definition:

services:
    fine_collection.normalizer:
        class:  ApiBundle\Hydra\Serializer\FineCollectionNormalizer
        decorates:  api_platform.hydra.normalizer.collection
        public: false
        arguments: ["@fine_collection.normalizer.inner"]

and official documentation here:
https://api-platform.com/docs/core/serialization#decorating-a-serializer-and-add-extra-data

thanks!

thanks from 2020 !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DenisVorobyov picture DenisVorobyov  路  3Comments

rockyweng picture rockyweng  路  3Comments

gustawdaniel picture gustawdaniel  路  3Comments

mahmoodbazdar picture mahmoodbazdar  路  3Comments

kate-kate picture kate-kate  路  3Comments