question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

POST does not work with mongodb, Unable to generate an IRI

See original GitHub issue

When 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:open
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
hubbyistcommented, Mar 7, 2019

You can use code ``` markup.

```
Your code here
```
0reactions
avandrevitorcommented, Mar 4, 2022

Has anyone managed to find a solution to this problem? @teohhanhui @dimitriacseo @xammmue @Belssem

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to generate an IRI when using mongoDb
I'm new to the API Platform framework, and for the project I'm working on we're required to use mongoDb. I followed the instructions...
Read more >
I suddenly became unable to connect to mongodb with shell ...
Hello everyone, I have been using mongoDb for quite a while. And only a few days ago I was able to dump mongodb....
Read more >
I can post to my database, but my get requests say ... - MongoDB
It's probably an error in your GET function. No reason to have a different behaviour between MongoDB localhost & MongoDB in Atlas unless...
Read more >
State Processors - API Platform
State Processors. To mutate the application states during POST , PUT , PATCH or DELETE operations, API Platform uses classes called state processors....
Read more >
Detecting Incremental Database Changes (Oracle ... - IRI CoSort
This command block truncates the file current.txt. This is to clear out the records that were loaded into MongoDB. If the import failed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found