Skip to content

Commit 8b2b4e6

Browse files
authored
Merge branch 'master' into vkarpov15/Automatticgh-14935
2 parents 47c3b46 + ee6b861 commit 8b2b4e6

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
matrix:
4242
node: [16, 18, 20]
4343
os: [ubuntu-20.04, ubuntu-22.04]
44-
mongodb: [4.4.29, 5.0.26, 6.0.15, 7.0.12]
44+
mongodb: [4.4.29, 5.0.26, 6.0.15, 7.0.12, 8.0.0]
4545
include:
4646
- os: ubuntu-20.04 # customize on which matrix the coverage will be collected on
4747
mongodb: 5.0.26

docs/compatibility.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Below are the [semver](http://semver.org/) ranges representing which versions of
1818

1919
| MongoDB Server | Mongoose |
2020
| :------------: | :-------------------------------------: |
21+
| `8.x` | `^8.7.0` |
2122
| `7.x` | `^7.4.0 \| ^8.0.0` |
2223
| `6.x` | `^6.5.0 \| ^7.0.0 \| ^8.0.0` |
2324
| `5.x` | `^5.13.0` \| `^6.0.0 \| ^7.0.0 \| ^8.0.0`|

lib/schema/buffer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
219219
return ret;
220220
}
221221

222+
if (utils.isPOJO(value) && (value.$binary instanceof Binary || typeof value.$binary === 'string')) {
223+
const buf = this.cast(Buffer.from(value.$binary, 'base64'));
224+
if (value.$type != null) {
225+
buf._subtype = value.$type;
226+
return buf;
227+
}
228+
}
229+
222230
throw new CastError('Buffer', value, this.path, null, this);
223231
};
224232

test/document.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13952,6 +13952,35 @@ describe('document', function() {
1395213952
const doc = await Test.findOne({ professionalId }).lean().orFail();
1395313953
assert.ok(doc.professionalId instanceof mongoose.Types.ObjectId);
1395413954
});
13955+
13956+
it('handles buffers stored as EJSON POJO (gh-14911)', async function() {
13957+
const pdfSchema = new mongoose.Schema({
13958+
pdfSettings: {
13959+
type: {
13960+
_id: false,
13961+
fileContent: { type: Buffer, required: true },
13962+
filePreview: { type: Buffer, required: true },
13963+
fileName: { type: String, required: true }
13964+
}
13965+
}
13966+
});
13967+
const PdfModel = db.model('Test', pdfSchema);
13968+
13969+
const _id = new mongoose.Types.ObjectId();
13970+
const buf = { $binary: Buffer.from('hello', 'utf8').toString('base64'), $type: '00' };
13971+
await PdfModel.collection.insertOne({
13972+
_id,
13973+
pdfSettings: {
13974+
fileContent: buf,
13975+
filePreview: buf,
13976+
fileName: 'sample.pdf'
13977+
}
13978+
});
13979+
13980+
const reloaded = await PdfModel.findById(_id);
13981+
assert.ok(Buffer.isBuffer(reloaded.pdfSettings.fileContent));
13982+
assert.strictEqual(reloaded.pdfSettings.fileContent.toString('utf8'), 'hello');
13983+
});
1395513984
});
1395613985

1395713986
describe('Check if instance function that is supplied in schema option is available', function() {

0 commit comments

Comments
 (0)