Skip to content

Rolemapping only accept principalId where with like #3121

Closed

Description

@felipetoffolo1

Bug/Feature request

After i upgraded Loopback to version 3, This filter o rolemapping returns nothing
{"where":{"principalId":"5880038427758463a1ae5a3b"}}
i changed my filter to this
{"where":{"principalId":{ "like": "5880038427758463a1ae5a3b"}}}
and it worked. But is it a bug? Or it changed onn the new version? (i did not find anything about it in migration docs)

Expected result

Expected
filter {"where":{"principalId":"5880038427758463a1ae5a3b"}} to return results

Actual
it returns nothing

Additional information

linux x64 7.4.0

├── loopback@3.2.1
├── loopback-boot@2.23.0
├── loopback-component-explorer@4.0.0
├── loopback-component-push@1.7.0
├── loopback-component-storage@3.0.0
├── loopback-connector-mongodb@3.0.0

Activity

  1. faridnsh commented on Jan 20, 2017

    @faridnsh
    Contributor

    Try converting "5880038427758463a1ae5a3b" to object id.

  2. totalard commented on Feb 5, 2017

    @totalard

    How i do that?

  3. luncht1me commented on Feb 9, 2017

    @luncht1me

    Converting to ObjectID doesn't work. I'm having the same problem.

    Trying to find a RoleMapping using principalId in the where query returns nothing.
    If I try and find it as a regex of the Id string it works.
    principalId is stored as a string in mongodb.

    Something, somewhere, in either connector-mongodb or datasource-juggler is breaking the string query for principalId. I can query any other field and get a result except the principalId.

    I'm sure you can understand the dangers of querying with a regex instead of an exact string match...

  4. luncht1me commented on Feb 9, 2017

    @luncht1me

    Just a debug output:

      loopback:connector:mongodb all +2ms RoleMapping { where:
       { roleId: 589b917cc889c618f09ca889,
         principalId: '589b917cc889c618f09ca888' } }
      loopback:connector:mongodb MongoDB: model=RoleMapping command=find +2ms [ { roleId: 589b917cc889c618f09ca889,
        principalId: 589b917cc889c618f09ca888 },
      [Function] ]
    

    In the first output, the principalId is a string, as seen by the quotes. But then the next output, principalId is no longer a string but an actual Id. I'm assuming this casting here is what's breaking the query.

  5. luncht1me commented on Feb 9, 2017

    @luncht1me

    After lots of debugging. I've come to a solution.

    In loopback's default common/models/role-mapping.json, added "strictObjectIDCoercion": true to the model definition.
    Now principalId can be queried properly.

    resulting node_modules/loopback/common/models/role-mapping.json:

    {
      "name": "RoleMapping",
      "description": "Map principals to roles",
      "strictObjectIDCoercion": true,
      "properties": {
        "id": {
          "type": "string",
          "id": true,
          "generated": true
        },
        "principalType": {
          "type": "string",
          "description": "The principal type, such as user, application, or role"
        },
        "principalId": {
          "type": "string",
          "index": true
        }
      },
      "relations": {
        "role": {
          "type": "belongsTo",
          "model": "Role",
          "foreignKey": "roleId"
        }
      }
    }

    Can we get this change persisted to loopback?? Thanks.

  6. faridnsh commented on Feb 10, 2017

    @faridnsh
    Contributor

    Seems like same issue at #1441. You can also try my workaround which I described in here:

    var ObjectID = RoleMapping.getDataSource().connector.getDefaultIdType();
    
    // Because of this: https://github.com/strongloop/loopback-connector-mongodb/issues/1441
    RoleMapping.defineProperty('principalId', {
      type: ObjectID,
    });
    
  7. luncht1me commented on Feb 10, 2017

    @luncht1me
  8. luncht1me commented on Feb 10, 2017

    @luncht1me
  9. matias-casal commented on Feb 14, 2017

    @matias-casal

    I try with @luncht1me solutions but still have this issue.

    after a few test, the problem is with all the ids in mongodb, its not just about the roleMapping

  10. bajtos commented on Feb 16, 2017

    @bajtos
    Member

    cc @fabien I know you are using the MongoDB connector a lot, have you encountered this issue too? Can you think of any advice on how to fix it in user-land?

  11. bajtos commented on Feb 16, 2017

    @bajtos
    Member

    Seems like same issue at #1441

    Can we close this issue as a duplicate of #1441 then? /cc @alFReD-NSH

  12. fabien commented on Feb 16, 2017

    @fabien
    Contributor

    @bajtos here's what I have in a bootscript - it works fine:

    var RoleMapping = app.models['RoleMapping'];
    RoleMapping.settings.strictObjectIDCoercion = true;
    
  13. bajtos commented on Feb 17, 2017

    @bajtos
    Member

    Hello, I submitted strongloop/loopback-workspace#437 that will enable strictObjectIDCoercion for all new applications scaffolded using our lb/apic/slc tooling.

    For existing applications, you can either add a boot script as @fabien shown above, or you can enable the setting via server/model-config.json:

    {
      "RoleMapping": {
        "dataSource": "db",
        "options": {
          "strictObjectIDCoercion": true
        },
        "public": false
      }
    }

    As part of the update, according to #3198 (comment), you also need to update the data already stored in your MongoDB database to fix the type of the relevant id fields.

  14. self-assigned this
    on Feb 17, 2017
  15. bajtos commented on Mar 3, 2017

    @bajtos
    Member

    I am closing this issue as resolved.

  16. pr3mar commented on Aug 25, 2017

    @pr3mar

    This error still exists.

  17. bajtos commented on Aug 28, 2017

    @bajtos
    Member

    @pr3mar

    This error still exists.

    Please open a new GitHub issue and provide us with instruction how to reproduce the error you are seeing, see http://loopback.io/doc/en/contrib/Reporting-issues.html#bug-report

  18. locked and limited conversation to collaborators on Aug 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions