Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route Group problem with name attribute if not all routes are named #745

Open
tpow opened this issue Mar 10, 2023 · 2 comments
Open

Route Group problem with name attribute if not all routes are named #745

tpow opened this issue Mar 10, 2023 · 2 comments
Labels

Comments

@tpow
Copy link
Contributor

tpow commented Mar 10, 2023

Describe the bug

In trying to upgrade a project from Masonite 3 to 4, I noticed a change in behavior. When the Route.group has an overall name attribute, all the routes within the group also need to have names. This was not true in version 3 -- some could be left unnamed.

Expected behaviour

I believe this example, slightly modified from the documentation, should work:

ROUTES = [
  Route.group([
    Route.get('/settings', 'DashboardController@settings').name('settings'),
    Route.get('/monitor', 'DashboardController@monitor'),  # <-- Note this is unnamed
  ],
  prefix="/dashboard",
  middleware=['web', 'cors'],
  name="dash."),
] 

My expectation is that the first route will use /dashboard/settings and have the name dash.settings and the second one will have the path /dashboard/monitor and will not be named at all. This should be visible in python craft routes:list The equivalent in masonite 3 worked as I expected and described above.

Steps to reproduce the bug

At the moment, after adding this to routes/web.py on a new project, when you show the routes it throws a traceback:

Traceback (most recent call last):
  File "/home/tim/m4/craft", line 8, in <module>
    from wsgi import application
  File "/home/tim/m4/wsgi.py", line 11, in <module>
    application.register_providers(Kernel, ApplicationKernel)
  File "/home/tim/m4/.venv/lib/python3.11/site-packages/masonite/foundation/Application.py", line 32, in register_providers
    provider.register()
  File "/home/tim/m4/Kernel.py", line 37, in register
    self.register_routes()
  File "/home/tim/m4/Kernel.py", line 80, in register_routes
    Route.group(
  File "/home/tim/m4/.venv/lib/python3.11/site-packages/masonite/routes/Route.py", line 166, in group
    middleware = route.list_middleware
                 ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'list_middleware'

There are two workarounds: either (1) add the name to every individual route in the group or (2) remove the name attribute from the overall group. With 2, you then need to repeat the group name prefix ("dash." in the example above) in each named route in order to get the same names. This defeats the point of having it at the group level.

Screenshots

No response

OS

Linux

OS version

Ubuntu LTS

Browser

Safari 16

Masonite Version

4.17.4

Anything else ?

No response

@tpow tpow added the bug label Mar 10, 2023
@DaveParr
Copy link

Same, though this occurs for me in the build a blog tutorial just before database set up: https://docs.masoniteproject.com/prologue/create-a-blog#database-setup using current PyPI release 4.17.4:

❯ python craft migrate
Traceback (most recent call last):
  File "/home/dave/Development/masonite_tutorial/craft", line 8, in <module>
    from wsgi import application
  File "/home/dave/Development/masonite_tutorial/wsgi.py", line 11, in <module>
    application.register_providers(Kernel, ApplicationKernel)
  File "/home/dave/Development/masonite_tutorial/.venv/lib/python3.10/site-packages/masonite/foundation/Application.py", line 32, in register_providers
    provider.register()
  File "/home/dave/Development/masonite_tutorial/Kernel.py", line 37, in register
    self.register_routes()
  File "/home/dave/Development/masonite_tutorial/Kernel.py", line 80, in register_routes
    Route.group(
  File "/home/dave/Development/masonite_tutorial/.venv/lib/python3.10/site-packages/masonite/routes/Route.py", line 166, in group
    middleware = route.list_middleware
AttributeError: 'NoneType' object has no attribute 'list_middleware'

@DaveParr
Copy link

Well, I take mine back, I deleted, then rewrote the line ROUTES += Auth.routes() and now it works. I guess I copied badly? Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants