core
core copied to clipboard
Swagger Core Docs example gives ServiceCircularReferenceException
I am trying to add model definitions to my swagger.json file, using the example decorator here:
https://api-platform.com/docs/core/swagger/
When I add the file and configuration, I automatically get a ServiceCircularReferenceException, I assume because it's trying to build the swagger.json file:
Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException:
Circular reference detected for service "AppBundle\Api\Swagger\Decorator\CommandDefinitionDecorator",
path: "AppBundle\Api\Swagger\Decorator\CommandDefinitionDecorator -> serializer -> AppBundle\Api\Swagger\Decorator\CommandDefinitionDecorator". in /var/www/projectx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php:67
Stack trace:
#0 /var/www/projectx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php(70): Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass->checkOutEdges(Array)
#1 /var/www/projectx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php(70): Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass->checkOutEdges(Array)
#2 /var/www/projectx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjec in /var/www/projectx/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php on line 67
This is the configuration:
# app/config/services.yml
services:
...
AppBundle\Api\Swagger\Decorator\CommandDefinitionDecorator:
decorates: 'api_platform.swagger.normalizer.documentation'
arguments: [ '@AppBundle\Api\Swagger\Decorator\CommandDefinitionDecorator.inner' ]
autoconfigure: false
autowire: false
...
And this is the file:
<?php
# src/AppBundle/Api/Swagger/Decorator/CommandDefinitionDecorator.php
namespace AppBundle\Api\Swagger\Decorator;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class CommandDefinitionDecorator implements NormalizerInterface
{
private $decorated;
public function __construct(NormalizerInterface $decorated)
{
$this->decorated = $decorated;
}
/**
* @param $object
* @param null $format
* @param array $context
* @return array|bool|float|int|string
*
* @throws CircularReferenceException
* @throws InvalidArgumentException
* @throws LogicException
*/
public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
$customDefinition = [
'name' => 'fields',
'definition' => 'Fields to remove of the outpout',
'default' => 'id',
'in' => 'query',
];
// e.g. add a custom parameter
$docs['paths']['/foos']['get']['parameters'][] = $customDefinition;
// Override title
$docs['info']['title'] = 'My Api Foo';
return $docs;
}
public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}
It's pretty close to what's in the docs right now, other than the obvious namespace/class name changes. I don't see anything I'm obviously doing wrong here. Is there an error in the docs, or how I'm using them?
Problem solved, had to add it to my excluded directory list (the Api, added to exclude):
# makes classes in src/AppBundle available to be used as services
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Api,Entity,Repository,Twig,Controller,Serializer,Admin}'