private function hydrateEntity(EntityInterface $entity, $group, $version)
    {
        $entity->setEntityGroup($group);
        $entity->setAPIVersion($version);

        // hydrate all the sub-fields recursively using JMS to extract them and identify their type
        $metadata = $this->metadataFactory->getMetadataForClass(\get_class($entity));
        /** @var PropertyMetadata $property */
        foreach ($metadata->propertyMetadata as $property) {
            if ('array' === $property->type['name']) {
                $array = $property->getValue($entity);
                if (\is_array($array) && \count($array) > 0) {
                    $first = \array_values($array)[0];

                    if ((\is_object($first) || \is_array($first))) {
                        $this->hydrateArray($array, $group, $version);
                    }
                }
            } elseif (($object = $property->getValue($entity)) instanceof EntityInterface) {
                $this->hydrateEntity($object, $group, $version);
            }
        }

        return $entity;
    }