Skip to content

Commit 47722ae

Browse files
committed
MAGETWO-31901: Implement Attributes Mapping to Data Interfaces
- Refactored Soap WSDL generation to add custom attribute complex types in WSDL schema Types based on available interfaces - Added new Test to verify against test service
1 parent 6c53c3e commit 47722ae

File tree

6 files changed

+108
-7
lines changed

6 files changed

+108
-7
lines changed

app/code/Magento/Webapi/Model/Config/ClassReflector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function extractMethodData(ReflectionMethod $method)
9494
/** @var \Zend\Server\Reflection\ReflectionParameter $parameter */
9595
foreach ($prototype->getParameters() as $parameter) {
9696
$parameterData = [
97-
'type' => $this->_typeProcessor->process($parameter->getType()),
97+
'type' => $this->_typeProcessor->register($parameter->getType()),
9898
'required' => !$parameter->isOptional(),
9999
'documentation' => $parameter->getDescription(),
100100
];
@@ -105,7 +105,7 @@ public function extractMethodData(ReflectionMethod $method)
105105
}
106106
if ($prototype->getReturnType() != 'void' && $prototype->getReturnType() != 'null') {
107107
$methodData['interface']['out']['parameters']['result'] = [
108-
'type' => $this->_typeProcessor->process($prototype->getReturnType()),
108+
'type' => $this->_typeProcessor->register($prototype->getReturnType()),
109109
'documentation' => $prototype->getReturnValue()->getDescription(),
110110
'required' => true,
111111
];

app/code/Magento/Webapi/Model/Soap/Wsdl/Generator.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Generator
4848
*/
4949
protected $storeManager;
5050

51+
/**
52+
* @var array
53+
*/
54+
protected $customAttributeMapArray = null;
55+
5156
/**
5257
* Initialize dependencies.
5358
*
@@ -62,13 +67,15 @@ public function __construct(
6267
Factory $wsdlFactory,
6368
\Magento\Webapi\Model\Cache\Type $cache,
6469
\Magento\Framework\Reflection\TypeProcessor $typeProcessor,
65-
\Magento\Store\Model\StoreManagerInterface $storeManager
70+
\Magento\Store\Model\StoreManagerInterface $storeManager,
71+
\Magento\Framework\Api\CustomAttributeMap $customAttributeMap
6672
) {
6773
$this->_apiConfig = $apiConfig;
6874
$this->_wsdlFactory = $wsdlFactory;
6975
$this->_cache = $cache;
7076
$this->_typeProcessor = $typeProcessor;
7177
$this->storeManager = $storeManager;
78+
$this->customAttributeMapArray = array_values($customAttributeMap->getData());
7279
}
7380

7481
/**
@@ -114,6 +121,8 @@ protected function _generate($requestedServices, $endPointUrl)
114121
$wsdl = $this->_wsdlFactory->create(self::WSDL_NAME, $endPointUrl);
115122
$wsdl->addSchemaTypeSection();
116123
$faultMessageName = $this->_addGenericFaultComplexTypeNodes($wsdl);
124+
$wsdl = $this->addCustomAttributeTypes($wsdl);
125+
117126
foreach ($requestedServices as $serviceClass => $serviceData) {
118127
$portTypeName = $this->getPortTypeName($serviceClass);
119128
$bindingName = $this->getBindingName($serviceClass);
@@ -159,6 +168,21 @@ protected function _generate($requestedServices, $endPointUrl)
159168
return $wsdl->toXML();
160169
}
161170

171+
/**
172+
* Create and add WSDL Types for complex custom attribute classes
173+
*
174+
* @param \Magento\Webapi\Model\Soap\Wsdl $wsdl
175+
* @return \Magento\Webapi\Model\Soap\Wsdl
176+
*/
177+
protected function addCustomAttributeTypes($wsdl)
178+
{
179+
foreach ($this->customAttributeMapArray as $customAttributeClass) {
180+
$typeName = $this->_typeProcessor->register($customAttributeClass);
181+
$wsdl->addComplexType($typeName);
182+
}
183+
return $wsdl;
184+
}
185+
162186
/**
163187
* Create input message and corresponding element and complex types in WSDL.
164188
*

app/code/Magento/Webapi/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@
5252
<argument name="cache" xsi:type="object">Magento\Webapi\Model\Cache\Type</argument>
5353
</arguments>
5454
</type>
55+
<type name="Magento\Webapi\Model\Soap\Config">
56+
<arguments>
57+
<argument name="customAttributeMap" xsi:type="object">Magento\Framework\Api\CustomAttributeMap</argument>
58+
</arguments>
59+
</type>
5560
</config>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
/**
12+
* Test WSDL generation mechanisms.
13+
*/
14+
class CustomAttributeTypeWsdlGenerationTest extends \Magento\TestFramework\TestCase\WebapiAbstract
15+
{
16+
/** @var string */
17+
protected $_baseUrl = TESTS_BASE_URL;
18+
19+
/** @var string */
20+
protected $_storeCode;
21+
22+
/** @var string */
23+
protected $_soapUrl;
24+
25+
protected function setUp()
26+
{
27+
$this->_markTestAsSoapOnly("WSDL generation tests are intended to be executed for SOAP adapter only.");
28+
$this->_storeCode = Bootstrap::getObjectManager()->get('Magento\Store\Model\StoreManagerInterface')
29+
->getStore()->getCode();
30+
$this->_soapUrl = "{$this->_baseUrl}/soap/{$this->_storeCode}?services=testModuleMSCAllSoapAndRestV1";
31+
parent::setUp();
32+
}
33+
34+
public function testCustomAttributeTypesInWsdl()
35+
{
36+
/** @var $soapAdapter \Magento\TestFramework\TestCase\Webapi\Adapter\Soap */
37+
$soapAdapter = $this->_getWebApiAdapter(self::ADAPTER_SOAP);
38+
$soapClient = $soapAdapter->instantiateSoapClient($this->_soapUrl);
39+
$types = $soapClient->getTypes();
40+
41+
$testCustomTypeCount = 0;
42+
foreach ($types as $type) {
43+
if (strpos($type, 'TestModuleMSCDataCustomAttributeDataObjectInterface') !== false ||
44+
strpos($type, 'TestModuleMSCDataCustomAttributeNestedDataObjectInterface') !== false
45+
) {
46+
$testCustomTypeCount++;
47+
}
48+
}
49+
50+
$this->assertEquals(
51+
2,
52+
$testCustomTypeCount,
53+
'Incorrect count for Custom attribute types. Found "' . $testCustomTypeCount . ' type(s)" expected 2.'
54+
);
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Api;
8+
9+
use Magento\Framework\Object;
10+
11+
/**
12+
* Marker Class of type Magento/Framework/Object for storing all Custom Attribute Interfaces
13+
*/
14+
class CustomAttributeMap extends Object
15+
{
16+
}

lib/internal/Magento/Framework/Reflection/TypeProcessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public function setTypeData($typeName, $data)
112112
* Process type name. In case parameter type is a complex type (class) - process its properties.
113113
*
114114
* @param string $type
115-
* @return string
115+
* @return string Complex type name
116116
* @throws \LogicException
117117
*/
118-
public function process($type)
118+
public function register($type)
119119
{
120120
$typeName = $this->normalizeType($type);
121121
if (!$this->isTypeSimple($typeName) && !$this->isTypeAny($typeName)) {
@@ -150,7 +150,7 @@ protected function _processComplexType($class)
150150
$typeName = $this->translateTypeName($class);
151151
$this->_types[$typeName] = [];
152152
if ($this->isArrayType($class)) {
153-
$this->process($this->getArrayItemType($class));
153+
$this->register($this->getArrayItemType($class));
154154
} else {
155155
if (!(class_exists($class) || interface_exists($class))) {
156156
throw new \InvalidArgumentException(
@@ -189,7 +189,7 @@ protected function _processMethod(\Zend\Code\Reflection\MethodReflection $method
189189
$returnMetadata = $this->getGetterReturnType($methodReflection);
190190
$fieldName = $this->dataObjectGetterNameToFieldName($methodReflection->getName());
191191
$this->_types[$typeName]['parameters'][$fieldName] = [
192-
'type' => $this->process($returnMetadata['type']),
192+
'type' => $this->register($returnMetadata['type']),
193193
'required' => $returnMetadata['isRequired'],
194194
'documentation' => $returnMetadata['description'],
195195
];

0 commit comments

Comments
 (0)