core icon indicating copy to clipboard operation
core copied to clipboard

API Platform's normalizers should not be used when using Symfony's `serializer` service

Open soyuka opened this issue 1 year ago • 0 comments

Discussed in https://github.com/api-platform/core/discussions/5875

Originally posted by andersmateusz October 6, 2023 Hey, I have encountered following problem when normalizing data in my Symfony app where I use API Platform as bundle. I have 'File' entity registered as API resource. When invoking follwing code:

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\Client;
use App\Entity\Complaint;
use App\Entity\File;
use App\Repository\ClientRepository;
use App\Repository\FileRepository;
use App\Service\AzureBlobServiceAdapter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;

#[Route('/storage')]
final class StorageController extends AbstractController
{
    public const DEFAULT_CONTAINER_NAME = 'files';

    #[Route('/store', name: 'app_storage_store', methods: ['POST'])]
    public function store(Request $request, AzureBlobServiceAdapter $azureBlobService, SerializerInterface $serializer, ClientRepository $clientRepository): JsonResponse
    {
        $file = new File();
        $normalize = $serializer->normalize($file);

I get an error: 'Unable to generate an IRI for the item of type "App\Entity\File"'. In general it happens when I want to normalize API resource that is not persisted yet and does not have id issigned.
Now the questions are: How to disable IRI generation in normalization context (if possible)? How to disable linking API docs in response header for routes that are not API platform routes? Can I define in API Platform which are and which are not API routes (for example all routes that start with prefix '/api' should return docs and rest of them not)? Why API Platform interferes in Symfony serialization process?

Thank you for your response in advance.

soyuka avatar Aug 29 '24 21:08 soyuka