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

feat(subdocument): support schematype-level minimize option to disable minimizing empty subdocuments #15336

Merged
merged 4 commits into from
Apr 6, 2025

Conversation

vkarpov15
Copy link
Collaborator

Fix #15313

Summary

In #11247, we made it so that Mongoose would minimize empty subdocuments by default. However, there isn't a great way to configure that behavior right now: #14151 disabled that behavior for required subdocs, but you might not want to make the field required.

This PR lets you set a minimize option on the schematype itself, like subdoc: { type: Schema, minimize: false } to disable minimize on the full subdocument. Setting minimize: false on the schematype will also override the child schema's minimize.

Examples

@vkarpov15 vkarpov15 added this to the 8.14 milestone Apr 1, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for a schematype-level minimize option on subdocuments to disable the default minimizing behavior for empty subdocuments.

  • Adds tests ensuring that an empty subdocument and one with an empty child are retained when minimize is false.
  • Overrides the subdocument’s $toObject method to check for schematype-level minimize settings.
  • Removes redundant minimize logic from the clone helper to avoid conflicts with the new behavior.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/types.subdocument.test.js Adds test cases for schematype-level minimize option behavior
lib/types/subdocument.js Modifies $toObject to respect the schematype-level minimize flag
lib/schema/subdocument.js Sets schema type options on subdocument prototypes
lib/helpers/clone.js Removes redundant logic for minimizing empty subdocuments within cloning
lib/document.js Adjusts toObject implementation to incorporate schematype minimize settings
Comments suppressed due to low confidence (1)

lib/helpers/clone.js:70

  • The removal of the minimization check in the clone function may lead to inconsistent behavior compared to the updated $toObject logic. Please verify that this change is intentional and that cloning subdocuments with the minimize option set behaves as expected.
if (options && options.minimize && !obj.constructor.$__required && isSingleNested && Object.keys(ret).length === 0) {

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

LGTM, though i think this option still needs to be added to SchemaSubDocumentOptions (at least i did not see it in the chain yet)

@vkarpov15
Copy link
Collaborator Author

Fixed in 5853a8b

@vkarpov15 vkarpov15 changed the base branch from master to 8.14 April 6, 2025 18:53
@vkarpov15 vkarpov15 merged commit 7e7dc1a into 8.14 Apr 6, 2025
62 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15313 branch April 7, 2025 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"minimize: false" is not working for it's own field
2 participants