core icon indicating copy to clipboard operation
core copied to clipboard

Custom uri variable for DTO doesn't work

Open mikitafreeman opened this issue 1 year ago • 5 comments

API Platform version(s) affected: 3.3.11

Description

For 3.2.21 version it worked, what needs to be done to make it work for 3.3.11?

#[Get(
    uriTemplate: '/notifications/unread_count/{type}',
    uriVariables: [
        'type' => 'system|non_system|all',
    ],
    output: UnreadNotificationsCount::class,
    provider: UnreadNotificationsCountProvider::class,
)]
final readonly class UnreadNotificationsCount
{
    public function __construct(
        public int $count,
    ) {
    }
}

After update on v3.3.11 I get an error

  "detail": "Unable to generate an IRI for the item of type \"App\\DTO\\Notification\\UnreadNotificationsCount\"",
  "trace": [
    {
      "namespace": "",
      "short_class": "",
      "class": "",
      "type": "",
      "function": "",
      "file": "/srv/app/vendor/api-platform/core/src/Symfony/Routing/IriConverter.php",
      "line": 194,
      "args": []
    },

mikitafreeman avatar Aug 20 '24 17:08 mikitafreeman

image

The root of the issue is that we get into the getIriFromResource method twice and the second time the context is an empty array

Due this commit https://github.com/api-platform/core/commit/cc9f6a518222598d20556fc1ec62b7c4be52bf52#diff-60bc22662b6ae1820b895aa95e88e86cb6e9bc227f03bb00d5589d494886eca3R73

mikitafreeman avatar Aug 21 '24 08:08 mikitafreeman

@soyuka Hello, why don't we pass $context further here

https://github.com/api-platform/core/blob/v3.3.11/src/State/Processor/RespondProcessor.php#L115

mikitafreeman avatar Aug 21 '24 10:08 mikitafreeman

It's because it could not find identifiers? Context is reset for embed resources it's expected

soyuka avatar Aug 21 '24 17:08 soyuka

The identifier all in my case was found, if it had not been found we would have received an InvalidArgumentException here

https://github.com/api-platform/core/blob/v3.3.11/src/Symfony/Routing/IriConverter.php#L194

The root of the issue is that we get into the getIriFromResource method twice and the second time the context is an empty array after the reset occurred as you said for embed resources.

mikitafreeman avatar Aug 21 '24 17:08 mikitafreeman

The identifiers extractor is supposed to find the uri variables from the $originalData at https://github.com/api-platform/core/blob/v3.3.11/src/State/Processor/RespondProcessor.php#L115

soyuka avatar Aug 29 '24 20:08 soyuka

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 29 '24 17:10 stale[bot]