Skip to content

Commit

Permalink
Merge pull request #13164 from Automattic/vkarpov15/gh-13154
Browse files Browse the repository at this point in the history
fix(schema): propagate typeKey down to implicitly created subdocuments
  • Loading branch information
vkarpov15 authored Mar 14, 2023
2 parents 5e3a4e7 + e5bf393 commit 2fedff1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,15 @@ Schema.prototype.add = function add(obj, prefix) {
if (prefix) {
this.nested[prefix.substring(0, prefix.length - 1)] = true;
}
const _schema = new Schema(_typeDef);
const schemaWrappedPath = Object.assign({}, val, { type: _schema });

const childSchemaOptions = {};
if (this._userProvidedOptions.typeKey) {
childSchemaOptions.typeKey = this._userProvidedOptions.typeKey;
}

const _schema = new Schema(_typeDef, childSchemaOptions);
_schema.$implicitlyCreated = true;
const schemaWrappedPath = Object.assign({}, val, { [typeKey]: _schema });
this.path(prefix + key, schemaWrappedPath);
} else {
// Either the type is non-POJO or we interpret it as Mixed anyway
Expand Down Expand Up @@ -1322,6 +1329,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
} else if (Schema.Types.DocumentArray.defaultOptions._id != null) {
childSchemaOptions._id = Schema.Types.DocumentArray.defaultOptions._id;
}

const childSchema = new Schema(castFromTypeKey, childSchemaOptions);
childSchema.$implicitlyCreated = true;
return new MongooseTypes.DocumentArray(path, childSchema, obj);
Expand Down Expand Up @@ -1365,7 +1373,6 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
}

if (type && type.instanceOfSchema) {

return new MongooseTypes.Subdocument(type, path, obj);
}

Expand Down
18 changes: 18 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,24 @@ describe('schema', function() {
assert.deepEqual(s.options.toJSON, { virtuals: true });
assert.deepEqual(s.options.toObject, { getters: true });
});

it('propagates typeKey down to implicitly created single nested schemas (gh-13154)', function() {
const TestSchema = {
action: {
$type: {
type: {
$type: String,
required: true
}
},
required: true
}
};
const s = new Schema(TestSchema, { typeKey: '$type' });
assert.equal(s.path('action').constructor.name, 'SubdocumentPath');
assert.ok(s.path('action').schema.$implicitlyCreated);
assert.equal(s.path('action.type').constructor.name, 'SchemaString');
});
});

describe('property names', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/types/querycursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Test.find().cursor().
expectType<Types.ObjectId>(doc._id);
expectType<number>(i);
}).
then(() => console.log('Done!'));
then(() => console.log('Done!'));

0 comments on commit 2fedff1

Please sign in to comment.