Skip to content

Nested arrays using of result in the incorrect schema type #14416

Closed
@forivall

Description

@forivall

Prerequisites

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

Mongoose version

7.6.9

Node.js version

20.6.1

MongoDB server version

5.x

Typescript version (if applicable)

5.2.2

Description

With a schema definition of

    const schema = new Schema({
      nums: [{ type: Array, of: Number }],
      tags: [{ type: 'Array', of: String }],
      subdocs: [{ type: Array, of: Schema({ name: String }) }]
    });

the resulting casters result in Mixed rather than the appropriate type.

    assert.equal(schema.path('nums.$').caster.instance, 'Number'); // actually Mixed
    assert.equal(schema.path('tags.$').caster.instance, 'String'); // actually Mixed
    assert.equal(schema.path('subdocs.$').casterConstructor.schema.path('name').instance, 'String'); // casterConstructor is actually a Mixed instance

Steps to Reproduce

See my updated tests at master...forivall:mongoose:forivall/nested-array-of

.patch
From fe53ac241a266afa08a1e5963e7ff53eac18c469 Mon Sep 17 00:00:00 2001
From: Emily M Klassen <[email protected]>
Date: Tue, 5 Mar 2024 17:50:52 -0800
Subject: [PATCH] test: add test for nested array of issue

---
 test/schema.test.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/test/schema.test.js b/test/schema.test.js
index 0df19a65790..d57fb8e528f 100644
--- a/test/schema.test.js
+++ b/test/schema.test.js
@@ -2672,6 +2672,18 @@ describe('schema', function() {
     assert.equal(schema.path('subdocs').casterConstructor.schema.path('name').instance, 'String');
   });
 
+  it('supports `of` for nested array type definition', function() {
+    const schema = new Schema({
+      nums: [{ type: Array, of: Number }],
+      tags: [{ type: 'Array', of: String }],
+      subdocs: [{ type: Array, of: Schema({ name: String }) }]
+    });
+
+    assert.equal(schema.path('nums.$').caster.instance, 'Number');
+    assert.equal(schema.path('tags.$').caster.instance, 'String');
+    assert.equal(schema.path('subdocs.$').casterConstructor.schema.path('name').instance, 'String');
+  });
+
   it('should use the top-most class\'s getter/setter gh-8892', function() {
     class C1 {
       get hello() {

Expected Behavior

The casters should be the correct schema types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions