Skip to content

Commit

Permalink
Merge branch 'master' into vkarpov15/gh-14935
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored Oct 8, 2024
2 parents 47c3b46 + ee6b861 commit 8b2b4e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
matrix:
node: [16, 18, 20]
os: [ubuntu-20.04, ubuntu-22.04]
mongodb: [4.4.29, 5.0.26, 6.0.15, 7.0.12]
mongodb: [4.4.29, 5.0.26, 6.0.15, 7.0.12, 8.0.0]
include:
- os: ubuntu-20.04 # customize on which matrix the coverage will be collected on
mongodb: 5.0.26
Expand Down
1 change: 1 addition & 0 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Below are the [semver](http://semver.org/) ranges representing which versions of

| MongoDB Server | Mongoose |
| :------------: | :-------------------------------------: |
| `8.x` | `^8.7.0` |
| `7.x` | `^7.4.0 \| ^8.0.0` |
| `6.x` | `^6.5.0 \| ^7.0.0 \| ^8.0.0` |
| `5.x` | `^5.13.0` \| `^6.0.0 \| ^7.0.0 \| ^8.0.0`|
Expand Down
8 changes: 8 additions & 0 deletions lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
return ret;
}

if (utils.isPOJO(value) && (value.$binary instanceof Binary || typeof value.$binary === 'string')) {
const buf = this.cast(Buffer.from(value.$binary, 'base64'));
if (value.$type != null) {
buf._subtype = value.$type;
return buf;
}
}

throw new CastError('Buffer', value, this.path, null, this);
};

Expand Down
29 changes: 29 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13952,6 +13952,35 @@ describe('document', function() {
const doc = await Test.findOne({ professionalId }).lean().orFail();
assert.ok(doc.professionalId instanceof mongoose.Types.ObjectId);
});

Check failure on line 13955 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 13955 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
it('handles buffers stored as EJSON POJO (gh-14911)', async function() {
const pdfSchema = new mongoose.Schema({
pdfSettings: {
type: {
_id: false,
fileContent: { type: Buffer, required: true },
filePreview: { type: Buffer, required: true },
fileName: { type: String, required: true }
}
}
});
const PdfModel = db.model('Test', pdfSchema);

const _id = new mongoose.Types.ObjectId();
const buf = { $binary: Buffer.from('hello', 'utf8').toString('base64'), $type: '00' };
await PdfModel.collection.insertOne({
_id,
pdfSettings: {
fileContent: buf,
filePreview: buf,
fileName: 'sample.pdf'
}
});

const reloaded = await PdfModel.findById(_id);
assert.ok(Buffer.isBuffer(reloaded.pdfSettings.fileContent));
assert.strictEqual(reloaded.pdfSettings.fileContent.toString('utf8'), 'hello');
});
});

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

0 comments on commit 8b2b4e6

Please sign in to comment.