From dd1cdec75208b695bf50428d04c2c1183c632d6d Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Fri, 4 Jan 2019 14:11:38 +0100 Subject: [PATCH 1/5] Fix Swagger caching issue, caused that Async Swagger and Usual Swagger are using the same cache key --- .../Magento/Webapi/Model/ServiceMetadata.php | 21 +++++++++++++++++-- .../WebapiAsync/Plugin/ServiceMetadata.php | 16 ++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Webapi/Model/ServiceMetadata.php b/app/code/Magento/Webapi/Model/ServiceMetadata.php index b56aa84b946..54b8557214c 100644 --- a/app/code/Magento/Webapi/Model/ServiceMetadata.php +++ b/app/code/Magento/Webapi/Model/ServiceMetadata.php @@ -79,6 +79,11 @@ class ServiceMetadata */ private $serializer; + /** + * @var string + */ + private $routesConfigCacheId; + /** * Initialize dependencies. * @@ -100,6 +105,7 @@ public function __construct( $this->classReflector = $classReflector; $this->typeProcessor = $typeProcessor; $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); + $this->routesConfigCacheId = self::ROUTES_CONFIG_CACHE_ID; } /** @@ -267,7 +273,7 @@ public function getRouteMetadata($serviceName) public function getRoutesConfig() { if (null === $this->routes) { - $routesConfig = $this->cache->load(self::ROUTES_CONFIG_CACHE_ID); + $routesConfig = $this->cache->load($this->routesConfigCacheId); $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID); if ($routesConfig && is_string($routesConfig) && $typesData && is_string($typesData)) { $this->routes = $this->serializer->unserialize($routesConfig); @@ -276,7 +282,7 @@ public function getRoutesConfig() $this->routes = $this->initRoutesMetadata(); $this->cache->save( $this->serializer->serialize($this->routes), - self::ROUTES_CONFIG_CACHE_ID + $this->routesConfigCacheId ); $this->cache->save( $this->serializer->serialize($this->typeProcessor->getTypesData()), @@ -287,6 +293,17 @@ public function getRoutesConfig() return $this->routes; } + /** + * Set Routes Config Cache ID + * + * @param string $routesConfigCacheId + */ + public function setRoutesConfigCacheId($routesConfigCacheId){ + + $this->routesConfigCacheId = $routesConfigCacheId; + + } + /** * Collect the list of services with routes and request types for use in REST. * diff --git a/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php b/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php index c4e5b572b80..fdd320e4a57 100644 --- a/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php +++ b/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php @@ -15,6 +15,9 @@ class ServiceMetadata { + + const ASYNC_ROUTES_CONFIG_CACHE_ID = 'async-routes-services-config'; + /** * @var \Magento\Webapi\Model\Config */ @@ -272,4 +275,17 @@ private function getResponseDefinitionReplacement() return $this->responseDefinitionReplacement; } + + /** + * Plugin to change config cache id for Asynchronous operations + * + * @return null + */ + public function beforeGetRoutesConfig(\Magento\Webapi\Model\ServiceMetadata $subject) + { + if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)) { + $subject->setRoutesConfigCacheId(self::ASYNC_ROUTES_CONFIG_CACHE_ID); + } + return null; + } } From dad84f4f02283b6b43fccf4e56fd7f4faa5f0409 Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Mon, 7 Jan 2019 14:09:35 +0100 Subject: [PATCH 2/5] Change implementation to use Plugins for Cache instances --- .../Magento/Webapi/Model/ServiceMetadata.php | 10 ++-------- .../WebapiAsync/Plugin/ServiceMetadata.php | 16 ---------------- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/app/code/Magento/Webapi/Model/ServiceMetadata.php b/app/code/Magento/Webapi/Model/ServiceMetadata.php index 54b8557214c..ccb1faae5a5 100644 --- a/app/code/Magento/Webapi/Model/ServiceMetadata.php +++ b/app/code/Magento/Webapi/Model/ServiceMetadata.php @@ -79,11 +79,6 @@ class ServiceMetadata */ private $serializer; - /** - * @var string - */ - private $routesConfigCacheId; - /** * Initialize dependencies. * @@ -105,7 +100,6 @@ public function __construct( $this->classReflector = $classReflector; $this->typeProcessor = $typeProcessor; $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); - $this->routesConfigCacheId = self::ROUTES_CONFIG_CACHE_ID; } /** @@ -273,7 +267,7 @@ public function getRouteMetadata($serviceName) public function getRoutesConfig() { if (null === $this->routes) { - $routesConfig = $this->cache->load($this->routesConfigCacheId); + $routesConfig = $this->cache->load(self::ROUTES_CONFIG_CACHE_ID); $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID); if ($routesConfig && is_string($routesConfig) && $typesData && is_string($typesData)) { $this->routes = $this->serializer->unserialize($routesConfig); @@ -282,7 +276,7 @@ public function getRoutesConfig() $this->routes = $this->initRoutesMetadata(); $this->cache->save( $this->serializer->serialize($this->routes), - $this->routesConfigCacheId + self::ROUTES_CONFIG_CACHE_ID ); $this->cache->save( $this->serializer->serialize($this->typeProcessor->getTypesData()), diff --git a/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php b/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php index fdd320e4a57..c4e5b572b80 100644 --- a/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php +++ b/app/code/Magento/WebapiAsync/Plugin/ServiceMetadata.php @@ -15,9 +15,6 @@ class ServiceMetadata { - - const ASYNC_ROUTES_CONFIG_CACHE_ID = 'async-routes-services-config'; - /** * @var \Magento\Webapi\Model\Config */ @@ -275,17 +272,4 @@ private function getResponseDefinitionReplacement() return $this->responseDefinitionReplacement; } - - /** - * Plugin to change config cache id for Asynchronous operations - * - * @return null - */ - public function beforeGetRoutesConfig(\Magento\Webapi\Model\ServiceMetadata $subject) - { - if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)) { - $subject->setRoutesConfigCacheId(self::ASYNC_ROUTES_CONFIG_CACHE_ID); - } - return null; - } } From 56f35807cb36ca741ed1057197052caf793535d3 Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Mon, 7 Jan 2019 14:11:10 +0100 Subject: [PATCH 3/5] Add Plugin --- .../WebapiAsync/Plugin/Cache/Webapi.php | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php diff --git a/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php b/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php new file mode 100644 index 00000000000..3d6492a8be3 --- /dev/null +++ b/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php @@ -0,0 +1,98 @@ +request = $request; + $this->asynchronousSchemaRequestProcessor = $asynchronousSchemaRequestProcessor; + } + + /** + * Change identifier in case if Async request before cache load + * + * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject + * @param string $identifier + * @return null|string + */ + public function beforeLoad(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier) + { + if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request) + && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) { + return self::ASYNC_ROUTES_CONFIG_CACHE_ID; + } + return null; + } + + /** + * Change identifier in case if Async request before cache save + * + * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject + * @param $data + * @param string $identifier + * @param array $tags + * @param null $lifeTime + * @return array|null + */ + public function beforeSave(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $data, $identifier, array $tags = [], $lifeTime = null) + { + if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request) + && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) { + return [$data, self::ASYNC_ROUTES_CONFIG_CACHE_ID, $tags, $lifeTime]; + } + return null; + } + + /** + * Change identifier in case if Async request before remove cache + * + * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject + * @param string $identifier + * @return null|string + */ + public function beforeRemove(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier) + { + if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request) + && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) { + return self::ASYNC_ROUTES_CONFIG_CACHE_ID; + } + return null; + } +} From cbcc5e805acf1df26de81b4784138528484061a2 Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Mon, 7 Jan 2019 14:12:19 +0100 Subject: [PATCH 4/5] Declate Plugin for WebApi cache and remove not used methods --- app/code/Magento/Webapi/Model/ServiceMetadata.php | 11 ----------- app/code/Magento/WebapiAsync/etc/di.xml | 3 +++ 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Webapi/Model/ServiceMetadata.php b/app/code/Magento/Webapi/Model/ServiceMetadata.php index ccb1faae5a5..b56aa84b946 100644 --- a/app/code/Magento/Webapi/Model/ServiceMetadata.php +++ b/app/code/Magento/Webapi/Model/ServiceMetadata.php @@ -287,17 +287,6 @@ public function getRoutesConfig() return $this->routes; } - /** - * Set Routes Config Cache ID - * - * @param string $routesConfigCacheId - */ - public function setRoutesConfigCacheId($routesConfigCacheId){ - - $this->routesConfigCacheId = $routesConfigCacheId; - - } - /** * Collect the list of services with routes and request types for use in REST. * diff --git a/app/code/Magento/WebapiAsync/etc/di.xml b/app/code/Magento/WebapiAsync/etc/di.xml index 83f1d6a78f2..7411ec0561d 100755 --- a/app/code/Magento/WebapiAsync/etc/di.xml +++ b/app/code/Magento/WebapiAsync/etc/di.xml @@ -10,6 +10,9 @@ + + + Magento\WebapiAsync\Model\BulkServiceConfig From eecb01fa884a9cd23883a4f243ebf61bd8e97a51 Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Tue, 8 Jan 2019 12:16:17 +0100 Subject: [PATCH 5/5] Fix static tests --- .../Magento/WebapiAsync/Plugin/Cache/Webapi.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php b/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php index 3d6492a8be3..ecc929b2048 100644 --- a/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php +++ b/app/code/Magento/WebapiAsync/Plugin/Cache/Webapi.php @@ -51,6 +51,7 @@ public function __construct( * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject * @param string $identifier * @return null|string + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeLoad(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier) { @@ -65,14 +66,20 @@ public function beforeLoad(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $id * Change identifier in case if Async request before cache save * * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject - * @param $data + * @param string $data * @param string $identifier * @param array $tags - * @param null $lifeTime + * @param int|bool|null $lifeTime * @return array|null + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function beforeSave(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $data, $identifier, array $tags = [], $lifeTime = null) - { + public function beforeSave( + \Magento\Webapi\Model\Cache\Type\Webapi $subject, + $data, + $identifier, + array $tags = [], + $lifeTime = null + ) { if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request) && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) { return [$data, self::ASYNC_ROUTES_CONFIG_CACHE_ID, $tags, $lifeTime]; @@ -86,6 +93,7 @@ public function beforeSave(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $da * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject * @param string $identifier * @return null|string + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeRemove(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier) {