Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding links fails due to missing request attribute #5886

@codedge

Description

@codedge

API Platform version(s) affected: 3.2

Description

Inside the newly added AddLinkHeaderProcessor the getLinks() call fails, if no request attribute with name _api_platform_links exists.

Error:

[Error] Call to a member function getLinks() on null

It does not even reach the custom processor.

How to reproduce

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata as Api;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;

#[
    ORM\Entity,
    ORM\Table(name: 'my_entity'),
    Api\ApiResource(
        operations: [
            new Api\Patch(
                denormalizationContext: [
                    'groups' => ['rest:update'],
                ],
                processor: MyCustomProcessor::class
            ),
        ],
        mercure: false
    )
]
class MyRequest
 #[
        ORM\Id,
        ORM\GeneratedValue(strategy: 'CUSTOM'),
        ORM\CustomIdGenerator(class: GuidGenerator::class),
        ORM\Column(type: 'guid'),
        Assert\Uuid,
    ]
    private string $id;

  #[
        ORM\Column(type: 'string'),
        Assert\Type(type: 'string'),
        Serializer\Groups(['rest:update'])
    ]
    private string $someId = '';
}

Run a plain PATCH request with the property someId set to some string. The request runs through these files

/var/www/html/vendor/api-platform/core/src/State/Processor/AddLinkHeaderProcessor.php:43
/var/www/html/vendor/api-platform/core/src/HttpCache/State/AddHeadersProcessor.php:32
/var/www/html/vendor/api-platform/core/src/Symfony/State/MercureLinkProcessor.php:32
/var/www/html/vendor/api-platform/core/src/State/Processor/SerializeProcessor.php:75
/var/www/html/vendor/api-platform/core/src/State/Processor/WriteProcessor.php:42
/var/www/html/vendor/api-platform/core/src/Symfony/Controller/MainController.php:100

... and fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions