diff --git a/src/Keboola/Json/Parser.php b/src/Keboola/Json/Parser.php index 19c2663..4b591ef 100755 --- a/src/Keboola/Json/Parser.php +++ b/src/Keboola/Json/Parser.php @@ -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, diff --git a/tests/Keboola/Json/AnalyzerTest.php b/tests/Keboola/Json/AnalyzerTest.php index 6fdc32d..53701fb 100755 --- a/tests/Keboola/Json/AnalyzerTest.php +++ b/tests/Keboola/Json/AnalyzerTest.php @@ -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 = [ @@ -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() + ); + } } diff --git a/tests/Keboola/Json/ParserTest.php b/tests/Keboola/Json/ParserTest.php index 5ecd68c..c066dce 100755 --- a/tests/Keboola/Json/ParserTest.php +++ b/tests/Keboola/Json/ParserTest.php @@ -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