Skip to content

Commit ae2445c

Browse files
committed
Fix check whether array is associative
1 parent 8d855b3 commit ae2445c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Result/DocumentConverter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function assignArrayToObject(array $array, ObjectInterface $object, array
117117
} elseif (in_array($propertyMetadata['type'], ['object', 'nested'])) {
118118
// ES doesn't mind having either single or multiple objects with the same mapping, but in this bundle we must specifically declare either.
119119
// So we must make sure everything works for a 'multiple' definition where we actually have a single object and vice versa.
120-
if ($propertyMetadata['multiple'] && key($array[$esField]) !== 0) {
120+
if ($propertyMetadata['multiple'] && is_string(key($array[$esField]))) {
121121
// field is declared multiple, but actual data is single object
122122
$data = [$array[$esField]];
123123
} elseif (!$propertyMetadata['multiple'] && key($array[$esField]) === 0) {

Tests/Functional/Result/DocumentConverterTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sineflow\ElasticsearchBundle\Document\MLProperty;
66
use Sineflow\ElasticsearchBundle\Exception\DocumentConversionException;
7+
use Sineflow\ElasticsearchBundle\Result\ObjectIterator;
78
use Sineflow\ElasticsearchBundle\Tests\AbstractContainerAwareTestCase;
89
use Sineflow\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\ObjCategory;
910
use Sineflow\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\Product;
@@ -156,6 +157,25 @@ public function testAssignArrayToObjectWithEmptyFields()
156157
$this->assertNull($product->mlInfo);
157158
}
158159

160+
public function testAssignArrayToObjectWithEmptyMultipleNestedField()
161+
{
162+
$converter = $this->getContainer()->get('sfes.document_converter');
163+
$metadataCollector = $this->getContainer()->get('sfes.document_metadata_collector');
164+
165+
$rawDoc = [
166+
'related_categories' => [],
167+
];
168+
169+
$product = new Product();
170+
$converter->assignArrayToObject(
171+
$rawDoc,
172+
$product,
173+
$metadataCollector->getDocumentMetadata('AcmeBarBundle:Product')->getPropertiesMetadata()
174+
);
175+
$this->assertInstanceOf(ObjectIterator::class, $product->relatedCategories);
176+
$this->assertSame(0, $product->relatedCategories->count());
177+
}
178+
159179
/**
160180
* @depends testAssignArrayToObjectWithAllFieldsCorrectlySet
161181
*/

Tests/app/config/config_test.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
parameters:
22
elasticsearch_hosts:
3-
- '127.0.0.1:9200'
3+
- '192.168.56.115:9200'
4+
# - '127.0.0.1:9200'
45

56
# Framework Configuration
67
framework:

0 commit comments

Comments
 (0)