Skip to content

Commit

Permalink
Merge pull request #518 from dakota/fail-on-entity
Browse files Browse the repository at this point in the history
Json api Error if the entity class is \Cake\ORM\Entity
  • Loading branch information
bravo-kernel authored May 2, 2017
2 parents 2bbe099 + 1a80581 commit 0129f12
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/View/JsonApiView.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cake\Event\EventManager;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;
use Cake\View\View;
use Crud\Error\Exception\CrudException;
Expand Down Expand Up @@ -217,6 +218,10 @@ protected function _entitiesToNeoMerxSchema(array $repositories)

$entityClass = $repository->entityClass();

if ($entityClass === Entity::class) {
throw new CrudException(sprintf('Entity classes must not be the generic "%s" class for repository "%s"', $entityClass, $repositoryName));
}

// Turn full class name back into plugin split format
// Not including /Entity in the type makes sure its compatible with other types
$entityName = App::shortName($entityClass, 'Model');
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/View/JsonApiViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ public function testGetSpecialVars()
$this->assertContains('_underscored_should_be_in_special_vars', $result);
}

/**
* Make sure that an exception is thrown for generic entity classes
*
* @return void
* @expectedException \Crud\Error\Exception\CrudException
* @expectedExceptionMessage Entity classes must not be the generic "Cake\ORM\Entity" class for repository "Countries"
*/
public function testEncodeWithGenericEntity()
{
TableRegistry::get('Countries')->entityClass(Entity::class);

// test collection of entities without relationships
$countries = TableRegistry::get('Countries')
->find()
->all();
$view = $this->_getView('Countries', [
'countries' => $countries
]);

$view->render();
}

/**
* Make sure expected JSON API strings are generated as expected when
* using Crud's DynamicEntitySchema.
Expand Down

0 comments on commit 0129f12

Please sign in to comment.