Skip to content

Commit

Permalink
Merge pull request #459 from vilius-g/js-config-encode
Browse files Browse the repository at this point in the history
Fix incorrect array encoding in config produced by RollbarJsHelper
  • Loading branch information
danielmorell authored Jan 17, 2023
2 parents 21b1754 + 12fd960 commit 4897fa2
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 4897fa2

Please sign in to comment.