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

Cannot set strictQuery: false globally and have an embedded discriminator #12796

Closed
2 tasks done
JavaScriptBach opened this issue Dec 14, 2022 · 4 comments · Fixed by #12827
Closed
2 tasks done

Cannot set strictQuery: false globally and have an embedded discriminator #12796

JavaScriptBach opened this issue Dec 14, 2022 · 4 comments · Fixed by #12827
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@JavaScriptBach
Copy link
Contributor

JavaScriptBach commented Dec 14, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.8.0

Node.js version

16.17.1

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

Running the following code:

const mongoose = require('mongoose');

mongoose.set("strictQuery", false);

const testSchema = new mongoose.Schema({
  test: [{foo: String}]
});

testSchema.path("test").discriminator("Bar", new mongoose.Schema({ bar: String }));

console.log('done');

Fails with:

Error: Can't customize discriminator option strictQuery (can only modify toJSON, toObject, _id, id, virtuals, methods)
    at merge (/.../node_modules/mongoose/lib/helpers/model/discriminator.js:174:17)
    at discriminator (/.../node_modules/mongoose/lib/helpers/model/discriminator.js:199:3)
    at DocumentArrayPath.discriminator (/.../node_modules/mongoose/lib/schema/documentarray.js:205:12)
...irrelevant stacks omitted

Steps to Reproduce

See above

Expected Behavior

I should be able to set strictQuery: false globally, but I can't because something about this implementation is broken with schemas that create embedded array discriminators.

@JavaScriptBach
Copy link
Contributor Author

A hacky workaround is to add strictQuery: true to https://github.com/Automattic/mongoose/blob/master/lib/helpers/model/discriminator.js#L9, but that's likely not the correct long-term approach.

@IslandRhythms
Copy link
Collaborator

Update to latest and see if that resolves your issue

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  test: [{foo: String}]
});

testSchema.path("test").discriminator("Bar", new mongoose.Schema({ bar: String }));

console.log('done');

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Dec 19, 2022
@JavaScriptBach
Copy link
Contributor Author

JavaScriptBach commented Dec 19, 2022

Issue is still there on latest. You need to add mongoose.set("strictQuery", false) to repro it. So something like:

const mongoose = require('mongoose');

mongoose.set("strictQuery", false);

const testSchema = new mongoose.Schema({
  test: [{foo: String}]
});

testSchema.path("test").discriminator("Bar", new mongoose.Schema({ bar: String }));

console.log('done');

@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
mongoose.set('strictQuery', false);
const testSchema = new mongoose.Schema({
  test: [{foo: String}]
});

testSchema.path("test").discriminator("Bar", new mongoose.Schema({ bar: String }));

console.log('done');

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Dec 19, 2022
@vkarpov15 vkarpov15 added this to the 6.8.2 milestone Dec 22, 2022
vkarpov15 added a commit that referenced this issue Dec 23, 2022
vkarpov15 added a commit that referenced this issue Dec 26, 2022
fix(schema): propagate strictQuery to implicitly created schemas for embedded discriminators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants