-
-
Notifications
You must be signed in to change notification settings - Fork 940
Closed
Labels
Description
API Platform version(s) affected: v3.0.8
Description
POST operation returns 'Unable to generate an IRI for the item of type "App\Document\TestResource"' using ODM.
In my project I am using: "doctrine/mongodb-odm-bundle": "^4.5.x-dev" which inside use "doctrine/mongodb-odm": "^2.3".
This link (https://www.doctrine-project.org/2021/12/04/mongodb-odm-2.3.html) says that from mongodb-odm:2.3 we can use PHP Attributes.
Soo I created a new API Platform 3 project with this mongodb support. I created "TestResource" like this:
<?php declare (strict_types=1);
namespace App\Document;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\ApiProperty;
use App\Repository\TestResourceRepository;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(
shortName: 'TestResource',
operations: [new Get(), new Put(), new Delete(), new GetCollection(), new Post()],
normalizationContext: ['groups' => ['test_resource:read']],
denormalizationContext: ['groups' => ['test_resource:write']]
)]
#[ODM\Document(repositoryClass: TestResourceRepository::class)]
class TestResource
{
#[ODM\Id(type: Type::STRING, strategy: "AUTO")]
#[ApiProperty(readable: false, writable: false, identifier: true)]
private ?string $id = null;
#[ODM\Field(type: Type::STRING)]
#[Groups(["test_resource:write", "test_resource:read"])]
#[Assert\NotBlank]
#[Assert\Length(min: 1, max: 128)]
#[Assert\Type(type: "string")]
#[Assert\Regex(pattern: '/^[0-9a-zA-Z ]+$/i')]
private string $name;
public function getId() : ?string
{
return $this->id;
}
public function setId(string $id) : self
{
$this->id = $id;
return $this;
}
public function getName() : string
{
return $this->name;
}
public function setName(string $name) : self
{
$this->name = $name;
return $this;
}
}and when I execute:
curl -X 'POST' \
'http://127.0.0.1:8130/api/test_resources' \
-H 'accept: application/ld+json' \
-H 'Content-Type: application/ld+json' \
-d '{
"name": "string"
}'
then I get response body like this:
{
"@context": "/api/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Unable to generate an IRI for the item of type \"App\\Document\\TestResource\"",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/var/www/html/vendor/api-platform/core/src/Symfony/Routing/IriConverter.php",
"line": 181,
"args": []
},
...Additional Context
When in the same project I will change PHP Attributes to PHP Annotations (only part related to Doctrine\MongoDB - #[ODM...]), then it works.