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

Missing breaking change in v6 changelog: schema no longer inherits minimize option #10827

Closed
ZhangYiJiang opened this issue Oct 1, 2021 · 5 comments
Assignees
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@ZhangYiJiang
Copy link

ZhangYiJiang commented Oct 1, 2021

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

In v5, the minimize option seem to be inherited by child schema. This is no longer the case in v6. This should be considered at least a bug in the changelog

v5 -

> const { Schema, model, version } = require('mongoose');
> console.log(version);
5.9.29
> const child = new Schema({ thing: Schema.Types.Mixed });
> const parent = new Schema({ child }, { minimize: false });
> const Parent = model('Parent', parent);
> const p = new Parent({ child: { thing: {} } });
> console.log(p.toObject());
{
  _id: 615670e7ce0fe77733511fa8,
  child: { _id: 615670e7ce0fe77733511fa9, thing: {} }
}

v6 -

> const { Schema, model, version } = require('mongoose');
> console.log(version);
6.0.6
> const child = new Schema({ thing: Schema.Types.Mixed });
> const parent = new Schema({ child }, { minimize: false });
> const Parent = model('Parent', parent);
> const p = new Parent({ child: { thing: {} } });
> console.log(p.toObject());
{
  child: { _id: new ObjectId("615670bbd7853d32111ae88e") },
  _id: new ObjectId("615670bbd7853d32111ae88d")
}

For easier copy-pasting -

const { Schema, model, version } = require('mongoose');
console.log(version);
const child = new Schema({ thing: Schema.Types.Mixed });
const parent = new Schema({ child }, { minimize: false });
const Parent = model('Parent', parent);
const p = new Parent({ child: { thing: {} } });
console.log(p.toObject());

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

@IslandRhythms
Copy link
Collaborator

So on newer versions of 5 thing does not show up.

@vkarpov15 vkarpov15 added this to the 6.0.13 milestone Oct 2, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed seen labels Oct 24, 2021
@vkarpov15 vkarpov15 assigned IslandRhythms and unassigned vkarpov15 Oct 24, 2021
@IslandRhythms IslandRhythms linked a pull request Nov 3, 2021 that will close this issue
@vkarpov15
Copy link
Collaborator

I took a closer look and this is expected behavior as of v5.10.5 with #9405. This is not a change in v6. As a workaround, you can either explicitly set minimize by calling toObject({ minimize: false }), or, in 6.0, you can define schemas inline (without new Schema()) so the schema inherits its parent's minimize:

const { Schema, model, version } = require('mongoose');
console.log(version);
const parent = new Schema({ child: { thing: Schema.Types.Mixed } }, { minimize: false });
const Parent = model('Parent', parent);
const p = new Parent({ child: { thing: {} } });
console.log(p.toObject()); // Contains `thing` in v6.x

@vkarpov15 vkarpov15 removed this from the 6.0.13 milestone Nov 10, 2021
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Nov 10, 2021
@nomcopter
Copy link

nomcopter commented Nov 10, 2021

Sure, if it is expected behavior then this is instead an undocumented breaking change introduced in a patch version.

@vkarpov15
Copy link
Collaborator

@nomcopter correct. This change was meant to be a bug fix, but sometimes bug fixes break code :( Do you need any help getting a workaround or do you just think there should be a note in the 6.0 changelog?

@nomcopter
Copy link

@vkarpov15 a note in the 6.0 changelog should cover it well at this point!

@vkarpov15 vkarpov15 reopened this Nov 21, 2021
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Nov 21, 2021
@vkarpov15 vkarpov15 added this to the 6.0.14 milestone Nov 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants