mongoosejs / mongoose-lean-getters

Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
Apache License 2.0
10 stars 15 forks source link

fix: correctly get schema for each element of a discriminated array #28

Closed nathan-knight closed 8 months ago

nathan-knight commented 1 year ago

Also do not attempt to get the schema for null/undefined docs

Summary

After updating the package to the latest version (1.1.0) it started failing when an empty array contained discriminated documents. It would get the child schema using the array as the document instead of an element of the array so it would end up with an undefined schema.

With this change it now gets the schema for each document in the array (unless it is null/undefined) and will also not break if a schema has a discriminator mapping key but no discriminators object (which happens when the schema is configured the way it is in the test).

Examples I have included a new test, should work on schemas with discriminators in arrays, that failed before but passes now.