Skip to content

Commit e338c69

Browse files
committed
Merge branch 'eb/sc-139580/empty-flags-state-json' into contract-tests
2 parents c01cf6e + e7857d2 commit e338c69

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/LaunchDarkly/FeatureFlagsState.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,17 @@ public function toValuesMap(): array
149149
public function jsonSerialize(): array
150150
{
151151
$ret = array_replace([], $this->_flagValues);
152-
$metaMap = [];
153-
foreach ($this->_flagMetadata as $key => $meta) {
154-
$meta = array_replace([], $meta);
155-
if (isset($meta['reason'])) {
156-
$meta['reason'] = $meta['reason']->jsonSerialize();
152+
if (count($this->_flagMetadata) === 0) {
153+
$metaMap = new \stdClass(); // using object rather than array ensures the JSON value is {}, not []
154+
} else {
155+
$metaMap = [];
156+
foreach ($this->_flagMetadata as $key => $meta) {
157+
$meta = array_replace([], $meta);
158+
if (isset($meta['reason'])) {
159+
$meta['reason'] = $meta['reason']->jsonSerialize();
160+
}
161+
$metaMap[$key] = $meta;
157162
}
158-
$metaMap[$key] = $meta;
159163
}
160164
$ret['$flagsState'] = $metaMap;
161165
$ret['$valid'] = $this->_valid;

tests/FeatureFlagsStateTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,20 @@ public function testJsonEncodeUsesCustomSerializer()
138138
$decoded = json_decode($json, true);
139139
$this->assertEquals($expected, $decoded);
140140
}
141+
142+
public function testJsonEncodeWithEmptyData()
143+
{
144+
$state = new FeatureFlagsState(true);
145+
$json = json_encode($state);
146+
147+
$expected = [
148+
'$valid' => true,
149+
'$flagsState' => []
150+
];
151+
$this->assertEquals($expected, json_decode($json, true));
152+
153+
// Due to ambiguity of PHP array types, we need to verify that the $flagsState value
154+
// is an empty JSON object, not an empty JSON array.
155+
$this->assertStringContainsString('"$flagsState":{}', $json);
156+
}
141157
}

0 commit comments

Comments
 (0)