Skip to content

Commit 25289dd

Browse files
committed
Fix GH-15711: SoapClient can't convert BackedEnum to scalar value
Allow SoapClient to use the backing value during response serialization. Closes GH-15803.
1 parent ca66a11 commit 25289dd

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010
. Fixed bug #73182 (PHP SOAPClient does not support stream context HTTP
1111
headers in array form). (nielsdos)
1212
. Fixed bug #62900 (Wrong namespace on xsd import error message). (nielsdos)
13+
. Fixed bug GH-15711 (SoapClient can't convert BackedEnum to scalar value).
14+
(nielsdos)
1315

1416
12 Sep 2024, PHP 8.3.12
1517

ext/soap/php_encoding.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,46 @@ static zval *to_zval_hexbin(zval *ret, encodeTypePtr type, xmlNodePtr data)
826826
static zend_string *get_serialization_string_from_zval(zval *data)
827827
{
828828
switch (Z_TYPE_P(data)) {
829+
case IS_OBJECT:
830+
if (Z_OBJCE_P(data)->ce_flags & ZEND_ACC_ENUM) {
831+
if (UNEXPECTED(Z_OBJCE_P(data)->enum_backing_type == IS_UNDEF)) {
832+
zend_value_error("Non-backed enums have no default serialization");
833+
return zend_empty_string;
834+
} else {
835+
zval *value = zend_enum_fetch_case_value(Z_OBJ_P(data));
836+
return zval_get_string_func(value);
837+
}
838+
}
839+
ZEND_FALLTHROUGH;
829840
default:
830841
return zval_get_string_func(data);
831842
}
832843
}
833844

845+
static zend_long get_serialization_long_from_zval(zval *data)
846+
{
847+
switch (Z_TYPE_P(data)) {
848+
case IS_OBJECT:
849+
if (Z_OBJCE_P(data)->ce_flags & ZEND_ACC_ENUM) {
850+
if (UNEXPECTED(Z_OBJCE_P(data)->enum_backing_type != IS_LONG)) {
851+
if (Z_OBJCE_P(data)->enum_backing_type == IS_UNDEF) {
852+
zend_value_error("Non-backed enums have no default serialization");
853+
} else {
854+
zend_value_error("String-backed enum cannot be serialized as int");
855+
}
856+
return 0;
857+
} else {
858+
zval *value = zend_enum_fetch_case_value(Z_OBJ_P(data));
859+
ZEND_ASSERT(Z_TYPE_P(value) == IS_LONG);
860+
return Z_LVAL_P(value);
861+
}
862+
}
863+
ZEND_FALLTHROUGH;
864+
default:
865+
return zval_get_long(data);
866+
}
867+
}
868+
834869
static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
835870
{
836871
xmlNodePtr ret, text;
@@ -1056,7 +1091,7 @@ static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNode
10561091
snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
10571092
xmlNodeSetContent(ret, BAD_CAST(s));
10581093
} else {
1059-
zend_string *str = zend_long_to_str(zval_get_long(data));
1094+
zend_string *str = zend_long_to_str(get_serialization_long_from_zval(data));
10601095
xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(str)), ZSTR_LEN(str));
10611096
zend_string_release_ex(str, 0);
10621097
}

ext/soap/tests/gh15711.phpt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--TEST--
2+
GH-15711 (SoapClient can't convert BackedEnum to scalar value)
3+
--EXTENSIONS--
4+
soap
5+
--INI--
6+
soap.wsdl_cache_enabled=0
7+
--FILE--
8+
<?php
9+
10+
enum StringBackedEnum: string
11+
{
12+
case First = 'BackingValue1';
13+
case Second = 'BackingValue2';
14+
case Third = 'BackingValue3';
15+
case Fourth = 'BackingValue4';
16+
case Fifth = 'BackingValue5';
17+
}
18+
19+
enum IntBackedEnum: int
20+
{
21+
case First = 1;
22+
case Second = 2;
23+
}
24+
25+
enum NonBackedEnum
26+
{
27+
case First;
28+
}
29+
30+
class TestSoapClient extends SoapClient {
31+
function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
32+
echo $request;
33+
}
34+
}
35+
36+
$client = new TestSoapClient('ext/soap/tests/gh15711.wsdl', ['classmap' => ['book' => 'book']]);
37+
38+
echo "--- Test with backed enum ---\n";
39+
40+
$book = new stdClass();
41+
$book->base64 = StringBackedEnum::First;
42+
$book->string = StringBackedEnum::Second;
43+
$book->any = StringBackedEnum::Third;
44+
$book->hexbin = StringBackedEnum::Fourth;
45+
$book->nmtokens = StringBackedEnum::Fifth;
46+
$book->integer = IntBackedEnum::First;
47+
$book->short = IntBackedEnum::Second;
48+
49+
try {
50+
$client->dotest($book);
51+
} catch (Throwable) {}
52+
53+
echo "--- Test with non-backed enum ---\n";
54+
55+
$book = new stdClass();
56+
$book->base64 = NonBackedEnum::First;
57+
$book->string = NonBackedEnum::First;
58+
$book->any = NonBackedEnum::First;
59+
$book->hexbin = NonBackedEnum::First;
60+
$book->nmtokens = NonBackedEnum::First;
61+
$book->integer = NonBackedEnum::First;
62+
$book->short = NonBackedEnum::First;
63+
64+
try {
65+
$client->dotest($book);
66+
} catch (ValueError $e) {
67+
echo "ValueError: ", $e->getMessage(), "\n";
68+
}
69+
70+
echo "--- Test with mismatched enum backing type ---\n";
71+
72+
$book->integer = StringBackedEnum::First;
73+
$book->short = StringBackedEnum::First;
74+
try {
75+
$client->dotest($book);
76+
} catch (ValueError $e) {
77+
echo "ValueError: ", $e->getMessage(), "\n";
78+
}
79+
80+
?>
81+
--EXPECT--
82+
--- Test with backed enum ---
83+
<?xml version="1.0" encoding="UTF-8"?>
84+
<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:dotest><dotestReturn xsi:type="ns1:book"><base64 xsi:type="xsd:base64Binary">QmFja2luZ1ZhbHVlMQ==</base64><string xsi:type="xsd:string">BackingValue2</string><any xsi:type="xsd:any"><name xsi:type="xsd:string">Third</name><value xsi:type="xsd:string">BackingValue3</value></any><hexbin xsi:type="xsd:hexBinary">4261636B696E6756616C756534</hexbin><nmtokens>BackingValue5</nmtokens><integer xsi:type="xsd:integer">1</integer><short xsi:type="xsd:short">2</short></dotestReturn></ns1:dotest></SOAP-ENV:Body></SOAP-ENV:Envelope>
85+
--- Test with non-backed enum ---
86+
ValueError: Non-backed enums have no default serialization
87+
--- Test with mismatched enum backing type ---
88+
ValueError: String-backed enum cannot be serialized as int

ext/soap/tests/gh15711.wsdl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.nothing.com" targetNamespace="http://schemas.nothing.com">
2+
<wsdl:types>
3+
<xsd:schema targetNamespace="http://schemas.nothing.com">
4+
<xsd:complexType name="book">
5+
<xsd:all>
6+
<xsd:element name="base64" type="xsd:base64Binary"/>
7+
<xsd:element name="string" type="xsd:string"/>
8+
<xsd:element name="any" type="xsd:any"/>
9+
<xsd:element name="hexbin" type="xsd:hexBinary"/>
10+
<xsd:element name="nmtokens" type="xsd:NMTOKENS"/>
11+
<xsd:element name="integer" type="xsd:integer"/>
12+
<xsd:element name="short" type="xsd:short"/>
13+
</xsd:all>
14+
</xsd:complexType>
15+
</xsd:schema>
16+
</wsdl:types>
17+
<message name="dotestRequest">
18+
<part name="dotestReturn" type="tns:book"/>
19+
</message>
20+
<portType name="testPortType">
21+
<operation name="dotest">
22+
<input message="tns:dotestRequest"/>
23+
</operation>
24+
</portType>
25+
<binding name="testBinding" type="tns:testPortType">
26+
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
27+
<operation name="dotest">
28+
<soap:operation soapAction="http://localhost:81/test/interface.php?class=test/dotest" style="rpc"/>
29+
<input>
30+
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
31+
</input>
32+
</operation>
33+
</binding>
34+
<service name="test">
35+
<port name="testPort" binding="tns:testBinding">
36+
<soap:address location="http://localhost:81/test/interface.php?class=test"/>
37+
</port>
38+
</service>
39+
</wsdl:definitions>

0 commit comments

Comments
 (0)