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

feat: allow methods to control strictObjectIDCoercion #467

Merged
merged 1 commit into from
Sep 19, 2018
Merged

Conversation

virkt25
Copy link
Contributor

@virkt25 virkt25 commented Sep 17, 2018

Description

  • This PR came about as my work on loopback4-example-shopping. For relations, we need to be able to enable strictObjectIDCoercion for those methods only. This PR allows the flag to be passed in from a method instead of just being set on the model.

Related issues

  • connect to <link_to_referenced_issue>

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

Copy link
Contributor

@b-admike b-admike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @virkt25. Can we add tests for this new behaviour?

@virkt25
Copy link
Contributor Author

virkt25 commented Sep 18, 2018

Nice work @virkt25. Can we add tests for this new behaviour?

Tried adding tests as follows in /test/objectid.test.js:

  it('coerces ObjectID', function() {
    const coercedId = db.connector.coerceId(Book, '52fcef5c0325ace8dcb7a0bd');
    coercedId.should.be.an.instanceOf(db.ObjectID);
  });

  it('given strictObjectIDCoercion: true, does not coerce ObjectID', function() {
    const coercedId = db.connector.coerceId(Book, '52fcef5c0325ace8dcb7a0bd', {strictObjectIDCoercion: true});
    coercedId.should.be.an.instanceOf(String);
  });

But I keep getting the following errors:

1) ObjectID
       coerces ObjectID:
     TypeError: Cannot read property 'idName' of undefined
      at MongoDB.Connector.idName (node_modules/loopback-connector/lib/connector.js:161:35)
      at MongoDB.coerceId (lib/mongodb.js:483:21)
      at Context.<anonymous> (test/objectid.test.js:58:36)

  2) ObjectID
       given strictObjectIDCoercion: true, does not coerce ObjectID:
     TypeError: Cannot read property 'idName' of undefined
      at MongoDB.Connector.idName (node_modules/loopback-connector/lib/connector.js:161:35)
      at MongoDB.coerceId (lib/mongodb.js:483:21)
      at Context.<anonymous> (test/objectid.test.js:63:36)

Any idea how to add tests? There are no existing tests covering this flag / the coerceId, isObjectIdProperty functions.

@jannyHou
Copy link
Contributor

jannyHou commented Sep 18, 2018

@virkt25 I think you can provide the model name as string instead of a model constructor to get the tests pass:

 it('coerces ObjectID', function() {
    // note it is 'Book' not Book
    const coercedId = db.connector.coerceId('Book', '52fcef5c0325ace8dcb7a0bd');
    coercedId.should.be.an.instanceOf(db.ObjectID);
  });

@bajtos
Copy link
Member

bajtos commented Sep 18, 2018

Here is the code throwing the error (assuming your stack trace is from the latest version of loopback-connector as seen on GitHub):

https://github.com/strongloop/loopback-connector/blob/dbfebb845b9eba321353e99e080a22f77df0c110/lib/connector.js#L160-L162

Connector.prototype.idName = function(model) {
  return this.getDataSource(model).idName(model);
};

I strongly suspect that in the test you tried to wrote, there was no model defined on the datasource with the name matching the string you passed to coerceId.

What you need to do in your test:

  1. Create a new model on the datasource backed by the connector under the test. DataSource provides API for creating new models - see DataSource.prototype.createModel.

  2. Pass the name of the newly created model to coerceId function.

 it('coerces ObjectID', function() {
  const Book = db.createModel('Book' /*, {properties...}, {settings...}*/);
  const coercedId = db.connector.coerceId(Book.modelName, '52fcef5c0325ace8dcb7a0bd');
  coercedId.should.be.an.instanceOf(db.ObjectID);
});

I am typing this information from my head, it may not be entirely accurate. Sorry if it does not work out of the box!

Signed-off-by: virkt25 <taranveer@virk.cc>
Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: nice! LGTM

@virkt25 virkt25 merged commit b30a28e into master Sep 19, 2018
@virkt25 virkt25 deleted the fix-id branch September 19, 2018 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants