Skip to content

Commit

Permalink
Handle circular references in ApiParser
Browse files Browse the repository at this point in the history
  • Loading branch information
teohhanhui committed Feb 18, 2016
1 parent 41343e5 commit d8b5a54
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Nelmio/Parser/ApiParser.php
Expand Up @@ -84,11 +84,14 @@ public function parse(array $item)
* @param ResourceInterface $resource
* @param string $entityClass
* @param string $io
* @param string[] $visited
*
* @return array
*/
private function parseClass(ResourceInterface $resource, $entityClass, $io)
private function parseClass(ResourceInterface $resource, $entityClass, $io, array $visited = array())
{
$visited[] = $entityClass;

$classMetadata = $this->classMetadataFactory->getMetadataFor(
$entityClass,
$resource->getNormalizationGroups(),
Expand All @@ -103,7 +106,7 @@ private function parseClass(ResourceInterface $resource, $entityClass, $io)
($attributeMetadata->isReadable() && self::OUT_PREFIX === $io) ||
($attributeMetadata->isWritable() && self::IN_PREFIX === $io)
) {
$data[$name] = $this->parseAttribute($resource, $attributeMetadata, $io);
$data[$name] = $this->parseAttribute($resource, $attributeMetadata, $io, null, $visited);
}
}

Expand All @@ -117,10 +120,11 @@ private function parseClass(ResourceInterface $resource, $entityClass, $io)
* @param AttributeMetadataInterface $attributeMetadata
* @param string $io
* @param Type|null $type
* @param string[] $visited
*
* @return array
*/
private function parseAttribute(ResourceInterface $resource, AttributeMetadataInterface $attributeMetadata, $io, Type $type = null)
private function parseAttribute(ResourceInterface $resource, AttributeMetadataInterface $attributeMetadata, $io, Type $type = null, array $visited = array())
{
$data = array(
'dataType' => null,
Expand All @@ -144,7 +148,7 @@ private function parseAttribute(ResourceInterface $resource, AttributeMetadataIn
$data['actualType'] = DataTypes::COLLECTION;

if ($collectionType = $type->getCollectionType()) {
$subAttribute = $this->parseAttribute($resource, $attributeMetadata, $io, $collectionType);
$subAttribute = $this->parseAttribute($resource, $attributeMetadata, $io, $collectionType, $visited);
if (self::IRI === $subAttribute['dataType']) {
$data['dataType'] = 'array of IRIs';
$data['subType'] = DataTypes::STRING;
Expand Down Expand Up @@ -182,7 +186,7 @@ private function parseAttribute(ResourceInterface $resource, AttributeMetadataIn

$data['actualType'] = DataTypes::MODEL;
$data['subType'] = $class;
$data['children'] = $this->parseClass($resource, $class, $io);
$data['children'] = in_array($class, $visited) ? [] : $this->parseClass($resource, $class, $io, $visited);

return $data;
}
Expand Down

0 comments on commit d8b5a54

Please sign in to comment.