nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.11k stars 7.56k forks source link

Route parameter override another controller #13723

Closed IgorRSGraziano closed 3 months ago

IgorRSGraziano commented 3 months ago

Is there an existing issue for this?

Current behavior

When creating a resource, such as User, which contains a route like :id, and within that resource create another resource, let's call it Auth, and the Auth has a root route '/', this root route instead of hitting in auth, it is falling on the User route

In example code, i create manually user (inside src/user), and generated by nest ge res resource-user (inside src/resource-user)

Minimum reproduction code

https://github.com/IgorRSGraziano/nest-route-override

Steps to reproduce

1 - npm i 2 - npm run start:dev 3 - try access http://localhost:3000/user/auth

Expected behavior

Expected result from example code is 'Session' response (from controller src/user/user.controller.ts)

Package

Other package

No response

NestJS version

10.0.0

Packages versions

    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^10.0.0",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1"

Node.js version

21.2.0

In which operating systems have you tested?

Other

No response

micalevisk commented 3 months ago

sorry but I didn't understand what's wrong. In your reproduction repository, GET http://localhost:3000/user/auth is returning the response from src/user/user.controller.ts, as you said it's the expected behavior

Are you saying that it should be returning "Session" from src/user/auth/auth.controller.ts instead?

IgorRSGraziano commented 3 months ago

sorry but I didn't understand what's wrong. In your reproduction repository, GET http://localhost:3000/user/auth is returning the response from src/user/user.controller.ts, as you said it's the expected behavior

Are you saying that it should be returning "Session" from src/user/auth/auth.controller.ts instead?

Sorry if the explanation was confusing, when accessing GET http://localhost:3000/user/auth, you should hit the controller src/user/auth/auth.controller.ts and not src/user/user.controller

micalevisk commented 3 months ago

can you share the initial version of the project? could be in another branch of your repository. I didn't understand how the nest ge res ... and that Auth controller are related to each other

IgorRSGraziano commented 3 months ago

The problem is not in nest g res..., I just generated it using this resource to have another sampling of the error.

But to be clearer, I created a branch without-resource without generating this resource

micalevisk commented 3 months ago

but the src/user/auth/auth.controller.ts controller doesn't have a user prefix. Why are you expecting GET /user/auth to hit that controller? :thinking:

image

IgorRSGraziano commented 3 months ago

but the src/user/auth/auth.controller.ts controller doesn't have a user prefix. Why are you expecting GET /user/auth to hit that controller? 🤔

image

Oh god, sorry, I really forgot about that. But when trying to add user/auth I had the same error on GET http://localhost:3000/user/auth

micalevisk commented 3 months ago

AFIAK this is expected. The controllers listed at AppModule are taking precedence over the ones discovered for each module at AppModule's imports.

If you create another module (eg: UserModule) to register the UserController controller and then if your AppModule imports both: [AuthModule, UserModule], it will behave as you're expecting due to the registration order.

IgorRSGraziano commented 3 months ago

It worked correctly, thank you very much!