loopbackio / loopbackio/loopback-next

[BUG]: @inject() + asProxyWithInterceptors make repository function not work!

Open
#8,475 5 comments 0 reactions 0 assignees View on GitHub
bug Repository
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

### Describe the bug

# What is the problem
When I use the global interceptor:
`@inject('repositorys.xxxRepository', {asProxyWithInterceptors: true})` will be successfully intercepted.
But many functions of repository variable not work!
```typescript
export class TodoController {
constructor(
@inject('repositorys.TodoRepository', {asProxyWithInterceptors: true})
public todoRepository: TodoRepository,
) { }
@get('/todos')
@response(200, {
description: 'Array of Todo model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Todo, {includeRelations: true}),
},
},
},
})
async find(
@param.filter(Todo) filter?: Filter,
): Promise {
return this.todoRepository.find(filter);
}
}
```
HTTP Status Code 500 Error:
```cmd
Server is running at http://[::1]:3000
Try http://[::1]:3000/ping
TodoRepository#find() interceptor success!
Request GET /todos?filter=%7B%0A%20%0A%7D failed with status code 500. ResolutionError: The argument 'TodoRepository.prototype.find[1]' is not decorated
for dependency injection but no value was supplied by the caller. Did you forget to apply @inject() to the argument? (context: InterceptedInvocationContext-n626DiRrTbSKOXsrJCAr-w-38)
at D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\resolver.ts:233:15
at resolveList (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\value-promise.ts:169:28)
at resolveInjectedArguments (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\resolver.ts:220:21)
at invokeTargetMethodWithInjection (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\invocation.ts:223:49)
at InterceptedInvocationContext.invokeTargetMethod (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\invocation.ts:119:14)
at targetMethodInvoker (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor.ts:349:23)
at D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:219:14
at transformValueOrPromise (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\value-promise.ts:298:12)
at GenericInterceptorChain.invokeNextInterceptor (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:214:35)
at GenericInterceptorChain.next (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:202:17)
at D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:219:42
at TestInterceptor.intercept (D:\githubFile\loopback4-repository-proxy-issue\src\interceptors\test.interceptor.ts:55:28)
at D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:219:14
at transformValueOrPromise (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\value-promise.ts:298:12)
at GenericInterceptorChain.invokeNextInterceptor (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:214:35)
at GenericInterceptorChain.next (D:\githubFile\loopback4-repository-proxy-issue\node_modules\@loopback\context\src\interceptor-chain.ts:202:17)
```
# Expected behavior
The interceptor takes effect and the repository variable function work.
# Actual behavior
It throws the above exception.
# How to reproduce the problem
1. git clone [https://github.com/Embraceeeee/loopback4-repository-proxy-issue](https://github.com/Embraceeeee/loopback4-repository-proxy-issue)
2. npm install
3. npm start
4. visit url [http://[::1]:3000/explorer/#/TodoController](http://[::1]:3000/explorer/#/TodoController)
5. view output

### Logs

_No response_

### Additional information

_No response_

### Reproduction

https://github.com/Embraceeeee/loopback4-repository-proxy-issue

Contributor guide

Open the contributing guide

Research direction

Start with the linked reproduction repository: run npm install, npm start, and the TodoController endpoint to reproduce the failure. Read src/interceptors/test.interceptor.ts and the repository proxy/interceptor entry points involved in the stack trace; done means repository methods work while the interceptor is active without the dependency-resolution error.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.