Skip to content
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

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

Open
soyuka opened this issue Aug 29, 2024 Discussed in #5875 · 0 comments
Open

Comments

@soyuka
Copy link
Member

soyuka commented Aug 29, 2024

Discussed in #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant