Skip to content

@ApiSubresource maxDepth parameter is not working in 2.6.1 #3982

Description

@crbanman

API Platform version(s) affected: 2.6.1

Description

When using the @ApiSubresource annotation, routes are still being generated beyond the defined maxDepth.

This makes it possible for the "Route pattern" LogicException to be thrown by the RouteCompiler if an entity shares a subresource of the same type with one of its subresources (i.e. A Post has a many-to-one relationship with Author and a one-to-many relationship with Comment and Comment also has a many-to-one relationship with Author).

How to reproduce

  1. Create Post, Author, and Comment Entities and add the @ApiSubresource(maxDepth=1) annotation to all relationships.
    • Post
      • id
      • content
      • author (many-to-one with Author)
      • comments (one-to-many with Post)
    • Author
      • id
      • posts (one-to-many with Post)
      • comments (one-to-many with Comment)
    • Comment
      • id
      • message
      • post (many-to-one with Comment)
      • author (many-to-one with Author)
  2. Clear cache to see the "Route pattern" error.

Additional Context

Result of cache:clear with above entities:

/srv/api # bin/console cache:clear

 // Clearing the cache for the dev environment with debug                       
 // true                                                                        


In RouteCompiler.php line 149:
                                                                               
  Route pattern "/posts/{id}/author/posts/{posts}/comments/{comments}/author/  
  comments/{comments}/post.{_format}" cannot reference variable name "comment  
  s" more than once.                                                           
                                                                               

cache:clear [--no-warmup] [--no-optional-warmers]

Removing the @ApiSubresource annotation from Comment::author will allow cache to clear, but then you get to see a good example of the subresource depth not being limited:

/srv/api # bin/console debug:router | grep "GET"
  api_comments_get_collection                                       GET      ANY      ANY    /comments.{_format}                                                             
  api_comments_get_item                                             GET      ANY      ANY    /comments/{id}.{_format}                                                        
  api_comments_post_get_subresource                                 GET      ANY      ANY    /comments/{id}/post.{_format}                                                   
  api_comments_post_author_get_subresource                          GET      ANY      ANY    /comments/{id}/post/author.{_format}                                            
  api_comments_post_author_posts_get_subresource                    GET      ANY      ANY    /comments/{id}/post/author/posts.{_format}                                      
  api_comments_post_author_posts_comments_get_subresource           GET      ANY      ANY    /comments/{id}/post/author/posts/{posts}/comments.{_format}                     
  api_comments_post_author_comments_get_subresource                 GET      ANY      ANY    /comments/{id}/post/author/comments.{_format}                                   
  api_comments_post_comments_get_subresource                        GET      ANY      ANY    /comments/{id}/post/comments.{_format}                                          
  api_posts_get_collection                                          GET      ANY      ANY    /posts.{_format}                                                                
  api_posts_get_item                                                GET      ANY      ANY    /posts/{id}.{_format}                                                           
  api_posts_author_get_subresource                                  GET      ANY      ANY    /posts/{id}/author.{_format}                                                    
  api_posts_author_posts_get_subresource                            GET      ANY      ANY    /posts/{id}/author/posts.{_format}                                              
  api_posts_author_posts_comments_get_subresource                   GET      ANY      ANY    /posts/{id}/author/posts/{posts}/comments.{_format}                             
  api_posts_author_posts_comments_post_get_subresource              GET      ANY      ANY    /posts/{id}/author/posts/{posts}/comments/{comments}/post.{_format}             
  api_posts_author_comments_get_subresource                         GET      ANY      ANY    /posts/{id}/author/comments.{_format}                                           
  api_posts_author_comments_post_get_subresource                    GET      ANY      ANY    /posts/{id}/author/comments/{comments}/post.{_format}                           
  api_posts_author_comments_post_comments_get_subresource           GET      ANY      ANY    /posts/{id}/author/comments/{comments}/post/comments.{_format}                  
  api_posts_comments_get_subresource                                GET      ANY      ANY    /posts/{id}/comments.{_format}                                                  
  api_posts_comments_post_get_subresource                           GET      ANY      ANY    /posts/{id}/comments/{comments}/post.{_format}                                  
  api_posts_comments_post_author_get_subresource                    GET      ANY      ANY    /posts/{id}/comments/{comments}/post/author.{_format}                           
  api_posts_comments_post_author_posts_get_subresource              GET      ANY      ANY    /posts/{id}/comments/{comments}/post/author/posts.{_format}                     
  api_posts_comments_post_author_comments_get_subresource           GET      ANY      ANY    /posts/{id}/comments/{comments}/post/author/comments.{_format}                  
  api_authors_get_collection                                        GET      ANY      ANY    /authors.{_format}                                                              
  api_authors_get_item                                              GET      ANY      ANY    /authors/{id}.{_format}                                                         
  api_authors_posts_get_subresource                                 GET      ANY      ANY    /authors/{id}/posts.{_format}                                                   
  api_authors_posts_author_get_subresource                          GET      ANY      ANY    /authors/{id}/posts/{posts}/author.{_format}                                    
  api_authors_posts_author_comments_get_subresource                 GET      ANY      ANY    /authors/{id}/posts/{posts}/author/comments.{_format}                           
  api_authors_posts_author_comments_post_get_subresource            GET      ANY      ANY    /authors/{id}/posts/{posts}/author/comments/{comments}/post.{_format}           
  api_authors_posts_author_comments_post_comments_get_subresource   GET      ANY      ANY    /authors/{id}/posts/{posts}/author/comments/{comments}/post/comments.{_format}  
  api_authors_posts_comments_get_subresource                        GET      ANY      ANY    /authors/{id}/posts/{posts}/comments.{_format}                                  
  api_authors_posts_comments_post_get_subresource                   GET      ANY      ANY    /authors/{id}/posts/{posts}/comments/{comments}/post.{_format}                  
  api_authors_posts_comments_post_author_get_subresource            GET      ANY      ANY    /authors/{id}/posts/{posts}/comments/{comments}/post/author.{_format}           
  api_authors_posts_comments_post_author_comments_get_subresource   GET      ANY      ANY    /authors/{id}/posts/{posts}/comments/{comments}/post/author/comments.{_format}  
  api_authors_comments_get_subresource                              GET      ANY      ANY    /authors/{id}/comments.{_format}                                                
  api_authors_comments_post_get_subresource                         GET      ANY      ANY    /authors/{id}/comments/{comments}/post.{_format}                                
  api_authors_comments_post_author_get_subresource                  GET      ANY      ANY    /authors/{id}/comments/{comments}/post/author.{_format}                         
  api_authors_comments_post_author_posts_get_subresource            GET      ANY      ANY    /authors/{id}/comments/{comments}/post/author/posts.{_format}                   
  api_authors_comments_post_author_posts_comments_get_subresource   GET      ANY      ANY    /authors/{id}/comments/{comments}/post/author/posts/{posts}/comments.{_format}  
  api_authors_comments_post_comments_get_subresource                GET      ANY      ANY    /authors/{id}/comments/{comments}/post/comments.{_format}   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions