Skip to content

Commit b785b8b

Browse files
JSON serialization of arrays with non-consecutive indices in multivalue fields (#1048)
Co-authored-by: Markus Kalkbrenner <git@kalki.de>
1 parent 5c43011 commit b785b8b

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
### Added
8+
### Fixed
9+
- JSON serialization of arrays with non-consecutive indices in multivalue fields
910

1011

1112
## [6.2.8]

src/QueryType/Update/Query/Document.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ public function jsonSerialize()
499499
if ($value instanceof \DateTimeInterface) {
500500
$value = $this->getHelper()->formatDate($value);
501501
} elseif (\is_array($value) && is_numeric(array_key_first($value))) {
502+
// ensure consecutive indices so it doesn't serialize to an object
503+
$value = array_values($value);
504+
502505
foreach ($value as &$multivalue) {
503506
if ($multivalue instanceof \DateTimeInterface) {
504507
$multivalue = $this->getHelper()->formatDate($multivalue);

tests/QueryType/Update/RequestBuilder/JsonTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ public function testBuildAddJsonMultivalueFieldWithEmptyArray()
231231
);
232232
}
233233

234+
public function testBuildAddJsonMultivalueFieldWithNonConsecutiveArrayIndices()
235+
{
236+
$command = new AddCommand();
237+
$command->addDocument(new Document(['id' => [0 => 1, 4 => 2, 6 => 3], 'text' => [1 => 'a', 2 => 'b', 3 => 'c']]));
238+
$json = [];
239+
240+
$this->builder->buildAddJson($command, $json);
241+
242+
$this->assertCount(1, $json);
243+
$this->assertJsonStringEqualsJsonString(
244+
'{
245+
"add": {
246+
"doc": {
247+
"id": [1, 2, 3],
248+
"text": ["a", "b", "c"]
249+
}
250+
}
251+
}',
252+
'{'.$json[0].'}'
253+
);
254+
}
255+
234256
public function testBuildAddJsonWithEmptyStrings()
235257
{
236258
$command = new AddCommand();
@@ -662,7 +684,7 @@ public function testBuildAddJsonWithDateTimeImmutable()
662684
);
663685
}
664686

665-
public function testBuildAddJsonWithMultivaluedDateTimes()
687+
public function testBuildAddJsonWithMultivalueDateTimes()
666688
{
667689
$command = new AddCommand();
668690
$command->addDocument(

tests/QueryType/Update/RequestBuilder/XmlTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ public function testBuildAddXmlMultivalueFieldWithEmptyArray()
174174
);
175175
}
176176

177+
public function testBuildAddXmlMultivalueFieldWithNonConsecutiveArrayIndices()
178+
{
179+
$command = new AddCommand();
180+
$command->addDocument(new Document(['id' => [0 => 1, 4 => 2, 6 => 3], 'text' => [1 => 'a', 2 => 'b', 3 => 'c']]));
181+
182+
$this->assertSame(
183+
'<add>'.
184+
'<doc>'.
185+
'<field name="id">1</field>'.
186+
'<field name="id">2</field>'.
187+
'<field name="id">3</field>'.
188+
'<field name="text">a</field>'.
189+
'<field name="text">b</field>'.
190+
'<field name="text">c</field>'.
191+
'</doc>'.
192+
'</add>',
193+
$this->builder->buildAddXml($command)
194+
);
195+
}
196+
177197
public function testBuildAddXmlWithEmptyStrings()
178198
{
179199
$command = new AddCommand();
@@ -489,7 +509,7 @@ public function testBuildAddXmlWithDateTimeImmutable()
489509
);
490510
}
491511

492-
public function testBuildAddXmlWithMultivaluedDateTimes()
512+
public function testBuildAddXmlWithMultivalueDateTimes()
493513
{
494514
$command = new AddCommand();
495515
$command->addDocument(

0 commit comments

Comments
 (0)