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

fix: 修复aop丢失原引用,导致修改ctx的逻辑失效的问题 #187

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/aop-runtime/src/AspectExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ export class AspectExecutor {
for (const aspectAdvice of this.aspectAdviceList) {
const advice: IAdvice = ctx.that[aspectAdvice.name];
if (advice.beforeCall) {
await advice.beforeCall({ ...ctx, adviceParams: aspectAdvice.adviceParams });
/**
* 这里...写法使传入的参数变成了一个新的对象
* 因此beforeCall里面如果修改了ctx.args
* 最新的args是不会在方法里生效的
* 先保证args可以生效
* 不改动其余地方
*/
const params = { ...ctx, adviceParams: aspectAdvice.adviceParams };
await advice.beforeCall(params);
ctx.args = params.args;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions core/aop-runtime/test/fixtures/modules/hello_succeed/Hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const pointcutAdviceParams = {
cut: Math.random().toString(),
};

// 测试aop修改ctx的args的值
const TEST_CTX_ARGS_VALUE = 123;

@Advice()
export class PointcutAdvice implements IAdvice<Hello> {
@Inject()
Expand All @@ -40,11 +43,13 @@ export class PointcutAdvice implements IAdvice<Hello> {
name: ctx.args[0],
adviceParams: ctx.adviceParams,
});
ctx.args = [...ctx.args, TEST_CTX_ARGS_VALUE];
}

async afterReturn(ctx: AdviceContext<Hello>, result: any): Promise<void> {
assert.ok(ctx.adviceParams);
assert.deepStrictEqual(ctx.adviceParams, pointcutAdviceParams);
assert.deepStrictEqual(ctx.args[ctx.args.length - 1], TEST_CTX_ARGS_VALUE);
this.callTrace.addMsg({
className: PointcutAdvice.name,
methodName: 'afterReturn',
Expand Down
Loading