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.69k stars 7.63k forks source link

The use of SetMetadata custom decorators does not take effect #13402

Closed jackQian666 closed 7 months ago

jackQian666 commented 7 months ago

Is there an existing issue for this?

Current behavior

I defined a decorator using SetMetadata As shown below image

Then I defined an AuthGuard and added the logic of using @Public() to skip the guard. As shown below

image

And used the @Public() decorator in the method to generate the verification code image

I found that @Public() did not take effect,Then I debugged the code, It was found that the value of IS_PUBLIC_KEY defined in the @Public() decorator was not obtained through Reflector. As shown below,You can see that the printed value is undefined ,According to the official documentation, this problem has occurred before image

Minimum reproduction code

https://github.com/jackQian666/demo

Steps to reproduce

No response

Expected behavior

I hope that using @Public() can skip token verification

Package

Other package

No response

NestJS version

10.3.7

Packages versions

{ "name": "quan-view-server", "version": "0.0.1", "description": "", "author": "", "private": true, "license": "UNLICENSED", "scripts": { "build": "nest build", "format": "prettier --write \"src//*.ts\" \"test/*/.ts\"", "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}//.ts\" --fix", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@fastify/static": "^7.0.2", "@nestjs-modules/ioredis": "^2.0.2", "@nestjs/cache-manager": "^2.2.2", "@nestjs/common": "^10.3.7", "@nestjs/core": "^10.0.0", "@nestjs/jwt": "^10.2.0", "@nestjs/passport": "^10.0.3", "@nestjs/platform-express": "^10.0.0", "@nestjs/platform-fastify": "^10.3.7", "@nestjs/platform-socket.io": "^10.3.7", "@nestjs/swagger": "^7.3.1", "@nestjs/typeorm": "^10.0.2", "@nestjs/websockets": "^10.3.7", "@types/lodash": "^4.17.0", "cache-manager": "^5.4.0", "cache-manager-redis-store": "^3.0.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "crypto-js": "^4.2.0", "ioredis": "^5.3.2", "lodash": "^4.17.21", "mysql2": "^3.9.3", "nanoid": "^3.3.7", "passport": "^0.7.0", "passport-local": "^1.0.0", "redis": "^3.1.2", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", "svg-captcha": "^1.4.0", "typeorm": "^0.3.20" }, "devDependencies": { "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/supertest": "^6.0.0", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "eslint": "^8.42.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", "jest": "^29.5.0", "prettier": "^3.0.0", "source-map-support": "^0.5.21", "supertest": "^6.3.3", "ts-jest": "^29.1.0", "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "tsconfig-paths": "^4.2.0", "typescript": "^5.1.3" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".\.spec\.ts$", "transform": { "^.+\.(t|j)s$": "ts-jest" }, "collectCoverageFrom": [ "*/.(t|j)s" ], "coverageDirectory": "../coverage", "testEnvironment": "node" } }

Node.js version

18.19.0

In which operating systems have you tested?

Other

No response

kangkangovo2333 commented 7 months ago

使用 this.reflector.get( IS_PUBLIC_KEY, context.getHandler() )

jackQian666 commented 7 months ago

使用 this.reflector.get( IS_PUBLIC_KEY, context.getHandler() ) 试过了,不行的 image

jackQian666 commented 7 months ago

使用 this.reflector.get( IS_PUBLIC_KEY, context.getHandler() )

不知道为啥没人提,this.reflector 下面的方法我都试过了,都不行,使用 @SetMetadata() 装饰器也不行,都没法把元素数注入

kangkangovo2333 commented 7 months ago

使用 this.reflector.get( IS_PUBLIC_KEY, context.getHandler() )

不知道为啥没人提,this.reflector 下面的方法我都试过了,都不行,使用 @SetMetadata() 装饰器也不行,都没法把元素数注入

你的context.getHandler()没调用

jackQian666 commented 7 months ago

使用 this.reflector.get( IS_PUBLIC_KEY, context.getHandler() )

不知道为啥没人提,this.reflector 下面的方法我都试过了,都不行,使用 @SetMetadata() 装饰器也不行,都没法把元素数注入

你的context.getHandler()没调用 nen能得到类,没得到方法 image

kangkangovo2333 commented 7 months ago

使用 this.reflector.get( IS_PUBLIC_KEY, context.getHandler() )

不知道为啥没人提,this.reflector 下面的方法我都试过了,都不行,使用 @SetMetadata() 装饰器也不行,都没法把元素数注入

你的context.getHandler()没调用 nen能得到类,没得到方法 image

image 你这里的方法要调用

jackQian666 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

kangkangovo2333 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

我说的是你在guard内的context.getHandler没有调用 你仔细看看

jackQian666 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

我说的是你在guard内的context.getHandler没有调用 你仔细看看

怎么调用呢? 不是很理解你的意思,我的理解是把通过 context.getHandler 获取到目标方法 或者目标类,传给reflector,拿到对应的注入元数据,如果方便,你给瞅瞅,这个是上传的代码地址:https://github.com/jackQian666/demo

kangkangovo2333 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

我说的是你在guard内的context.getHandler没有调用 你仔细看看

怎么调用呢? 不是很理解你的意思,我的理解是把通过 context.getHandler 获取到目标方法 或者目标类,传给reflector,拿到对应的注入元数据

你看你装饰器的代码 确认在this.reflector.get()中 context.getHandler()是调用的

jackQian666 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

我说的是你在guard内的context.getHandler没有调用 你仔细看看

怎么调用呢? 不是很理解你的意思,我的理解是把通过 context.getHandler 获取到目标方法 或者目标类,传给reflector,拿到对应的注入元数据

你看你装饰器的代码 确认在this.reflector.get()中 context.getHandler()是调用的

image 这个是装饰器代码,都试过了,还是不行

kangkangovo2333 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

我说的是你在guard内的context.getHandler没有调用 你仔细看看

怎么调用呢? 不是很理解你的意思,我的理解是把通过 context.getHandler 获取到目标方法 或者目标类,传给reflector,拿到对应的注入元数据

你看你装饰器的代码 确认在this.reflector.get()中 context.getHandler()是调用的

image 这个是装饰器代码,都试过了,还是不行

说错了是守卫里

jackQian666 commented 7 months ago

"reflect-metadata": "^0.1.13",

调用了的,我把装饰器加到了captcha 方法上面,调用的也是captcha方法,守卫里面打印的依旧是上面的内容 image

我说的是你在guard内的context.getHandler没有调用 你仔细看看

怎么调用呢? 不是很理解你的意思,我的理解是把通过 context.getHandler 获取到目标方法 或者目标类,传给reflector,拿到对应的注入元数据

你看你装饰器的代码 确认在this.reflector.get()中 context.getHandler()是调用的

image 这个是装饰器代码,都试过了,还是不行

说错了是守卫里

大意了,应该是这种,jias加上括号才行,感谢感谢 image