From b7f5fb58d4c4f563b2600b200d28289c0c893e4f Mon Sep 17 00:00:00 2001
From: Bez Hermoso <bez@activelamp.com>
Date: Mon, 4 Aug 2014 10:58:41 -0700
Subject: [PATCH] Added  definition when  is provided.

---
 .../SwaggerConfigCompilerPass.php             |  5 +++
 Formatter/SwaggerFormatter.php                | 32 +++++++++++++++++--
 Tests/Fixtures/app/config/default.yml         |  4 +++
 Tests/Formatter/SwaggerFormatterTest.php      | 31 +++++++++++++++---
 4 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/DependencyInjection/SwaggerConfigCompilerPass.php b/DependencyInjection/SwaggerConfigCompilerPass.php
index 3e9d488..9e41910 100644
--- a/DependencyInjection/SwaggerConfigCompilerPass.php
+++ b/DependencyInjection/SwaggerConfigCompilerPass.php
@@ -45,5 +45,10 @@ class SwaggerConfigCompilerPass implements CompilerPassInterface
         $formatter->addMethodCall('setSwaggerVersion', array($container->getParameter('nelmio_api_doc.swagger.swagger_version')));
         $formatter->addMethodCall('setInfo', array($container->getParameter('nelmio_api_doc.swagger.info')));
 
+        $authentication = $container->getParameter('nelmio_api_doc.sandbox.authentication');
+
+        if ($authentication !== null) {
+            $formatter->addMethodCall('setAuthenticationConfig', array($authentication));
+        }
     }
 }
\ No newline at end of file
diff --git a/Formatter/SwaggerFormatter.php b/Formatter/SwaggerFormatter.php
index 6b9cb53..9b21d31 100644
--- a/Formatter/SwaggerFormatter.php
+++ b/Formatter/SwaggerFormatter.php
@@ -54,6 +54,16 @@ class SwaggerFormatter implements FormatterInterface
         DataTypes::DATETIME => 'date-time',
     );
 
+    /**
+     * @var array
+     */
+    protected $authConfig = null;
+
+    public function setAuthenticationConfig(array $config)
+    {
+        $this->authConfig = $config;
+    }
+
     /**
      * Format a collection of documentation data.
      *
@@ -115,7 +125,25 @@ class SwaggerFormatter implements FormatterInterface
 
     protected function getAuthorizations()
     {
-        return array();
+        $auth = array();
+
+        if ($this->authConfig === null) {
+            return $auth;
+        }
+
+        $config = $this->authConfig;
+
+        if ($config['delivery'] === 'http') {
+            return $auth;
+        }
+
+        $auth['apiKey'] = array(
+            'type' => 'apiKey',
+            'passAs' => $config['delivery'],
+            'keyname' => $config['name'],
+        );
+
+        return $auth;
     }
 
     /**
@@ -157,7 +185,7 @@ class SwaggerFormatter implements FormatterInterface
             'models' => array(),
             'produces' => array(),
             'consumes' => array(),
-            'authorizations' => array(),
+            'authorizations' => $this->getAuthorizations(),
         );
 
         $main = null;
diff --git a/Tests/Fixtures/app/config/default.yml b/Tests/Fixtures/app/config/default.yml
index 727293a..24e6683 100644
--- a/Tests/Fixtures/app/config/default.yml
+++ b/Tests/Fixtures/app/config/default.yml
@@ -53,6 +53,10 @@ jms_serializer:
         auto_detection: true
 
 nelmio_api_doc:
+    sandbox:
+        authentication:
+            delivery: header
+            name: access_token
     cache:
         enabled: true
     exclude_sections: ["private", "exclusive"]
diff --git a/Tests/Formatter/SwaggerFormatterTest.php b/Tests/Formatter/SwaggerFormatterTest.php
index 488849b..cc79cb4 100644
--- a/Tests/Formatter/SwaggerFormatterTest.php
+++ b/Tests/Formatter/SwaggerFormatterTest.php
@@ -61,7 +61,13 @@ class SwaggerFormatterTest extends WebTestCase
                     'licenseUrl' => 'http://opensource.org/licenses/MIT',
                 ),
             'authorizations' =>
-                array(),
+                array(
+                    'apiKey' => array(
+                        'type' => 'apiKey',
+                        'passAs' => 'header',
+                        'keyname' => 'access_token',
+                    )
+                ),
             'apis' =>
                 array(
                     array(
@@ -419,7 +425,13 @@ With multiple lines.',
                     'consumes' =>
                         array(),
                     'authorizations' =>
-                        array(),
+                        array(
+                            'apiKey' => array(
+                                'type' => 'apiKey',
+                                'passAs' => 'header',
+                                'keyname' => 'access_token',
+                            )
+                        ),
                 ),
             ),
             array(
@@ -539,7 +551,13 @@ With multiple lines.',
                     'consumes' =>
                         array(),
                     'authorizations' =>
-                        array(),
+                        array(
+                            'apiKey' => array(
+                                'type' => 'apiKey',
+                                'passAs' => 'header',
+                                'keyname' => 'access_token',
+                            )
+                        ),
                 ),
             ),
             array(
@@ -721,7 +739,12 @@ With multiple lines.',
                         array (
                         ),
                     'authorizations' =>
-                        array (
+                        array(
+                            'apiKey' => array(
+                                'type' => 'apiKey',
+                                'passAs' => 'header',
+                                'keyname' => 'access_token',
+                            )
                         ),
                 ),
             ),