Skip to content

ext/soap: setting xml namespace in classmap #12411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions ext/soap/php_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,20 @@ static encodePtr find_encoder_by_type_name(sdlPtr sdl, const char *type)
encodePtr enc;

ZEND_HASH_FOREACH_PTR(sdl->encoders, enc) {
if (strcmp(enc->details.type_str, type) == 0) {
return enc;
}

if (type[0] == '{') {
size_t ns_len = strlen(enc->details.ns);
if (strncmp(enc->details.ns, type + 1, ns_len) == 0
&& type[ns_len + 1] == '}'
&& strcmp(enc->details.type_str, type + ns_len + 2) == 0) {
return enc;
}
} else {
if (strcmp(enc->details.type_str, type) == 0) {
return enc;
}
}

} ZEND_HASH_FOREACH_END();
}
return NULL;
Expand Down Expand Up @@ -1380,8 +1391,15 @@ static zval *to_zval_object_ex(zval *ret, encodeTypePtr type, xmlNodePtr data, z
} else if (SOAP_GLOBAL(class_map) && type->type_str) {
zval *classname;
zend_class_entry *tmp;

if ((classname = zend_hash_str_find_deref(SOAP_GLOBAL(class_map), type->type_str, strlen(type->type_str))) != NULL &&
classname = zend_hash_str_find_deref(SOAP_GLOBAL(class_map), type->type_str, strlen(type->type_str));
if(classname == NULL){
if (type->ns) {
zend_string *nscat =zend_strpprintf(0, "{%s}%s", type->ns, type->type_str);
classname = zend_hash_find_deref(SOAP_GLOBAL(class_map),nscat);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think it's better to change the order (first search in namespace).
  • Usage of zend_strpprintf() on each lookup looks inefficient (additional memory allocation/deallocation). It would be great to find a better solution.

zend_string_release_ex(nscat, 0);
}
}
if (classname != NULL &&
Z_TYPE_P(classname) == IS_STRING &&
(tmp = zend_fetch_class(Z_STR_P(classname), ZEND_FETCH_CLASS_AUTO)) != NULL) {
ce = tmp;
Expand Down
46 changes: 46 additions & 0 deletions ext/soap/tests/classmap005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
SOAP Classmap 5: SoapClient support for classmap with namespace
--EXTENSIONS--
soap
--INI--
soap.wsdl_cache_enabled=0
--FILE--
<?php
class TestSoapClient extends SoapClient{
function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
return <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.nothing.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>
<ns1:dotest2Response><res xsi:type="ns1:book">
<a xsi:type="xsd:string">Blaat</a>
<b xsi:type="xsd:string">aap</b>
</res>
</ns1:dotest2Response></SOAP-ENV:Body></SOAP-ENV:Envelope>
EOF;
}
}

class bookNs{
public $a="a";
public $b="c";

}

$options=Array(
'actor' =>'http://schema.nothing.com',
'classmap' => array('{http://schemas.nothing.com}book'=>'bookNs', 'wsdltype2'=>'classname2')
);

$client = new TestSoapClient(__DIR__."/classmap.wsdl",$options);
$ret = $client->dotest2("???");
var_dump($ret);
echo "ok\n";
?>
--EXPECT--
object(bookNs)#2 (2) {
["a"]=>
string(5) "Blaat"
["b"]=>
string(3) "aap"
}
ok
60 changes: 60 additions & 0 deletions ext/soap/tests/classmap006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
SOAP Classmap 6: encoding of inherited objects with namespace
--EXTENSIONS--
soap
--FILE--
<?php
ini_set("soap.wsdl_cache_enabled",0);

class A {
public $x;
function __construct($a){
$this->x = $a;
}
}
class Attest {
public $x;
function __construct($a){
$this->x = $a;
}
}
class B extends A {
public $y;
function __construct($a){
parent::__construct($a);
$this->y = $a + 1;
}
}

function f($input){
return new B(5);
}

class LocalSoapClient extends SoapClient {
private $server;

function __construct($wsdl, $options) {
parent::__construct($wsdl, $options);
$this->server = new SoapServer($wsdl, $options);
$this->server->addFunction("f");
}

function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
ob_start();
$this->server->handle($request);
$response = ob_get_contents();
ob_end_clean();
return $response;
}
}

$client = new LocalSoapClient(__DIR__."/classmap006.wsdl",
array('classmap'=>array('A'=>'A','{urn:abt}At'=>'Attest','B'=>'B')));
print_r($client->f(new Attest('test')));
?>
--EXPECT--
B Object
(
[x] => 5
[y] => 6
)
70 changes: 70 additions & 0 deletions ext/soap/tests/classmap006.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version='1.0' encoding='UTF-8'?>

<!-- WSDL file generated by Zend Studio. -->

<definitions name="ab" targetNamespace="urn:ab" xmlns:typens="urn:ab" xmlns:typenst="urn:abt" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:ab">
<xsd:complexType name="A">
<xsd:sequence>
<xsd:element name="x" type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="B">
<xsd:complexContent>
<xsd:extension base="typens:A">
<xsd:sequence>
<xsd:element name="y" type="xsd:anyType"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:abt">
<xsd:complexType name="At">
<xsd:sequence>
<xsd:element name="x" type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Bt">
<xsd:complexContent>
<xsd:extension base="typens:A">
<xsd:sequence>
<xsd:element name="y" type="xsd:anyType"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>

</types>
<message name="f">
<part name="fInput" type="xsd:anyType"/>
</message>
<message name="fResponse">
<part name="fReturn" type="typens:A"/>
</message>
<portType name="abServerPortType">
<operation name="f">
<input message="typens:f"/>
<output message="typens:fResponse"/>
</operation>
</portType>
<binding name="abServerBinding" type="typens:abServerPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="f">
<soap:operation soapAction="urn:abServerAction"/>
<input>
<soap:body namespace="urn:ab" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body namespace="urn:ab" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="abService">
<port name="abServerPort" binding="typens:abServerBinding">
<soap:address location="http://localhost/abServer.php"/>
</port>
</service>
</definitions>