Replies: 1 comment 4 replies
-
I solved the issue by creating a custom service provider where I load my normalizer and denormalizer classes. I used $this->app->extend for that purpose. <?php
namespace App\Providers;
use ApiPlatform\GraphQl\Serializer\Exception\ErrorNormalizer as GraphQlErrorNormalizer;
use ApiPlatform\GraphQl\Serializer\Exception\HttpExceptionNormalizer as GraphQlHttpExceptionNormalizer;
use ApiPlatform\GraphQl\Serializer\Exception\RuntimeExceptionNormalizer as GraphQlRuntimeExceptionNormalizer;
use ApiPlatform\GraphQl\Serializer\Exception\ValidationExceptionNormalizer as GraphQlValidationExceptionNormalizer;
use ApiPlatform\GraphQl\Serializer\ItemNormalizer as GraphQlItemNormalizer;
use ApiPlatform\GraphQl\Serializer\ObjectNormalizer as GraphQlObjectNormalizer;
use ApiPlatform\GraphQl\Type\FieldsBuilderEnumInterface;
use ApiPlatform\Hal\Serializer\CollectionNormalizer as HalCollectionNormalizer;
use ApiPlatform\Hal\Serializer\EntrypointNormalizer as HalEntrypointNormalizer;
use ApiPlatform\Hal\Serializer\ItemNormalizer as HalItemNormalizer;
use ApiPlatform\Hal\Serializer\ObjectNormalizer as HalObjectNormalizer;
use ApiPlatform\Hydra\Serializer\DocumentationNormalizer as HydraDocumentationNormalizer;
use ApiPlatform\Hydra\Serializer\EntrypointNormalizer as HydraEntrypointNormalizer;
use ApiPlatform\Hydra\Serializer\PartialCollectionViewNormalizer as HydraPartialCollectionViewNormalizer;
use ApiPlatform\JsonApi\Serializer\CollectionNormalizer as JsonApiCollectionNormalizer;
use ApiPlatform\JsonApi\Serializer\EntrypointNormalizer as JsonApiEntrypointNormalizer;
use ApiPlatform\JsonApi\Serializer\ErrorNormalizer as JsonApiErrorNormalizer;
use ApiPlatform\JsonApi\Serializer\ItemNormalizer as JsonApiItemNormalizer;
use ApiPlatform\JsonApi\Serializer\ObjectNormalizer as JsonApiObjectNormalizer;
use ApiPlatform\JsonLd\Serializer\ItemNormalizer as JsonLdItemNormalizer;
use ApiPlatform\JsonLd\Serializer\ObjectNormalizer as JsonLdObjectNormalizer;
use ApiPlatform\OpenApi\Serializer\OpenApiNormalizer;
use ApiPlatform\Serializer\ItemNormalizer;
use ApiPlatform\Serializer\JsonEncoder;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class SerializerServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->extend(Serializer::class, function () {
$app = app();
/** @var ConfigRepository */
$config = $app['config'];
$list = new \SplPriorityQueue;
// Here's my stuff
$list->insert($app->make(\App\Serializer\UppercaseKeysNormalizer::class), -100);
$list->insert($app->make(\App\Serializer\UppercaseKeysDenormalizer::class), -100);
$list->insert($app->make(HydraEntrypointNormalizer::class), -800);
$list->insert($app->make(HydraPartialCollectionViewNormalizer::class), -800);
$list->insert($app->make(HalCollectionNormalizer::class), -800);
$list->insert($app->make(HalEntrypointNormalizer::class), -985);
$list->insert($app->make(HalObjectNormalizer::class), -995);
$list->insert($app->make(HalItemNormalizer::class), -890);
$list->insert($app->make(JsonLdItemNormalizer::class), -890);
$list->insert($app->make(JsonLdObjectNormalizer::class), -995);
$list->insert($app->make(ArrayDenormalizer::class), -990);
$list->insert($app->make(DateTimeZoneNormalizer::class), -915);
$list->insert($app->make(DateIntervalNormalizer::class), -915);
$list->insert($app->make(DateTimeNormalizer::class), -910);
$list->insert($app->make(ObjectNormalizer::class), -1000);
$list->insert($app->make(ItemNormalizer::class), -895);
$list->insert($app->make(OpenApiNormalizer::class), -780);
$list->insert($app->make(HydraDocumentationNormalizer::class), -790);
$list->insert($app->make(JsonApiEntrypointNormalizer::class), -800);
$list->insert($app->make(JsonApiCollectionNormalizer::class), -985);
$list->insert($app->make(JsonApiErrorNormalizer::class), -790);
$list->insert($app->make(JsonApiObjectNormalizer::class), -995);
if (interface_exists(FieldsBuilderEnumInterface::class)) {
$list->insert($app->make(GraphQlItemNormalizer::class), -890);
$list->insert($app->make(GraphQlObjectNormalizer::class), -995);
$list->insert($app->make(GraphQlErrorNormalizer::class), -790);
$list->insert($app->make(GraphQlValidationExceptionNormalizer::class), -780);
$list->insert($app->make(GraphQlHttpExceptionNormalizer::class), -780);
$list->insert($app->make(GraphQlRuntimeExceptionNormalizer::class), -780);
}
// TODO: unused + implement hal/jsonapi ?
// $list->insert($dataUriNormalizer, -920);
// $list->insert($unwrappingDenormalizer, 1000);
// $list->insert($jsonserializableNormalizer, -900);
// $list->insert($uuidDenormalizer, -895); //Todo ramsey uuid support ?
return new Serializer(
iterator_to_array($list),
[
new JsonEncoder('json'),
$app->make(JsonEncoder::class),
new JsonEncoder('jsonopenapi'),
new JsonEncoder('jsonapi'),
new JsonEncoder('jsonhal'),
new CsvEncoder,
]
);
});
}
} This is not optimal as I'm duplicating core logic in my application but I found this like an acceptable workaround. In a future, I found that it would be nice if we could inject our normalizers and denormalizers into the SplPriorityQueue via configuration. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
At the moment, I've been able to add my own providers and processors by tagging them in the register method of my application service provider:
But I haven't found a way, yet, to register a custom denormalizer. I tried tagging it with Symfony's DenormalizerInterface but id didn't work.
I tried reading the packaged service provider but I only got more confused.
Any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions