Skip to content

Fix normalization with NormalizableInterface #231

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

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"php-http/discovery": true
}
},
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions src/SearchableEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand All @@ -22,8 +23,7 @@ final class SearchableEntity
/** @var ClassMetadata<object> */
private ClassMetadata $entityMetadata;

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

private bool $useSerializerGroups;

Expand All @@ -34,14 +34,13 @@ final class SearchableEntity
* SearchableEntity constructor.
*
* @param object $entity
* @param object|null $normalizer
* @param ClassMetadata<object> $entityMetadata
*/
public function __construct(
string $indexUid,
$entity,
ClassMetadata $entityMetadata,
$normalizer = null,
?NormalizerInterface $normalizer = null,
array $extra = []
) {
$this->indexUid = $indexUid;
Expand Down Expand Up @@ -71,7 +70,11 @@ public function getSearchableArray(): array
$context['groups'] = [Searchable::NORMALIZATION_GROUP];
}

if ($this->normalizer instanceof NormalizerInterface) {
if ($this->entity instanceof NormalizableInterface && null !== $this->normalizer) {
return $this->entity->normalize($this->normalizer, Searchable::NORMALIZATION_FORMAT, $context);
}

if (null !== $this->normalizer) {
return $this->normalizer->normalize($this->entity, Searchable::NORMALIZATION_FORMAT, $context);
}

Expand All @@ -86,7 +89,7 @@ private function setId(): void
throw new Exception('Entity has no primary key');
}

if (1 == \count($ids)) {
if (1 === \count($ids)) {
$this->id = reset($ids);
} else {
$objectID = '';
Expand Down
6 changes: 3 additions & 3 deletions src/Services/MeilisearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Class MeilisearchService.
*/
final class MeilisearchService implements SearchService
{
private SerializerInterface $normalizer;
private NormalizerInterface $normalizer;
private Engine $engine;
private Collection $configuration;
private PropertyAccessor $propertyAccessor;
Expand All @@ -33,7 +33,7 @@ final class MeilisearchService implements SearchService
private array $classToSerializerGroupMapping;
private array $indexIfMapping;

public function __construct(SerializerInterface $normalizer, Engine $engine, array $configuration)
public function __construct(NormalizerInterface $normalizer, Engine $engine, array $configuration)
{
$this->normalizer = $normalizer;
$this->engine = $engine;
Expand Down
68 changes: 68 additions & 0 deletions tests/Entity/SelfNormalizable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Tests\Entity;

use Doctrine\ORM\Mapping as ORM;
use Meilisearch\Bundle\Searchable;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @ORM\Entity
*/
class SelfNormalizable implements NormalizableInterface
{
/**
* @ORM\Id
*
* @ORM\Column(type="integer")
*/
private int $id;

/**
* @ORM\Column(type="string")
*/
private string $name;

/**
* @ORM\Column(type="datetime_immutable")
*/
private \DateTimeImmutable $createdAt;

public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
$this->createdAt = new \DateTimeImmutable();
}

public function getId(): int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}

public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array
{
if (Searchable::NORMALIZATION_FORMAT === $format) {
return [
'id' => $this->id,
'name' => 'this test is correct',
'self_normalized' => true,
];
}

return [];
}
}
42 changes: 42 additions & 0 deletions tests/Integration/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Meilisearch\Bundle\Tests\Integration;

use Meilisearch\Bundle\Tests\BaseKernelTestCase;
use Meilisearch\Bundle\Tests\Entity\SelfNormalizable;
use Meilisearch\Client;
use Meilisearch\Endpoints\Indexes;
use Meilisearch\Exceptions\ApiException;
Expand Down Expand Up @@ -85,6 +86,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Importing for index Meilisearch\Bundle\Tests\Entity\Link
Importing for index Meilisearch\Bundle\Tests\Entity\Page
Indexed 6 / 6 Meilisearch\Bundle\Tests\Entity\Page entities into sf_phpunit__pages index
Importing for index Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Importing for index Meilisearch\Bundle\Tests\Entity\Post
Indexed 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__posts index
Indexed 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__aggregated index
Expand All @@ -108,6 +110,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Cleared sf_phpunit__tags index of Meilisearch\Bundle\Tests\Entity\Tag
Cleared sf_phpunit__tags index of Meilisearch\Bundle\Tests\Entity\Link
Cleared sf_phpunit__pages index of Meilisearch\Bundle\Tests\Entity\Page
Cleared sf_phpunit__self_normalizable index of Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Done!

EOD, $clearOutput);
Expand All @@ -124,6 +127,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Deleted sf_phpunit__aggregated
Deleted sf_phpunit__tags
Deleted sf_phpunit__pages
Deleted sf_phpunit__self_normalizable
Done!

EOD, $clearOutput);
Expand Down Expand Up @@ -298,6 +302,7 @@ public function testSearchCreateWithoutIndices(): void
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Tag
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Link
Creating index sf_phpunit__pages for Meilisearch\Bundle\Tests\Entity\Page
Creating index sf_phpunit__self_normalizable for Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Post
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Tag
Done!
Expand Down Expand Up @@ -330,4 +335,41 @@ public function testCreateExecuteIndexCreation(): void

$this->assertEquals($this->client->getTasks()->getResults()[0]['type'], 'indexCreation');
}

public function testImportsSelfNormalizable(): void
{
for ($i = 1; $i <= 2; ++$i) {
$this->entityManager->persist(new SelfNormalizable($i, "Self normalizabie $i"));
}

$this->entityManager->flush();

$importCommand = $this->application->find('meili:import');
$importCommandTester = new CommandTester($importCommand);
$importCommandTester->execute(['--indices' => 'self_normalizable']);

$importOutput = $importCommandTester->getDisplay();

$this->assertSame(<<<'EOD'
Importing for index Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Indexed 2 / 2 Meilisearch\Bundle\Tests\Entity\SelfNormalizable entities into sf_phpunit__self_normalizable index
Done!

EOD, $importOutput);

self::assertSame([
[
'objectID' => 1,
'id' => 1,
'name' => 'this test is correct',
'self_normalized' => true,
],
[
'objectID' => 2,
'id' => 2,
'name' => 'this test is correct',
'self_normalized' => true,
],
], $this->client->index('sf_phpunit__self_normalizable')->getDocuments()->getResults());
}
}
2 changes: 2 additions & 0 deletions tests/config/meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ meilisearch:
- name: pages
class: 'Meilisearch\Bundle\Tests\Entity\Page'
enable_serializer_groups: true
- name: self_normalizable
class: 'Meilisearch\Bundle\Tests\Entity\SelfNormalizable'