Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: array schema definitions with of keyword #14447

Merged
merged 1 commit into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/schema.js
Expand Up @@ -1268,6 +1268,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
return clone;
}


// If this schema has an associated Mongoose object, use the Mongoose object's
// copy of SchemaTypes re: gh-7158 gh-6933
const MongooseTypes = this.base != null ? this.base.Schema.Types : Schema.Types;
Expand Down Expand Up @@ -1333,9 +1334,13 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
}
return new MongooseTypes.DocumentArray(path, cast[options.typeKey], obj, cast);
}

if (Array.isArray(cast)) {
return new MongooseTypes.Array(path, this.interpretAsType(path, cast, options), obj);
if (typeof cast !== 'undefined') {
if (Array.isArray(cast) || cast.type === Array || cast.type == 'Array') {
if (cast && cast.type == 'Array') {
cast.type = Array;
}
return new MongooseTypes.Array(path, this.interpretAsType(path, cast, options), obj);
}
}

// Handle both `new Schema({ arr: [{ subpath: String }] })` and `new Schema({ arr: [{ type: { subpath: string } }] })`
Expand Down Expand Up @@ -1386,7 +1391,6 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
type = cast[options.typeKey] && (options.typeKey !== 'type' || !cast.type.type)
? cast[options.typeKey]
: cast;

if (Array.isArray(type)) {
return new MongooseTypes.Array(path, this.interpretAsType(path, type, options), obj);
}
Expand Down
10 changes: 10 additions & 0 deletions test/schema.test.js
Expand Up @@ -3202,4 +3202,14 @@ describe('schema', function() {
const doc = new baseModel({ type: 1, self: [{ type: 1 }] });
assert.equal(doc.self[0].type, 1);
});
it('should have the correct schema definition with array schemas (gh-14416)', function() {
const schema = new Schema({
nums: [{ type: Array, of: Number }],
tags: [{ type: 'Array', of: String }],
subdocs: [{ type: Array, of: Schema({ name: String }) }]
});
assert.equal(schema.path('nums.$').caster.instance, 'Number'); // actually Mixed
assert.equal(schema.path('tags.$').caster.instance, 'String'); // actually Mixed
assert.equal(schema.path('subdocs.$').casterConstructor.schema.path('name').instance, 'String'); // actually Mixed
Comment on lines +3211 to +3213

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove these comments now.

Suggested change
assert.equal(schema.path('nums.$').caster.instance, 'Number'); // actually Mixed
assert.equal(schema.path('tags.$').caster.instance, 'String'); // actually Mixed
assert.equal(schema.path('subdocs.$').casterConstructor.schema.path('name').instance, 'String'); // actually Mixed
assert.equal(schema.path('nums.$').caster.instance, 'Number');
assert.equal(schema.path('tags.$').caster.instance, 'String');
assert.equal(schema.path('subdocs.$').casterConstructor.schema.path('name').instance, 'String');

});
});