Skip to content

feat(metadata): get URI variables from an IRI without loading the item - #8841

Open
ViPErCZ wants to merge 3 commits into
api-platform:mainfrom
ViPErCZ:feat/iri-converter-uri-variables-from-iri
Open

ViPErCZ wants to merge 3 commits into
api-platform:mainfrom
ViPErCZ:feat/iri-converter-uri-variables-from-iri

Conversation

@ViPErCZ

@ViPErCZ ViPErCZ commented Nov 12, 2026

Copy link
Copy Markdown
Contributor
Q A
Branch? main
Tickets -
License MIT
Doc PR api-platform/docs#...

Adds UriVariablesFromIriInterface::getUriVariablesFromIri(), which matches an IRI against the router and returns the URI variables of the item it references, without calling the state
provider. It lives in a separate interface, so IriConverterInterface is untouched and existing implementations and decorators keep working.

Functional test (tests/Functional/UriVariablesFromIriTest.php), the resource's provider throws, so getting a result proves the item is not loaded:

public function testGetUriVariablesFromIriWithoutCallingTheProvider(): void
{
    $this->assertSame(['id' => 1], $this->getIriConverter()->getUriVariablesFromIri('/uri_variables_from_iri_resources/1'));
}

public function testGetUriVariablesFromCollectionIri(): void
{
    $this->expectException(InvalidArgumentException::class);
    $this->expectExceptionMessage('The iri "/uri_variables_from_iri_resources" references a collection not an item.');

    $this->getIriConverter()->getUriVariablesFromIri('/uri_variables_from_iri_resources');
}

public function testGetUriVariablesFromUnknownIri(): void
{
    $this->expectException(InvalidArgumentException::class);
    $this->expectExceptionMessage('No route matches "/does_not_exist/1".');

    $this->getIriConverter()->getUriVariablesFromIri('/does_not_exist/1');
}

Unit tests cover the Symfony, Laravel and MCP converters (tests/Symfony/Routing/IriConverterTest.php, src/Laravel/Tests/Unit/Routing/IriConverterTest.php,
src/Mcp/Tests/Routing/IriConverterTest.php).

@soyuka soyuka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice thanks this is a good idea!

@soyuka soyuka added this to the 5.1 milestone Nov 24, 2026
@ViPErCZ

ViPErCZ commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. I’ve added a test and a changelog. And I’ve merged main.

@soyuka soyuka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took another look before 5.1, a few things to fix:

  • please rebase on main (no merge commit), conflicts are only the use block in ApiPlatformProvider and the new tests in IriConverterTest
  • Symfony, Laravel and Mcp still require api-platform/metadata: ^5.0 but their converters now implement UriVariablesFromIriInterface which only exists in 5.1, the Mcp lowest job already fails with "Interface not found", bump to ^5.1
  • the interface doc says the result can be passed back as uri_variables to getIriFromResource(), that's not true with composite identifiers: Symfony returns ['first' => '1', 'second' => '2'] and the route expects {id} so generating the IRI throws. Either fix the doc or rebuild the composite identifier
  • Laravel and Symfony don't behave the same behind one interface: Laravel ignores $context, returns raw strings and doesn't split composite identifiers, Symfony converts values (/books/1 gives ['id' => 1] vs ['id' => '1']). They should return the same thing, no?
  • in the Mcp decorator, an inner converter without the interface throws InvalidArgumentException, which is the "bad IRI" exception of the interface so callers will swallow a configuration error. Use a LogicException
  • the new tests in tests/Symfony/Routing/IriConverterTest.php use Prophecy, we're moving away from it, please use PHPUnit mocks. Also drop the docblocks on the private resolveIri methods and the comment in UriVariablesFromIriTest

Also we'll need a docs PR as this is a new public service, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants