{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/2990","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/2990/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/2990/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/2990/events","html_url":"https://github.com/Automattic/mongoose/issues/2990","id":77026247,"number":2990,"title":"toJSON on sub-document is not called","user":{"login":"jsedlacek","id":10656,"avatar_url":"https://avatars.githubusercontent.com/u/10656?v=3","gravatar_id":"","url":"https://api.github.com/users/jsedlacek","html_url":"https://github.com/jsedlacek","followers_url":"https://api.github.com/users/jsedlacek/followers","following_url":"https://api.github.com/users/jsedlacek/following{/other_user}","gists_url":"https://api.github.com/users/jsedlacek/gists{/gist_id}","starred_url":"https://api.github.com/users/jsedlacek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jsedlacek/subscriptions","organizations_url":"https://api.github.com/users/jsedlacek/orgs","repos_url":"https://api.github.com/users/jsedlacek/repos","events_url":"https://api.github.com/users/jsedlacek/events{/privacy}","received_events_url":"https://api.github.com/users/jsedlacek/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/42","html_url":"https://github.com/Automattic/mongoose/milestones/3.8.29","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/42/labels","id":1106915,"number":42,"title":"3.8.29","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":6,"closed_issues":1,"state":"open","created_at":"2015-05-12T15:23:17Z","updated_at":"2015-05-20T16:18:53Z","due_on":"2015-05-25T04:00:00Z","closed_at":null},"comments":8,"created_at":"2015-05-16T12:17:37Z","updated_at":"2015-05-20T18:54:26Z","closed_at":"2015-05-20T16:18:53Z","body":"There is a regression in mongoose 4.0.3: parent's `toJSON` is called instead of sub-document's one.\r\n\r\nHere is a test-case:\r\n```js\r\nvar childSchema = new Schema({ name: 'string' });\r\nchildSchema.options.toJSON = {\r\n  transform: function(doc) {\r\n    return doc.name;\r\n  }\r\n};\r\nsinon.spy(childSchema.options.toJSON, \"transform\");\r\n\r\nvar parentSchema = new Schema({\r\n  children: [childSchema]\r\n});\r\nparentSchema.options.toJSON = {\r\n  transform: function(doc, ret, options) {\r\n    delete ret._id;\r\n  }\r\n};\r\nsinon.spy(parentSchema.options.toJSON, \"transform\");\r\n\r\nvar Parent = mongoose.model('Parent', parentSchema);\r\n\r\nvar parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })\r\nvar json = JSON.stringify({parent: parent});\r\n\r\nassert.equal(json, '{\"parent\":{\"children\":[\"Matt\",\"Sarah\"]}}');\r\nassert.equal(parentSchema.options.toJSON.transform.callCount, 1, 'Parrent toJSON called count');\r\nassert.equal(childSchema.options.toJSON.transform.callCount, 2, 'Child toJSON called count');\r\n```\r\n\r\nThis test passes on mongoose 4.0.2, but fails on 4.0.3 with the following result.\r\n```\r\nAssertionError: expected `'{\"parent\":{\"children\":[{\"name\":\"Matt\"},{\"name\":\"Sarah\"}]}}'` to equal `'{\"parent\":{\"children\":[\"Matt\",\"Sarah\"]}}'`\r\n\r\nAssertionError: Parrent toJSON called count: expected 3 to equal 1\r\n\r\nAssertionError: Child toJSON called count: expected 0 to equal 2\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/103994583","html_url":"https://github.com/Automattic/mongoose/issues/2990#issuecomment-103994583","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/2990","id":103994583,"user":{"login":"jsedlacek","id":10656,"avatar_url":"https://avatars.githubusercontent.com/u/10656?v=3","gravatar_id":"","url":"https://api.github.com/users/jsedlacek","html_url":"https://github.com/jsedlacek","followers_url":"https://api.github.com/users/jsedlacek/followers","following_url":"https://api.github.com/users/jsedlacek/following{/other_user}","gists_url":"https://api.github.com/users/jsedlacek/gists{/gist_id}","starred_url":"https://api.github.com/users/jsedlacek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jsedlacek/subscriptions","organizations_url":"https://api.github.com/users/jsedlacek/orgs","repos_url":"https://api.github.com/users/jsedlacek/repos","events_url":"https://api.github.com/users/jsedlacek/events{/privacy}","received_events_url":"https://api.github.com/users/jsedlacek/received_events","type":"User","site_admin":false},"created_at":"2015-05-20T18:54:26Z","updated_at":"2015-05-20T18:54:26Z","body":"Thanks for such a quick fix @vkarpov15!!"}}