@@ -9239,6 +9239,49 @@ describe('document', function() {
9239
9239
assert . ok ( foo . isModified ( 'subdoc.bar' ) ) ;
9240
9240
} ) ;
9241
9241
9242
+ it ( 'does not unmark modified if there is no initial value (gh-9396)' , async function ( ) {
9243
+ const IClientSchema = new Schema ( {
9244
+ jwt : {
9245
+ token_crypt : { type : String , template : false , maxSize : 8 * 1024 } ,
9246
+ token_salt : { type : String , template : false }
9247
+ }
9248
+ } ) ;
9249
+
9250
+ const encrypt = function ( doc , path , value ) {
9251
+ doc . set ( path + '_crypt' , value + '_crypt' ) ;
9252
+ doc . set ( path + '_salt' , value + '_salt' ) ;
9253
+ } ;
9254
+
9255
+ const decrypt = function ( doc , path ) {
9256
+ return doc . get ( path + '_crypt' ) . replace ( '_crypt' , '' ) ;
9257
+ } ;
9258
+
9259
+ IClientSchema . virtual ( 'jwt.token' )
9260
+ . get ( function ( ) {
9261
+ return decrypt ( this , 'jwt.token' ) ;
9262
+ } )
9263
+ . set ( function ( value ) {
9264
+ encrypt ( this , 'jwt.token' , value ) ;
9265
+ } ) ;
9266
+
9267
+
9268
+ const iclient = db . model ( 'Test' , IClientSchema ) ;
9269
+ const test = new iclient ( {
9270
+ jwt : {
9271
+ token : 'firstToken'
9272
+ }
9273
+ } ) ;
9274
+
9275
+ await test . save ( ) ;
9276
+ const entry = await iclient . findById ( test . _id ) . orFail ( ) ;
9277
+ entry . set ( 'jwt.token' , 'secondToken' ) ;
9278
+ entry . set ( entry . toJSON ( ) ) ;
9279
+ await entry . save ( ) ;
9280
+
9281
+ const { jwt } = await iclient . findById ( test . _id ) . orFail ( ) ;
9282
+ assert . strictEqual ( jwt . token , 'secondToken' ) ;
9283
+ } ) ;
9284
+
9242
9285
it ( 'correctly tracks saved state for deeply nested objects (gh-10773) (gh-9396)' , async function ( ) {
9243
9286
const PaymentSchema = Schema ( { status : String } , { _id : false } ) ;
9244
9287
const OrderSchema = new Schema ( {
0 commit comments