node-client-api icon indicating copy to clipboard operation
node-client-api copied to clipboard

DatabaseClient.eval() always return true when the result is a boolean

Open fgeorges opened this issue 2 years ago • 3 comments

When evaluating a piece of JavaScript on the server using DatabaseClient.eval() seems to always return true when the expression results in a single boolean. Even when the result is false.

To reproduce:

require('marklogic')
    .createDatabaseClient({
        host:     'localhost',
        port:     '8000',
        database: 'neo-elec-content',
        user:     'admin',
        password: 'admin',
        authType: 'DIGEST'
    })
    .eval(`false;`)
    .result()
    .then(res => console.dir(res));

returns the following:

[ { format: 'text', datatype: 'boolean', value: true } ]

We would expect of course value to be false. If you change the expression in the eval() to, say, '42;', then you get the correct result:

[ { format: 'text', datatype: 'integer', value: 42 } ]

I use the marklogic package version 2.9.0 (installed last week with npm i marklogic.)

fgeorges avatar Mar 25 '22 20:03 fgeorges

Looks like this is where that response object is generated for boolean:

https://github.com/marklogic/node-client-api/blob/master/lib/server-exec.js#L75

and Boolean(data.content)would produce the correct boolean value if content is false

let data = {"content": false};
Boolean(data.content);

So, the issue is further upstream in how the data.content is produced from the results, and that looks like it is done in the marshal() function https://github.com/marklogic/node-client-api/blob/ee49df1fa05bb29908f931b2f13bce69470c2567/lib/mlutil.js#L215

where boolean falls through to the return String(data) at the end.

I think it needs an extra if statement to handle boolean:

else if (typeof data == "boolean") {
  return data;
}

If that is the case, when making a fix for boolean, should ensure that we have full coverage for all primitive types (and figure out if/when we will be upgrading the MarkLogic V8 engine and can handle bigint )

hansenmc avatar Mar 25 '22 20:03 hansenmc

This test to verify boolean only verifies true, which is why it was not discovered. It would be helpful to have tests for both true and false https://github.com/marklogic/node-client-api/blob/master/test-basic/server-exec.js#L78

hansenmc avatar Mar 27 '22 11:03 hansenmc

The fix for this issue will be available in the upcoming release.

anu3990 avatar Jul 26 '24 19:07 anu3990

Closing this since fix is now available in version 3.5.0. Thanks.!

anu3990 avatar Aug 15 '24 21:08 anu3990