Skip to content

Commit

Permalink
fix: Fix an exception on empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondrej Vana committed Sep 28, 2015
1 parent c710f4f commit 73b20f6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Keboola/Json/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static function create(Logger $logger, array $definitions = [], $analyzeR
public function process(array $data, $type = "root", $parentId = null)
{
// The analyzer wouldn't set the $struct and parse fails!
if (empty($data) && empty($this->struct->getTypes($type))) {
if (empty($data) && !$this->struct->hasDefinitions($type)) {
throw new NoDataException("Empty data set received for {$type}", [
"data" => $data,
"type" => $type,
Expand Down
78 changes: 77 additions & 1 deletion tests/Keboola/Json/AnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public function testAnalyzeKnownArrayMismatch2()
*/
public function testAnalyzeKnownArrayMismatch3()
{
$analyzer = new Analyzer($this->getLogger('analyzer', true)/*, $struct*/);
$analyzer = new Analyzer($this->getLogger('analyzer', true));
$analyzer->setStrict(true);

$data1 = [
Expand Down Expand Up @@ -420,4 +420,80 @@ public function testAnalyzeKnownArrayMismatch3()
$analyzer->getStruct()->getStruct()
);
}

public function testAnalyzeEmptyArrayOfObject()
{
$data = [
(object) [
'id' => 1,
'arr' => []
],
(object) [
'id' => 2,
'arr' => [
(object) ['val' => 'value']
]
]
];

$analyzer = new Analyzer($this->getLogger('analyzer', true));

$analyzer->analyze($data, 'test');

$this->assertEquals(
[
'test' => [
'id' => 'scalar',
'arr' => 'arrayOfobject'
],
'test.arr' => [
'val' => 'scalar'
]
],
$analyzer->getStruct()->getStruct()
);
}

public function testAnalyzeEmptyArrayOfObjectAutoUpgrade()
{
$data = [
(object) [
'id' => 1,
'arr' => []
],
(object) [
'id' => 2,
'arr' => (object) ['val' => 'value']
],
(object) [
'id' => 3,
'arr' => [
(object) ['val' => 'value2'],
(object) ['val' => 'value3']
]
],
(object) [
'id' => 4,
'arr' => (object) ['val' => 'value4']
]
];

$analyzer = new Analyzer($this->getLogger('analyzer', true));
$analyzer->getStruct()->setAutoUpgradeToArray(true);

$analyzer->analyze($data, 'test');

$this->assertEquals(
[
'test' => [
'id' => 'scalar',
'arr' => 'arrayOfobject'
],
'test.arr' => [
'val' => 'scalar'
]
],
$analyzer->getStruct()->getStruct()
);
}
}
11 changes: 11 additions & 0 deletions tests/Keboola/Json/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,17 @@ public function testIncompleteData()
);
}

/**
* @expectedException \Keboola\Json\Exception\NoDataException
* @expectedExceptionMessage Empty data set received for root
*/
public function testEmptyData()
{
$parser = $this->getParser();

$parser->process([]);
}

/**
* Call a non-public method
* @param mixed $obj
Expand Down

0 comments on commit 73b20f6

Please sign in to comment.