File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments