rupadana / filament-api-service

A simple api service for supporting filamentphp
https://filamentphp.com/plugins/rupadana-api-service
MIT License
95 stars 22 forks source link

[Bug]: in some cases groupStack for api routes are not correct (fix available) #45

Closed eelco2k closed 3 months ago

eelco2k commented 3 months ago

What happened?

in ApiService.php in function registerRoutes(Panel $panel) there is a static function call to static::handlers(). But this makes the group() of routes in some specific cases incorrect

the array length of the groupStack should be 3 like so:

#groupStack: array:3 [▼
    0 => array:2 [▼
      "prefix" => "api"
      "as" => "api."
    ]
    1 => array:5 [▼
      "as" => "api.admin."
      "middleware" => array:3 [ …3]
      "prefix" => "api/admin/{tenant}"
      "namespace" => null
      "where" => []
    ]
    2 => array:5 [▼
      "middleware" => array:3 [ …3]
      "as" => "api.admin.users."
      "prefix" => "api/admin/{tenant}/users"
      "namespace" => null
      "where" => []
    ]
  ]

but in somecases a 4th item will get appended with the next resource like so:

#groupStack: array:3 [▼
    0 => array:2 [▼
      "prefix" => "api"
      "as" => "api."
    ]
    1 => array:5 [▼
      "as" => "api.admin."
      "middleware" => array:3 [ …3]
      "prefix" => "api/admin/{tenant}"
      "namespace" => null
      "where" => []
    ]
    2 => array:5 [▼
      "middleware" => array:3 [ …3]
      "as" => "api.admin.users."
      "prefix" => "api/admin/{tenant}/users"
      "namespace" => null
      "where" => []
    ]
    3 => array:5 [▼
      "middleware" => array:3 [ …3]
      "as" => "api.admin.users."
      "prefix" => "api/admin/{tenant}/users/companies"
      "namespace" => null
      "where" => []
    ]
  ]

which is incorrect. because in this example the prefix should be on index 2: api/admin/{tenant}/companies and not a third index added with: api/admin/{tenant}/users/companies.

The fix should be:

remove: static::handlers(); in the ->group() function:

$route = Route::name(
            $name
        )
            ->middleware($resourceRouteMiddlewares)
            ->prefix(static::$groupRouteName ?? $slug)
            ->group(function (Router $route) {
                foreach (static::handlers() as $key => $handler) {
                    app($handler)->route($route);
                }
            });

How to reproduce the bug

see above

Package Version

3.2

PHP Version

8.3

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

rupadana commented 3 months ago

thank you, it fixed on this PR #46