Skip to content

Commit 369ee68

Browse files
committed
MLE-21460 : ResultProvider.result() sometimes returns a text node as a Buffer
1 parent 219472f commit 369ee68

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

lib/responder.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,14 @@ function resolvedPromise(operation, resolve) {
11211121
var data = operation.data;
11221122
var hasData = (data != null);
11231123
var dataLen = hasData ? data.length : null;
1124+
if(hasData && dataLen) {
1125+
for(let i=0; i<data.length; i++){
1126+
if(data[i] && data[i].value &&
1127+
data[i].format === 'text' && Buffer.isBuffer(data[i].value)){
1128+
data[i].value = data[i].value.toString();
1129+
}
1130+
}
1131+
}
11241132

11251133
operation.logger.debug('promise resolving with '+(
11261134
hasData ? (dataLen+' chunks of') : 'null'

test-basic/documents-read-xqy-docs.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2025 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
let testconfig = require('../etc/test-config.js');
18+
let should = require('should');
19+
let marklogic = require('../');
20+
let db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
21+
let uris = ['/hello.xqy', '/hello-2.xqy', '/test-doc.txt', '/test-doc.json'];
22+
23+
describe('document-read-xqy-documents', function() {
24+
this.timeout(5000);
25+
before(function(done){
26+
db.documents.write({
27+
uri: uris[0],
28+
contentType: 'application/vnd.marklogic-xdmp',
29+
content: 'Hello, world! in hello.xqy'
30+
})
31+
.result(()=>{
32+
db.documents.write({
33+
uri: uris[1],
34+
contentType: 'application/vnd.marklogic-xdmp',
35+
content: 'Hello, world! in hello-2.xqy'
36+
}).result(()=>{
37+
db.documents.write({
38+
uri: uris[2],
39+
contentType: 'text/plain',
40+
content: 'Hello, world! in test-doc.txt'
41+
}).result(()=> {
42+
db.documents.write({
43+
uri: uris[3],
44+
contentType: 'application/json',
45+
content: '{"key":"value"}'
46+
}).result(()=> done())
47+
})
48+
})
49+
})
50+
.catch(error=> done(error));
51+
});
52+
53+
after(function(done){
54+
db.documents.remove(uris)
55+
.result(function(response){
56+
done();
57+
})
58+
.catch(err=> done(err));
59+
});
60+
61+
it('should read single xqy document and return content in string', done => {
62+
63+
db.eval(`cts.doc('/hello.xqy')`).result(res=>{
64+
res[0].value.should.equal('Hello, world! in hello.xqy')
65+
}).then(()=> done())
66+
.catch(error=> done(error))
67+
});
68+
69+
it('should read multiple xqy documents and return content in string', done => {
70+
71+
db.eval(`Sequence.from([cts.doc('/hello.xqy'), cts.doc('/hello-2.xqy')])`).result(res=>{
72+
res[0].value.should.equal('Hello, world! in hello.xqy');
73+
res[1].value.should.equal('Hello, world! in hello-2.xqy');
74+
}).then(()=> done())
75+
.catch(error=> done(error))
76+
});
77+
78+
it('should read xqy document along with text document and return content in string', done => {
79+
80+
db.eval(`Sequence.from([cts.doc('/hello.xqy'), cts.doc('/test-doc.txt')])`).result(res=>{
81+
res[0].value.should.equal('Hello, world! in hello.xqy');
82+
res[1].value.should.equal('Hello, world! in test-doc.txt');
83+
}).then(()=> done())
84+
.catch(error=> done(error))
85+
});
86+
87+
it('should read multiple xqy documents along with json document and return content in string', done => {
88+
89+
db.eval(`Sequence.from([cts.doc('/hello.xqy'), cts.doc('/hello-2.xqy'), cts.doc('/test-doc.json')])`).result(res=>{
90+
res[0].value.should.equal('Hello, world! in hello.xqy');
91+
res[1].value.should.equal('Hello, world! in hello-2.xqy');
92+
res[2].value.should.have.property('key');
93+
res[2].value.key.should.equal('value');
94+
}).then(()=> done())
95+
.catch(error=> done(error))
96+
});
97+
});

0 commit comments

Comments
 (0)