Api-platform 2.7 - Unable to generate an IRI for the item of type

951 views Asked by At

I'm facing a problem using Api-platform:2.7. Everything works fine in my application with PHP Attributes and APi resources. However, I find out that when I composer require doctrine/mongodb-odm-bundle, all the POST resources become unusable. my application is using both an SQL and a Mongo database.

Removing the composer package fix the error.

This is the doctrine_mongodb.yaml I'm using (I tried with and without the auto_mapping. Same error)

doctrine_mongodb:
    auto_generate_proxy_classes: true
    auto_generate_hydrator_classes: true
    connections:
        default:
            server: '%env(resolve:MONGODB_URL)%'
            options: {}
    default_database: '%env(resolve:MONGODB_DB)%'
    document_managers:
        default:
            # auto_mapping: true
            mappings:
                App:
                    is_bundle: false
                    type: attribute
                    dir: '%kernel.project_dir%/src/Document'
                    prefix: 'App\Document'
                    alias: App

Here is the error stacktrace:

{
  "@context": "/contexts/Error",
  "@type": "hydra:Error",
  "hydra:title": "An error occurred",
  "hydra:description": "Unable to generate an IRI for the item of type \"App\\Entity\\TryingResource\"",
  "trace": [
    {
      "namespace": "",
      "short_class": "",
      "class": "",
      "type": "",
      "function": "",
      "file": "/srv/api/vendor/api-platform/core/src/Symfony/Routing/IriConverter.php",
      "line": 177,
      "args": []
    },
    {
      "namespace": "ApiPlatform\\Symfony\\Routing",
      "short_class": "IriConverter",
      "class": "ApiPlatform\\Symfony\\Routing\\IriConverter",
      "type": "->",
      "function": "getIriFromResource",
      "file": "/srv/api/vendor/api-platform/core/src/Symfony/EventListener/WriteListener.php",
      "line": 113,
      "args": [
        [
          "object",
          "App\\Entity\\TryingResource"
        ]
      ]
    },
    {
      "namespace": "ApiPlatform\\Symfony\\EventListener",
      "short_class": "WriteListener",
      "class": "ApiPlatform\\Symfony\\EventListener\\WriteListener",
      "type": "->",
      "function": "onKernelView",
      "file": "/srv/api/vendor/symfony/event-dispatcher/Debug/WrappedListener.php",
      "line": 117,
      "args": [
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Event\\ViewEvent"
        ],
        [
          "string",
          "kernel.view"
        ],
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\EventDispatcher\\Debug",
      "short_class": "WrappedListener",
      "class": "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener",
      "type": "->",
      "function": "__invoke",
      "file": "/srv/api/vendor/symfony/event-dispatcher/EventDispatcher.php",
      "line": 230,
      "args": [
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Event\\ViewEvent"
        ],
        [
          "string",
          "kernel.view"
        ],
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\EventDispatcher",
      "short_class": "EventDispatcher",
      "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
      "type": "->",
      "function": "callListeners",
      "file": "/srv/api/vendor/symfony/event-dispatcher/EventDispatcher.php",
      "line": 59,
      "args": [
        [
          "array",
          [
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ],
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ],
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ],
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ],
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ],
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ],
            [
              "object",
              "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
            ]
          ]
        ],
        [
          "string",
          "kernel.view"
        ],
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Event\\ViewEvent"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\EventDispatcher",
      "short_class": "EventDispatcher",
      "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
      "type": "->",
      "function": "dispatch",
      "file": "/srv/api/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php",
      "line": 154,
      "args": [
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Event\\ViewEvent"
        ],
        [
          "string",
          "kernel.view"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\EventDispatcher\\Debug",
      "short_class": "TraceableEventDispatcher",
      "class": "Symfony\\Component\\EventDispatcher\\Debug\\TraceableEventDispatcher",
      "type": "->",
      "function": "dispatch",
      "file": "/srv/api/vendor/symfony/http-kernel/HttpKernel.php",
      "line": 158,
      "args": [
        [
          "object",
          "Symfony\\Component\\HttpKernel\\Event\\ViewEvent"
        ],
        [
          "string",
          "kernel.view"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\HttpKernel",
      "short_class": "HttpKernel",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->",
      "function": "handleRaw",
      "file": "/srv/api/vendor/symfony/http-kernel/HttpKernel.php",
      "line": 75,
      "args": [
        [
          "object",
          "Symfony\\Component\\HttpFoundation\\Request"
        ],
        [
          "integer",
          1
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\HttpKernel",
      "short_class": "HttpKernel",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->",
      "function": "handle",
      "file": "/srv/api/vendor/symfony/http-kernel/Kernel.php",
      "line": 202,
      "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": "/srv/api/public/index.php",
      "line": 28,
      "args": [
        [
          "object",
          "Symfony\\Component\\HttpFoundation\\Request"
        ]
      ]
    }
  ]
}
2

There are 2 answers

0
Guillaume On BEST ANSWER

The problem came from a bug in api-platform and it was not able to work with both ORM and ODM.

It relates to this issue : https://github.com/api-platform/core/issues/5020

Fix was made in this PR : https://github.com/api-platform/core/pull/5032

0
Alex Javadi On

Note: this answer is just the completion of @Guaillaume's answer

If you check Issue #5020 by Oskmoz, you will see this was an issue that you can't use both ODM and ORM together.

this issue was solved on 30 Sep 2022 by soyuka in pull request #5032 and they merged with their source code.

Just use composer update. it should be work.

what was the problem ?

if you check the pull request's files, you will see they used PersistProcessor and RemoveProcessor directly, Soyuka Just changed it to something like this:

'api_platform.doctrine.orm.state.remove_processor';
'api_platform.doctrine_mongodb.odm.state.persist_processor';

Instead of :

RemoveProcessor::class;
PersistProcessor::class;