Loopback: PersistedModel's updateAll method crashes the server when invoked via the remote connector

Created on 5 Dec 2017  路  16Comments  路  Source: strongloop/loopback

Description

I have a model with a remote datasource. When I tries to execute updateAll method my server crashes.

The problem is loopback registers updateAll remote method with an object type, which is not supported by strong-remoting type registry.

setRemoting(PersistedModel, 'updateAll', {
   ...
   returns: {
     arg: 'info',
     description: 'Information related to the outcome of the operation',
     type: { // THE PROBLEM PLACE
       count: {
         type: 'number',
         description: 'The number of instances updated',
       },
     },
     root: true,
   },
   http: {verb: 'post', path: '/update'},
 });

Reproduction

To reproduce the problem, just run this test in any loopback application. It fails on updateAll shared method.

describe('Type Registry Tests', () => {
  let Model;

  before('init a memory datasource', () => {
    if (!app.dataSources.db) {
      app.dataSource('db', {connector: 'memory'});
    }
  });

  beforeEach('create a model and attach it to the app', () => {
    Model = app.registry.createModel({
      name: specs.modelName()
    });
    app.model(Model, {dataSource: 'db'});
  });

  it('should be compatible with get converter method', () => {
    _.forEach(app.models[Model.modelName].sharedClass.methods(), (method) => {
      should(() => {
        _.forEach(method.returns, ({type}) => {

          // The type is {count: {type: 'number', description: '...'} 
          // for updateAll method, but it supports only string and array
          app.remotes()._typeRegistry.getConverter(type);
        });
      }).not.throw();
    });
  });
});

Additional information

"loopback": "3.17.0",
"strong-remoting": "3.6.0",

bug major

All 16 comments

Hi @angfal I am trying to reproduce your error, are you running any endpoint from the Explorer(usually http://localhost:3000/explorer/)? If so can you post the endpoint name here? Thanks.

I have a model with a remote datasource. When I tries to execute updateAll method my server crashes.

I am trying to figure out how do you execute the updateAll method.

@jannyHou I have two loopback servers A and B. The server A contains a model with a remote connector to the server B. When a function from the server A executes 'Model.updateAll' request to the server B, I receive success result, but the server A crashes when it tries to process the response.

I can provide a sandbox example if it doesn't help

The links to the sandbox:

Just run the both servers and execute Message.updateAll service from the explorer of serverA (3000 port). The server crashes

@angfal Thank you now I can see the error, the count's type is number, so its type is string, which should pass the following assertion:

assert(typeof type === 'string' || Array.isArray(type),
    'type must be either an array or a string.');

Probably for some reason the type for count is detected incorrectly...looking into the bug. Will update here when have the fix PR.

FYI, I created a PR for it, but not sure is it a proper fix, would like to hear opinions from strong-remoting experts before review.
https://github.com/strongloop/loopback/pull/3741

@jannyHou Thank you very much! But I made the same pull above. It was rejected

I see...closed https://github.com/strongloop/loopback/pull/3741, I find the rejected PR, following the chat there.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@jannyHou Any news here?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Wanting to keep this open as I see no fix yet.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Cross-posting https://github.com/strongloop/loopback/pull/3718#issuecomment-349319801:

Hello, thank you for the pull request. I am afraid we cannot revert #2842 because it's required for client code-generators to work, see the discussion in https://github.com/strongloop/loopback/issues/2104.

We need to fix strong-remoting and/or loopback-connector-remote to correctly support anonymously defined types in remote invocations.

Let me add major label to this issue to prevent stalebot from trying to automatically close it. (And also because LB should not crash on unhandled errors!)

Discussion from the estimation meeting:
possible solution: deserialize the object into JSON

Was this page helpful?
0 / 5 - 0 ratings