Skip to content

Conversation

@VincentLanglet
Copy link
Contributor

Q A
Branch? main
Tickets Closes #7476
License MIT
Doc PR api-platform/docs#...

@VincentLanglet VincentLanglet changed the title Try adding requirements dynamically based on doctrine field feat(doctrine): add requirements dynamically based on doctrine field Oct 22, 2025
@VincentLanglet VincentLanglet marked this pull request as ready for review October 22, 2025 13:05
@VincentLanglet VincentLanglet requested a review from soyuka October 22, 2025 13:06
@VincentLanglet VincentLanglet added this to the 4.3 milestone Oct 22, 2025
@VincentLanglet
Copy link
Contributor Author

Test are failing because of this line

1-nested is set as id to the nested ressource and then the uri is not correctly generated because it's not a valid id.

I feel like the error is still legit because the resource has

    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups('contain_non_resource')]

which means that 1-nested will never be a valid id.

But it still make me think I cannot add automatically a requirement if a Provider is used.

if (null === $operation->getProvider()) {
$operation = $operation->withProvider($this->getProvider($operation));

if ($operation instanceof HttpOperation) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ($operation instanceof HttpOperation) {
if ($operation instanceof HttpOperation && null === $operation->getRequirements()) {

}
}

return $requirements;
Copy link
Member

Choose a reason for hiding this comment

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

Not sure that this should be enabeld like this, especially with regular expression. I largely prefer using validation constraints (that are now enabled on query parameters and that we should activate on uri variables).

IMO this should likely be opt-in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll give you my use case (which is maybe wrong).

When let's say I declare a Get() route on project resource, without specifying the url.
By default ApiPlatform will generate project/{id}

Let's say I also declare manually a GET project/settings url, this will conflict and ApiPlatform will try to resolve setting as an id even if

  • It's not digit (if I use integer ids)
  • It's not a uuid (if I use uuids)

By adding requirements I solve the conflict. What would recommend you instead ?

When people are defining manually they template we could consider it's legit to ask them to add the requirement too. But when it's api platform which auto-generate the url, it's an issue.

Copy link
Member

Choose a reason for hiding this comment

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

can you not set the requirements yourself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure I can do, and will do (manually or with a RequirementResourceMetadataCollectionFactory). But it felt "weird" to me to have a definition like

new Get(
     requirements: ['uuid' => '...']
);

when the route is not explicitly given. And I wanted to avoid to redefine

new Get(
     uriTemplate: 'foo/{uuid}',
     requirements: ['uuid' => '...']
);

since it's already the default uriTemplate.

But, not a big deal

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.

Automatically add requirements on uriVariables

2 participants