Skip to content

AdminBro crashes when saving record with an empty array #352

@RiledUpCrow

Description

@RiledUpCrow

Steps to reproduce:

  1. Install AdminBro with Express and Mongoose adapters
  2. Have resource with an array of objects (let's call them Foo) as one of the properties. Those nested objects have to contain an array of objects (let's call them Bar) as one of their properties.
  3. Click "Create new" button (new action).
  4. Fill the form with some data, including one Foo object in the array.
  5. Fill the Foo object with some data, including one Bar object in the array.
  6. Remove all Bar objects from the array. (This is what triggers the bug. If the Bar array was empty and the user wouldn't touch it, the bug would not appear. Adding and removing items triggers the bug.)
  7. Save the record. AdminBro crashes the whole application. Not just the request, the whole app goes down.

Stack trace:

/home/user/Projects/some-project/node_modules/mongoose/lib/error/objectParameter.js:25
    Error.captureStackTrace(this);
          ^
ObjectParameterError: Parameter "obj" to Document() must be an object, got 
    at new ObjectParameterError (/home/user/Projects/some-project/node_modules/mongoose/lib/error/objectParameter.js:25:11)
    at EmbeddedDocument.Document (/home/user/Projects/some-project/node_modules/mongoose/lib/document.js:87:11)
    at EmbeddedDocument [as constructor] (/home/user/Projects/some-project/node_modules/mongoose/lib/types/embedded.js:42:12)
    at new EmbeddedDocument (/home/user/Projects/some-project/node_modules/mongoose/lib/schema/documentarray.js:116:17)
    at cb (/home/user/Projects/some-project/node_modules/mongoose/lib/schema/documentarray.js:252:26)
    at DocumentArrayPath.SchemaType.doValidate (/home/user/Projects/some-project/node_modules/mongoose/lib/schematype.js:1046:12)
    at DocumentArrayPath.doValidate (/home/user/Projects/some-project/node_modules/mongoose/lib/schema/documentarray.js:201:37)
    at /home/user/Projects/some-project/node_modules/mongoose/lib/document.js:2323:9
    at process._tickCallback (internal/process/next_tick.js:61:11)

Example object Foo:

interface Foo {
  name: string;
  nested: Bar;
}

interface Foo {
  title: string;
  bugs: Baz; // this is the part triggering the bug
}

interface Baz {
  anything: number;
}

More info:

The bug is being caused by two things: when we add and remove the object, request form/multipart made by the frontend will contain a field like this:

nested.0.bugs:

Notice that it has no value. If we haven't added and then removed objects from Foo then this field would not appear at all. That's the second reason for the bug - backend tries to parse that valueless field and Mongoose crashes.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions