Skip to content

Commit

Permalink
[OpenApi] ObjectReference support mongo db
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jan 16, 2024
1 parent fba9a9d commit 31c26d7
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/open-api/Serializer/Handler/ObjectReferenceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Component\OpenApi\Serializer\Handler;

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use JMS\Serializer\Context;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\GraphNavigatorInterface;
Expand Down Expand Up @@ -30,8 +31,10 @@ public static function getSubscribingMethods(): array
];
}

public function __construct(private ManagerRegistry $managerRegistry)
{
public function __construct(
private ?ManagerRegistry $ormManagerRegistry,
private ?ManagerRegistry $odmManagerRegistry
) {
}

public function serializeObjectReference(
Expand All @@ -45,8 +48,7 @@ public function serializeObjectReference(
}

$class = $type['params'][0]['name'];
$identifiers = $this->managerRegistry
->getManagerForClass($class)
$identifiers = $this->getManagerForClass($class)
->getMetadataFactory()
->getMetadataFor($class)
->getIdentifierValues($value);
Expand All @@ -64,9 +66,14 @@ public function deserializeObjectReference(
return null;
}

$repository = $this->managerRegistry
->getRepository($type['params'][0]['name']);
return $this->getManagerForClass($type['params'][0]['name'])
->find($type['params'][0]['name'], $value);
}

return $repository->find($value);
private function getManagerForClass(string $class): ObjectManager
{
return $this->ormManagerRegistry?->getManagerForClass($class)
?? $this->odmManagerRegistry?->getManagerForClass($class)
?? throw new \RuntimeException('No object manager found for class ' . $class);
}
}

0 comments on commit 31c26d7

Please sign in to comment.