Skip to content

Inconsistency in response types creates unreliable bulk insert operations #19

@JustinShift

Description

@JustinShift

Package version

v3.0.2

Describe the bug

The processBulkInsert() method in Query/Processor.php fails with Call to a member function asArray() on array during bulk insert operations.

The error occurs because the method expects an object with an asArray() method but receives a plain array instead, creating an inconsistent response type between regular inserts and bulk inserts.

To Reproduce

Steps to reproduce the behavior:

  1. Set up a Laravel application with the OpenSearch package
  2. Perform bulk insert operations with a large dataset
  3. Use the bulkInsert() method on a query builder instance

EG:

$query->bulkInsert($largeBatchOfDocuments);

Stack trace:

Call to a member function asArray() on array at /vendor/pdphilip/opensearch/src/Query/Processor.php:432
PDPhilip\OpenSearch\Query\Processor->processBulkInsert()

Expected behavior

Bulk insert operations should handle response types consistently and not fail with type errors. The processBulkInsert() method should successfully process both array and object responses from the connection layer, similar to how processInsert() handles array responses.

Component Versions

  "require": {
    "php": "^8.3",
    "laravel/framework": "^10.0",
    "pdphilip/opensearch": "^3.0"
  },

Additional context:

The issue stems from inconsistent response type expectations between methods:

  • processInsert() (line 424) expects an array: $this->getRawResponse()['errors']
  • processBulkInsert() (line 432) expects an object: $result->asArray()

Both methods receive their $result parameter from the same $this->connection->insert() method but with different parameters:

// Regular insert (Builder.php:685)

$this->connection->insert($this->grammar->compileInsert($this, $values))

// Bulk insert (Builder.php:693)

$this->connection->insert($this->grammar->compileInsert($this, $values), [], true)

The third parameter true appears to affect the response type inconsistently.

Suggested Fix:

Add type checking in processBulkInsert() to handle both response types:

public function processBulkInsert(Builder $query, $result): array
{
    $this->rawResponse = $result;
    $this->query = $query;

    // Handle both array and object responses for consistency
    $process = is_array($result) ? $result : $result->asArray();

    $outcome = [
        'hasErrors' => $process['errors'],
        // ... rest of method unchanged
    ];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions