Skip to content

Commit de7da4f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-31900-SoapDynamicTypeBinding' into develop
2 parents ff76fe8 + 9801f1e commit de7da4f

File tree

8 files changed

+103
-13
lines changed

8 files changed

+103
-13
lines changed

app/code/Magento/Webapi/Model/Soap/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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class Generator
4949
*/
5050
protected $storeManager;
5151

52+
/**
53+
* @var array
54+
*/
55+
protected $customAttributeMapArray = null;
56+
5257
/**
5358
* Initialize dependencies.
5459
*
@@ -57,19 +62,22 @@ class Generator
5762
* @param \Magento\Framework\App\Cache\Type\Webapi $cache
5863
* @param \Magento\Framework\Reflection\TypeProcessor $typeProcessor
5964
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
65+
* @param \Magento\Framework\Object $customAttributeMap
6066
*/
6167
public function __construct(
6268
\Magento\Webapi\Model\Soap\Config $apiConfig,
6369
WsdlFactory $wsdlFactory,
6470
\Magento\Framework\App\Cache\Type\Webapi $cache,
6571
\Magento\Framework\Reflection\TypeProcessor $typeProcessor,
66-
\Magento\Store\Model\StoreManagerInterface $storeManager
72+
\Magento\Store\Model\StoreManagerInterface $storeManager,
73+
\Magento\Framework\Object $customAttributeMap
6774
) {
6875
$this->_apiConfig = $apiConfig;
6976
$this->_wsdlFactory = $wsdlFactory;
7077
$this->_cache = $cache;
7178
$this->_typeProcessor = $typeProcessor;
7279
$this->storeManager = $storeManager;
80+
$this->customAttributeMapArray = array_values($customAttributeMap->getData());
7381
}
7482

7583
/**
@@ -115,6 +123,8 @@ protected function _generate($requestedServices, $endPointUrl)
115123
$wsdl = $this->_wsdlFactory->create(self::WSDL_NAME, $endPointUrl);
116124
$wsdl->addSchemaTypeSection();
117125
$faultMessageName = $this->_addGenericFaultComplexTypeNodes($wsdl);
126+
$wsdl = $this->addCustomAttributeTypes($wsdl);
127+
118128
foreach ($requestedServices as $serviceClass => $serviceData) {
119129
$portTypeName = $this->getPortTypeName($serviceClass);
120130
$bindingName = $this->getBindingName($serviceClass);
@@ -160,6 +170,21 @@ protected function _generate($requestedServices, $endPointUrl)
160170
return $wsdl->toXML();
161171
}
162172

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@
2727
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Webapi</argument>
2828
</arguments>
2929
</type>
30+
<type name="Magento\Webapi\Model\Soap\Wsdl\Generator">
31+
<arguments>
32+
<argument name="customAttributeMap" xsi:type="object">CustomAttributeMap</argument>
33+
</arguments>
34+
</type>
3035
</config>

dev/tests/api-functional/_files/Magento/TestModuleMSC/etc/di.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
<argument name="resource" xsi:type="object">Magento\TestModuleMSC\Model\Resource\Item</argument>
3131
</arguments>
3232
</type>
33-
<type name="Magento\TestModuleMSC\Model\Data\CustomAttributeNestedDataObject">
33+
<virtualType name="CustomAttributeMap" type="Magento\Framework\Object">
3434
<arguments>
35-
<argument name="resource" xsi:type="object">Magento\TestModuleMSC\Model\Resource\Item</argument>
35+
<argument name="data" xsi:type="array">
36+
<item name="customAttributeDataObjectInterface" xsi:type="string">Magento\TestModuleMSC\Api\Data\CustomAttributeDataObjectInterface</item>
37+
<item name="customAttributeNestedDataObjectInterface" xsi:type="string">Magento\TestModuleMSC\Api\Data\CustomAttributeNestedDataObjectInterface</item>
38+
</argument>
3639
</arguments>
37-
</type>
38-
40+
</virtualType>
3941
</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}?wsdl=1&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+
}

dev/tests/unit/testsuite/Magento/Webapi/Model/Soap/Config/ClassReflectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function _getSampleReflectionData()
8282
],
8383
'out' => [
8484
'parameters' => [
85-
'result' => ['type' => 'str', 'documentation' => 'random string', 'required' => true],
85+
'result' => ['type' => 'string', 'documentation' => 'random string', 'required' => true],
8686
],
8787
],
8888
]

dev/tests/unit/testsuite/Magento/Webapi/Model/Soap/Wsdl/GeneratorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ protected function setUp()
9696
'wsdlFactory' => $this->_wsdlFactoryMock,
9797
'cache' => $this->_cacheMock,
9898
'typeProcessor' => $this->_typeProcessor,
99-
'storeManagerMock' => $this->storeManagerMock
99+
'storeManagerMock' => $this->storeManagerMock,
100+
'customAttributeMap' => new \Magento\Framework\Api\CustomAttributeMap()
100101
]
101102
);
102103

@@ -190,6 +191,7 @@ public function testHandleWithException()
190191
$this->_cacheMock,
191192
$this->_typeProcessor,
192193
$this->storeManagerMock,
194+
new \Magento\Framework\Api\CustomAttributeMap()
193195
]
194196
)->getMock();
195197

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ public function setTypeData($typeName, $data)
101101
* Process type name. In case parameter type is a complex type (class) - process its properties.
102102
*
103103
* @param string $type
104-
* @return string
104+
* @return string Complex type name
105105
* @throws \LogicException
106106
*/
107-
public function process($type)
107+
public function register($type)
108108
{
109109
$typeName = $this->normalizeType($type);
110110
if (!$this->isTypeSimple($typeName) && !$this->isTypeAny($typeName)) {
@@ -136,7 +136,7 @@ protected function _processComplexType($class)
136136
$typeName = $this->translateTypeName($class);
137137
$this->_types[$typeName] = [];
138138
if ($this->isArrayType($class)) {
139-
$this->process($this->getArrayItemType($class));
139+
$this->register($this->getArrayItemType($class));
140140
} else {
141141
if (!(class_exists($class) || interface_exists($class))) {
142142
throw new \InvalidArgumentException(
@@ -175,7 +175,7 @@ protected function _processMethod(\Zend\Code\Reflection\MethodReflection $method
175175
$returnMetadata = $this->getGetterReturnType($methodReflection);
176176
$fieldName = $this->dataObjectGetterNameToFieldName($methodReflection->getName());
177177
$this->_types[$typeName]['parameters'][$fieldName] = [
178-
'type' => $this->process($returnMetadata['type']),
178+
'type' => $this->register($returnMetadata['type']),
179179
'required' => $returnMetadata['isRequired'],
180180
'documentation' => $returnMetadata['description'],
181181
];

0 commit comments

Comments
 (0)