POST does not work with mongodb, Unable to generate an IRI
See original GitHub issueWhen I execute a POST call to my API, all I get is the error Unable to generate an IRI for the item of type "App\Document\plantcard"
I created my document following the api-platform MongoDB Support documentation, but even when I copied the code from the documentation the result was the same…
https://api-platform.com/docs/core/mongodb/
Error Message from log:
request.CRITICAL: Uncaught PHP Exception ApiPlatform\Core\Exception\InvalidArgumentException: "Unable to generate an IRI for the item of type "App\Document\plantcard"" at /var/www/symfony/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php line 131 {"exception":"[object] (ApiPlatform\\Core\\Exception\\InvalidArgumentException(code: 0): Unable to generate an IRI for the item of type \"App\\Document\\plantcard\" at /var/www/symfony/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:131, Symfony\\Component\\Routing\\Exception\\InvalidParameterException(code: 0): Parameter \"id\" for route \"api_plantcards_get_item\" must match \"[^/\\.]++\" (\"\" given) to generate a corresponding URL. at /var/www/symfony/vendor/symfony/routing/Generator/UrlGenerator.php:163)"} []
Here is what I implemented and the error I get. if you need further information just ask me
PHP version: 7.2 Symfony version: 4.2
api_platform.yaml
api_platform:
title: 'module-task'
# The description of the API.
description: 'module-task api'
# The version of the API.
version: '1.0.0'
mapping:
paths: ['%kernel.project_dir%/src/Entity', '%kernel.project_dir%/src/Document']
path_segment_name_generator: api_platform.path_segment_name_generator.dash
# The list of enabled formats. The first one will be the default.
formats:
json:
mime_types: ['application/json']
html:
mime_types: ['text/html']
doctrine_mongodb.yaml
doctrine_mongodb:
auto_generate_proxy_classes: '%kernel.debug%'
auto_generate_hydrator_classes: '%kernel.debug%'
connections:
default:
server: '%env(MONGODB_URL)%'
options:
username: '%env(MONGODB_USER)%'
password: '%env(MONGODB_PASSWORD)%'
default_database: '%env(MONGODB_DB)%'
document_managers:
default:
auto_mapping: true
logging: false # deactivate logging to prevent the logfile to get too big.
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Document'
prefix: App\Document\
alias: App
PHP Code
<?php
namespace App\Document;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* A plantcard
*
* @ApiResource
* @MongoDB\Document
*/
class plantcard
{
/**
* @MongoDB\Id(strategy="INCREMENT", type="integer")
* @var integer
*/
protected $id;
/**
* @MongoDB\Field(name="plantCardId", type="string")
* @var String
*/
protected $plantCardId;
/**
* @MongoDB\Field(name="plantName", type="string")
*
* @var String
*/
protected $plantName;
/**
* @return integer
*/
public function getId(): int
{
return $this->id;
}
/**
* @return String
*/
public function getPlantCardId(): String
{
return $this->plantCardId;
}
/**
* @param String $plantCardId
*/
public function setPlantCardId(String $plantCardId): void
{
$this->plantCardId = $plantCardId;
}
/**
* @return String
*/
public function getPlantName()
{
return $this->plantName;
}
/**
* @param String $plantName
*/
public function setPlantName($plantName)
{
$this->plantName = $plantName;
}
}
Error shown in browser/postman
{
"type": "https://tools.ietf.org/html/rfc2616#section-10",
"title": "An error occurred",
"detail": "Unable to generate an IRI for the item of type \"App\\Document\\plantcard\"",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/var/www/symfony/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php",
"line": 131,
"args": []
},
{
"namespace": "ApiPlatform\\Core\\Bridge\\Symfony\\Routing",
"short_class": "IriConverter",
"class": "ApiPlatform\\Core\\Bridge\\Symfony\\Routing\\IriConverter",
"type": "->",
"function": "getIriFromItem",
"file": "/var/www/symfony/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
"line": 107,
"args": [
[
"object",
"App\\Document\\plantcard"
]
]
},
{
"namespace": "ApiPlatform\\Core\\Serializer",
"short_class": "AbstractItemNormalizer",
"class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
"type": "->",
"function": "normalize",
"file": "/var/www/symfony/vendor/symfony/serializer/Serializer.php",
"line": 152,
"args": [
[
"object",
"App\\Document\\plantcard"
],
[
"string",
"json"
],
[
"array",
{
"collection_operation_name": [
"string",
"post"
],
"operation_type": [
"string",
"collection"
],
"resource_class": [
"string",
"App\\Document\\plantcard"
],
"request_uri": [
"string",
"/plantcards"
],
"uri": [
"string",
"http://localhost:7226/plantcards"
],
"resources": [
"object",
"ApiPlatform\\Core\\Serializer\\ResourceList"
],
"api_sub_level": [
"boolean",
true
],
"api_normalize": [
"boolean",
true
]
}
]
]
},
{
"namespace": "Symfony\\Component\\Serializer",
"short_class": "Serializer",
"class": "Symfony\\Component\\Serializer\\Serializer",
"type": "->",
"function": "normalize",
"file": "/var/www/symfony/vendor/symfony/serializer/Serializer.php",
"line": 125,
"args": [
[
"object",
"App\\Document\\plantcard"
],
[
"string",
"json"
],
[
"array",
{
"collection_operation_name": [
"string",
"post"
],
"operation_type": [
"string",
"collection"
],
"resource_class": [
"string",
"App\\Document\\plantcard"
],
"request_uri": [
"string",
"/plantcards"
],
"uri": [
"string",
"http://localhost:7226/plantcards"
],
"resources": [
"object",
"ApiPlatform\\Core\\Serializer\\ResourceList"
]
}
]
]
},
{
"namespace": "Symfony\\Component\\Serializer",
"short_class": "Serializer",
"class": "Symfony\\Component\\Serializer\\Serializer",
"type": "->",
"function": "serialize",
"file": "/var/www/symfony/vendor/api-platform/core/src/EventListener/SerializeListener.php",
"line": 68,
"args": [
[
"object",
"App\\Document\\plantcard"
],
[
"string",
"json"
],
[
"array",
{
"collection_operation_name": [
"string",
"post"
],
"operation_type": [
"string",
"collection"
],
"resource_class": [
"string",
"App\\Document\\plantcard"
],
"request_uri": [
"string",
"/plantcards"
],
"uri": [
"string",
"http://localhost:7226/plantcards"
],
"resources": [
"object",
"ApiPlatform\\Core\\Serializer\\ResourceList"
]
}
]
]
},
{
"namespace": "ApiPlatform\\Core\\EventListener",
"short_class": "SerializeListener",
"class": "ApiPlatform\\Core\\EventListener\\SerializeListener",
"type": "->",
"function": "onKernelView",
"file": "/var/www/symfony/vendor/symfony/event-dispatcher/EventDispatcher.php",
"line": 212,
"args": [
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent"
],
[
"string",
"kernel.view"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\EventDispatcher"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher",
"short_class": "EventDispatcher",
"class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
"type": "->",
"function": "doDispatch",
"file": "/var/www/symfony/vendor/symfony/event-dispatcher/EventDispatcher.php",
"line": 44,
"args": [
[
"array",
[
[
"array",
[
[
"object",
"ApiPlatform\\Core\\Validator\\EventListener\\ValidateListener"
],
[
"string",
"onKernelView"
]
]
],
[
"array",
[
[
"object",
"ApiPlatform\\Core\\EventListener\\WriteListener"
],
[
"string",
"onKernelView"
]
]
],
[
"array",
[
[
"object",
"ApiPlatform\\Core\\EventListener\\SerializeListener"
],
[
"string",
"onKernelView"
]
]
],
[
"array",
[
[
"object",
"ApiPlatform\\Core\\EventListener\\RespondListener"
],
[
"string",
"onKernelView"
]
]
]
]
],
[
"string",
"kernel.view"
],
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher",
"short_class": "EventDispatcher",
"class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
"type": "->",
"function": "dispatch",
"file": "/var/www/symfony/vendor/symfony/http-kernel/HttpKernel.php",
"line": 155,
"args": [
[
"string",
"kernel.view"
],
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent"
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "HttpKernel",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"type": "->",
"function": "handleRaw",
"file": "/var/www/symfony/vendor/symfony/http-kernel/HttpKernel.php",
"line": 67,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
],
[
"integer",
1
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "HttpKernel",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"type": "->",
"function": "handle",
"file": "/var/www/symfony/vendor/symfony/http-kernel/Kernel.php",
"line": 198,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
],
[
"integer",
1
],
[
"boolean",
true
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "Kernel",
"class": "Symfony\\Component\\HttpKernel\\Kernel",
"type": "->",
"function": "handle",
"file": "/var/www/symfony/public/index.php",
"line": 25,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
]
]
}
]
}
Edit: Using code markups now
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
You can use code
```
markup.Has anyone managed to find a solution to this problem? @teohhanhui @dimitriacseo @xammmue @Belssem