Skip to content

Commit

Permalink
fix: Use correct array encoding in config produced by RollbarJsHelper
Browse files Browse the repository at this point in the history
When building JS code with RollbarJsHelper and passing configuration parameters that are arrays, those arrays in the final JS output are incorrectly encoded as objects.
  • Loading branch information
vilius-g committed Jan 16, 2023
1 parent 21b1754 commit 12fd960
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/RollbarJsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function addJs($headers = null, $nonce = null, $customJs = "")
*/
public function configJsTag()
{
return "var _rollbarConfig = " . json_encode($this->config, JSON_FORCE_OBJECT) . ";";
return "var _rollbarConfig = " . json_encode((object)$this->config) . ";";
}

/**
Expand Down
22 changes: 16 additions & 6 deletions tests/JsHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,11 @@ public function scriptTagProvider()
);
}

public function testConfigJsTag()
/**
* @dataProvider configJsTagProvider
*/
public function testConfigJsTag($config, $expectedJson)
{
$config = array(
'config1' => 'value 1'
);

$expectedJson = json_encode($config);
$expected = "var _rollbarConfig = $expectedJson;";

$helper = new RollbarJsHelper($config);
Expand All @@ -241,6 +239,18 @@ public function testConfigJsTag()
$this->assertEquals($expected, $result);
}

public function configJsTagProvider()
{
return array(
array(array(), '{}'),
array(array('config1' => 'value 1'), '{"config1":"value 1"}'),
array(
array('hostBlackList' => array('example.com', 'badhost.com')),
'{"hostBlackList":["example.com","badhost.com"]}'
),
);
}

/**
* @dataProvider addJsProvider
*/
Expand Down

0 comments on commit 12fd960

Please sign in to comment.