Swagger support:

Unified data types [actualType and subType]
Updated tests.
JMS parsing fixes; updated {Validator,FormType}Parser, FOSRestHandler, and AbstractFormatter, and updated DataTypes enum.
Modified dataType checking.
Updated tests.
Updated DataTypes enum.
Quick fix and added doc comments.
CS fixes.
Refactored FormTypeParser to produce nested parameters. Updated tests accordingly.
Logical and CS fixes.
Sub-forms and more tests.
Logical and CS fixes.
Swagger support: created formatter.
Configuration and resourcePath logic update.
ApiDoc annotation update. Updated formatter and added tests.
Parameter formatting.
Added tests for SwaggerFormatter.
Added  option in annotation, and the corresponding logic for parsing the supplied values and processing them in the formatter.
Routing update.
Updated tests.
Removed unused dependency and updated doc comments.
Renamed 'responseModels' to 'responseMap'
Update the resource filtering and formatting of response messages.
Updated check for 200 response model.
Ignore data_class and always use form-type to avoid conflicts.
Fix: add 'type' even if '' is specified.
Refactored responseMap; added parsedResponseMap. Added tests and updated some.
Fix: add 'type' even if '' is specified.
Initial commit of command.
Finished logic for dumping files.
Updated doc comment; added license and added more meaningful class comment.
Array of models support.
This commit is contained in:
Bez Hermoso 2014-06-17 17:05:00 -07:00
parent bb723bdb40
commit 9d3c0a8c29
7 changed files with 53 additions and 36 deletions

View File

@ -11,7 +11,9 @@
namespace Nelmio\ApiDocBundle\Controller; namespace Nelmio\ApiDocBundle\Controller;
use Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -28,11 +30,13 @@ class ApiDocController extends Controller
public function swaggerAction(Request $request, $resource = null) public function swaggerAction(Request $request, $resource = null)
{ {
$docs = $this->get('nelmio_api_doc.extractor.api_doc_extractor')->all(); $docs = $this->get('nelmio_api_doc.extractor.api_doc_extractor')->all();
$formatter = $this->get('nelmio_api_doc.formatter.request_aware_swagger_formatter'); $formatter = new RequestAwareSwaggerFormatter($request, $this->get('nelmio_api_doc.formatter.swagger_formatter'));
$spec = $formatter->format($docs, $resource ? '/' . $resource : null); $spec = $formatter->format($docs, $resource ? '/' . $resource : null);
if (count($spec['apis']) === 0) { if ($resource !== null && count($spec['apis']) === 0) {
throw $this->createNotFoundException(sprintf('Cannot find resource "%s"', $resource)); throw $this->createNotFoundException(sprintf('Cannot find resource "%s"', $resource));
} }

View File

@ -11,11 +11,11 @@
namespace Nelmio\ApiDocBundle\DependencyInjection; namespace Nelmio\ApiDocBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Processor; use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
class NelmioApiDocExtension extends Extension class NelmioApiDocExtension extends Extension
{ {

View File

@ -11,8 +11,14 @@
namespace Nelmio\ApiDocBundle\DependencyInjection; namespace Nelmio\ApiDocBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
/** /**
@ -39,12 +45,5 @@ class SwaggerConfigCompilerPass implements CompilerPassInterface
$formatter->addMethodCall('setSwaggerVersion', array($container->getParameter('nelmio_api_doc.swagger.swagger_version'))); $formatter->addMethodCall('setSwaggerVersion', array($container->getParameter('nelmio_api_doc.swagger.swagger_version')));
$formatter->addMethodCall('setInfo', array($container->getParameter('nelmio_api_doc.swagger.info'))); $formatter->addMethodCall('setInfo', array($container->getParameter('nelmio_api_doc.swagger.info')));
$formatter = $container->getDefinition('nelmio_api_doc.formatter.request_aware_swagger_formatter');
$formatter->addMethodCall('setBasePath', array($container->getParameter('nelmio_api_doc.swagger.base_path')));
$formatter->addMethodCall('setApiVersion', array($container->getParameter('nelmio_api_doc.swagger.api_version')));
$formatter->addMethodCall('setSwaggerVersion', array($container->getParameter('nelmio_api_doc.swagger.swagger_version')));
$formatter->addMethodCall('setInfo', array($container->getParameter('nelmio_api_doc.swagger.info')));
} }
} }

View File

@ -485,8 +485,8 @@ class ApiDocExtractor
/** /**
* Creates a human-readable version of the `actualType`. `subType` is taken into account. * Creates a human-readable version of the `actualType`. `subType` is taken into account.
* *
* @param string $actualType * @param string $actualType
* @param string $subType * @param string $subType
* @return string * @return string
*/ */
protected function generateHumanReadableType($actualType, $subType) protected function generateHumanReadableType($actualType, $subType)
@ -509,7 +509,6 @@ class ApiDocExtractor
if (class_exists($subType)) { if (class_exists($subType)) {
$parts = explode('\\', $subType); $parts = explode('\\', $subType);
return sprintf('array of objects (%s)', end($parts)); return sprintf('array of objects (%s)', end($parts));
} }

View File

@ -12,6 +12,7 @@
namespace Nelmio\ApiDocBundle\Formatter; namespace Nelmio\ApiDocBundle\Formatter;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
@ -19,31 +20,55 @@ use Symfony\Component\HttpFoundation\Request;
* *
* @author Bezalel Hermoso <bezalelhermoso@gmail.com> * @author Bezalel Hermoso <bezalelhermoso@gmail.com>
*/ */
class RequestAwareSwaggerFormatter extends SwaggerFormatter class RequestAwareSwaggerFormatter implements FormatterInterface
{ {
/** /**
* @var \Symfony\Component\HttpFoundation\Request * @var \Symfony\Component\HttpFoundation\Request
*/ */
protected $request; protected $request;
/**
* @var SwaggerFormatter
*/
protected $formatter;
/** /**
* @param Request $request * @param Request $request
* @param SwaggerFormatter $formatter
*/ */
public function __construct(Request $request) public function __construct(Request $request, SwaggerFormatter $formatter)
{ {
$this->request = $request; $this->request = $request;
$this->formatter = $formatter;
} }
/** /**
* Format a collection of documentation data.
*
* @param array $collection * @param array $collection
* @param string $resource * @param null $resource
* @return array * @internal param $array [ApiDoc] $collection
* @return string|array
*/ */
protected function produceApiDeclaration(array $collection, $resource) public function format(array $collection, $resource = null)
{ {
$data = parent::produceApiDeclaration($collection, $resource); $result = $this->formatter->format($collection, $resource);
$data['basePath'] = $this->request->getBaseUrl() . $data['basePath'];
return $data; if ($resource !== null) {
$result['basePath'] = $this->request->getBaseUrl() . $result['basePath'];
}
return $result;
} }
}
/**
* Format documentation data for one route.
*
* @param ApiDoc $annotation
* return string|array
*/
public function formatOne(ApiDoc $annotation)
{
return $this->formatter->formatOne($annotation);
}
}

View File

@ -14,9 +14,6 @@ namespace Nelmio\ApiDocBundle\Formatter;
use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\DataTypes; use Nelmio\ApiDocBundle\DataTypes;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Router; use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
@ -57,11 +54,6 @@ class SwaggerFormatter implements FormatterInterface
DataTypes::DATETIME => 'date-time', DataTypes::DATETIME => 'date-time',
); );
/**
* @var Request
*/
protected $request;
/** /**
* Format a collection of documentation data. * Format a collection of documentation data.
* *
@ -139,10 +131,11 @@ class SwaggerFormatter implements FormatterInterface
* *
* @param ApiDoc $annotation * @param ApiDoc $annotation
* return string|array * return string|array
* @throws \BadMethodCallException
*/ */
public function formatOne(ApiDoc $annotation) public function formatOne(ApiDoc $annotation)
{ {
// TODO: Implement formatOne() method. throw new \BadMethodCallException(sprintf('%s does not support formatting a single ApiDoc only.', __CLASS__));
} }
/** /**

View File

@ -9,7 +9,6 @@
<parameter key="nelmio_api_doc.formatter.simple_formatter.class">Nelmio\ApiDocBundle\Formatter\SimpleFormatter</parameter> <parameter key="nelmio_api_doc.formatter.simple_formatter.class">Nelmio\ApiDocBundle\Formatter\SimpleFormatter</parameter>
<parameter key="nelmio_api_doc.formatter.html_formatter.class">Nelmio\ApiDocBundle\Formatter\HtmlFormatter</parameter> <parameter key="nelmio_api_doc.formatter.html_formatter.class">Nelmio\ApiDocBundle\Formatter\HtmlFormatter</parameter>
<parameter key="nelmio_api_doc.formatter.swagger_formatter.class">Nelmio\ApiDocBundle\Formatter\SwaggerFormatter</parameter> <parameter key="nelmio_api_doc.formatter.swagger_formatter.class">Nelmio\ApiDocBundle\Formatter\SwaggerFormatter</parameter>
<parameter key="nelmio_api_doc.formatter.request_aware_swagger_formatter.class">Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter</parameter>
<parameter key="nelmio_api_doc.sandbox.authentication">null</parameter> <parameter key="nelmio_api_doc.sandbox.authentication">null</parameter>
</parameters> </parameters>
@ -58,10 +57,8 @@
<argument>%nelmio_api_doc.sandbox.authentication%</argument> <argument>%nelmio_api_doc.sandbox.authentication%</argument>
</call> </call>
</service> </service>
<service id="nelmio_api_doc.formatter.swagger_formatter" class="%nelmio_api_doc.formatter.swagger_formatter.class%" /> <service id="nelmio_api_doc.formatter.swagger_formatter" class="%nelmio_api_doc.formatter.swagger_formatter.class%"
<service id="nelmio_api_doc.formatter.request_aware_swagger_formatter" class="%nelmio_api_doc.formatter.request_aware_swagger_formatter.class%" scope="request"> parent="nelmio_api_doc.formatter.abstract_formatter" />
<argument type="service" id="request" />
</service>
</services> </services>
</container> </container>