-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: resolve customAccessToken warning error that causes app to crash #3921
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
Conversation
In the present implementation verifyUserRelations(Model) sets a const hasManyTokens which seems to resolve to a boolean or undefined, so checking hasManyTokens.polymorphic throws an error. It seems the intention is to check whether the the following conditions are met: 1. a model has a hasMany accessTokens relation 2. that relation is polymorphic The proposed change should resolve correctly for any models that meet this criteria.
|
Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test." |
|
@ryanxwelch , thanks for your contribution. Could you please fix the commit linter error according to the commit message guideline? Also, could you please add some tests? Thanks. |
|
Hello @ryanxwelch, thank you for the pull request. How can we reproduce the problem you are trying to fix? It would be great if we could add a unit test to verify your solution. To be honest, I never remember what is the difference between Lines 473 to 474 in 778395e
I am proposing to refactor this code a bit more, move variables like Thoughts? |
|
Hi @bajtos, thanks for the recommendation about empty objects in case of missing values. I've hopefully picked up this correctly in c5cf2f1. I confess, I'm quite new to unit testing, so it may be some time before I can have a working test added to the PR. Would it be appropriate to insert the test here? https://github.com/strongloop/loopback/blob/3.x-latest/test/access-token.test.js#L74 |
Ha! I looked up the commit which introduced the user/token verification at app startup (see However, I'd like to better understand how to reproduce the problem you are encountering so that I can verify the fix manually and ensure we are fixing the right place. How did you @ryanxwelch configure your application so that your User model's relations don't have Earlier today, I was investigating #3917, where @mcitdev is running into a similar problem, where our code assumes a User model has "accessTokens" relation configured, but the I'd like to build a better understanding of both of these use cases, there may be other places we need to fix to support you. |
|
Hi @bajtos, Thanks for looking into this. My app generally follows the guidelines laid out here: It's configured with two models named {
"name": "CustomAccessToken",
"base": "AccessToken",
"relations": {
"user": {
"type": "belongsTo",
"idName": "id",
"polymorphic": {
"idType": "string",
"foreignKey": "userId",
"discriminator": "principalType"
}
}
}
}{
"name": "user",
"base": "User",
"properties": { ... },
...
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "CustomAccessToken",
"polymorphic": { "foreignKey": "userId", "discriminator": "principalType" },
"options": { "disableInclude": true }
}
},
...
}{
"name": "admin",
"base": "User",
"properties": { ... },
...
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "CustomAccessToken",
"polymorphic": { "foreignKey": "userId", "discriminator": "principalType" },
"options": { "disableInclude": true }
}
},
...
}
|
|
Let's not spend too much time on this. In #3917, I got a better understanding on how it can be that |
Description
In the present implementation
verifyUserRelations(Model)sets aconst hasManyTokenswhich seems to resolve to a boolean or undefined, so checkinghasManyTokens.polymorphicthrows an error.It seems the intention is to check whether the the following conditions are met:
The proposed change should resolve correctly for any models that meet this criteria.