diff --git a/lib/Alchemy/Phrasea/Controller/Api/Result.php b/lib/Alchemy/Phrasea/Controller/Api/Result.php index 148b679f9f..15624e1f6e 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/Result.php +++ b/lib/Alchemy/Phrasea/Controller/Api/Result.php @@ -11,6 +11,7 @@ namespace Alchemy\Phrasea\Controller\Api; +use Alchemy\Phrasea\ControllerProvider\Api\V1; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -267,7 +268,13 @@ class Result public function getVersion() { if (null === $this->version) { - $this->version = $this->request->attributes->get('api_version') ?: self::$defaultVersion; + if($this->request->attributes->get('api_version')){ + $this->version = $this->request->attributes->get('api_version'); + }elseif(mb_strpos($this->request->getPathInfo(), '/api/v1') !== FALSE){ + $this->version = V1::VERSION; + }else{ + $this->version = self::$defaultVersion; + } } return $this->version; diff --git a/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php b/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php index 1ccbd0580a..34c4685149 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php +++ b/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php @@ -13,6 +13,8 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Authentication\Authenticator; use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Controller\Api\Result; +use Alchemy\Phrasea\ControllerProvider\Api\V1; +use Alchemy\Phrasea\ControllerProvider\Api\V2; use Alchemy\Phrasea\Core\Configuration\PropertyAccess; use Alchemy\Phrasea\Core\Event\ApiOAuth2EndEvent; use Alchemy\Phrasea\Core\Event\ApiOAuth2StartEvent; @@ -72,7 +74,16 @@ class OAuthListener $oAuth2Account = $token->getAccount(); // Sets the Api Version - $request->attributes->set('api_version', $oAuth2Account->getApiVersion()); + + $CalledController = $request->attributes->get('_controller'); + if(mb_strpos($CalledController, 'controller.api.v1') !== FALSE){ + $request->attributes->set('api_version', V1::VERSION); + }elseif(mb_strpos($CalledController, 'controller.api.v2') !== FALSE){ + $request->attributes->set('api_version', V2::VERSION); + }else{ + $request->attributes->set('api_version', $oAuth2Account->getApiVersion()); + } + $oAuth2App = $oAuth2Account->getApplication(); /** @var PropertyAccess $conf */ diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php b/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php index 3b10c5266f..13d77f7c9a 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php @@ -6,6 +6,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Border\File; use Alchemy\Phrasea\Controller\Api\V1Controller; +use Alchemy\Phrasea\ControllerProvider\Api\V1; use Alchemy\Phrasea\ControllerProvider\Api\V2; use Alchemy\Phrasea\Core\Configuration\PropertyAccess; use Alchemy\Phrasea\Core\PhraseaEvents; @@ -459,6 +460,10 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase protected function evaluateMeta($content, $version = null) { + if(mb_strpos($content['meta']['request'], '/api/v1') !== FALSE){ + $version = V1::VERSION; + } + $this->assertTrue(is_array($content), 'La reponse est un objet'); $this->assertArrayHasKey('meta', $content); $this->assertArrayHasKey('response', $content);