mirror of
https://github.com/php/php-src.git
synced 2025-08-15 13:38:49 +02:00
Fix GH-15711: SoapClient can't convert BackedEnum to scalar value
Allow SoapClient to use the backing value during response serialization. Closes GH-15803.
This commit is contained in:
parent
ca66a11c36
commit
25289dd08e
4 changed files with 165 additions and 1 deletions
2
NEWS
2
NEWS
|
@ -10,6 +10,8 @@ PHP NEWS
|
||||||
. Fixed bug #73182 (PHP SOAPClient does not support stream context HTTP
|
. Fixed bug #73182 (PHP SOAPClient does not support stream context HTTP
|
||||||
headers in array form). (nielsdos)
|
headers in array form). (nielsdos)
|
||||||
. Fixed bug #62900 (Wrong namespace on xsd import error message). (nielsdos)
|
. Fixed bug #62900 (Wrong namespace on xsd import error message). (nielsdos)
|
||||||
|
. Fixed bug GH-15711 (SoapClient can't convert BackedEnum to scalar value).
|
||||||
|
(nielsdos)
|
||||||
|
|
||||||
12 Sep 2024, PHP 8.3.12
|
12 Sep 2024, PHP 8.3.12
|
||||||
|
|
||||||
|
|
|
@ -826,11 +826,46 @@ static zval *to_zval_hexbin(zval *ret, encodeTypePtr type, xmlNodePtr data)
|
||||||
static zend_string *get_serialization_string_from_zval(zval *data)
|
static zend_string *get_serialization_string_from_zval(zval *data)
|
||||||
{
|
{
|
||||||
switch (Z_TYPE_P(data)) {
|
switch (Z_TYPE_P(data)) {
|
||||||
|
case IS_OBJECT:
|
||||||
|
if (Z_OBJCE_P(data)->ce_flags & ZEND_ACC_ENUM) {
|
||||||
|
if (UNEXPECTED(Z_OBJCE_P(data)->enum_backing_type == IS_UNDEF)) {
|
||||||
|
zend_value_error("Non-backed enums have no default serialization");
|
||||||
|
return zend_empty_string;
|
||||||
|
} else {
|
||||||
|
zval *value = zend_enum_fetch_case_value(Z_OBJ_P(data));
|
||||||
|
return zval_get_string_func(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ZEND_FALLTHROUGH;
|
||||||
default:
|
default:
|
||||||
return zval_get_string_func(data);
|
return zval_get_string_func(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static zend_long get_serialization_long_from_zval(zval *data)
|
||||||
|
{
|
||||||
|
switch (Z_TYPE_P(data)) {
|
||||||
|
case IS_OBJECT:
|
||||||
|
if (Z_OBJCE_P(data)->ce_flags & ZEND_ACC_ENUM) {
|
||||||
|
if (UNEXPECTED(Z_OBJCE_P(data)->enum_backing_type != IS_LONG)) {
|
||||||
|
if (Z_OBJCE_P(data)->enum_backing_type == IS_UNDEF) {
|
||||||
|
zend_value_error("Non-backed enums have no default serialization");
|
||||||
|
} else {
|
||||||
|
zend_value_error("String-backed enum cannot be serialized as int");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
zval *value = zend_enum_fetch_case_value(Z_OBJ_P(data));
|
||||||
|
ZEND_ASSERT(Z_TYPE_P(value) == IS_LONG);
|
||||||
|
return Z_LVAL_P(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ZEND_FALLTHROUGH;
|
||||||
|
default:
|
||||||
|
return zval_get_long(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
|
static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
|
||||||
{
|
{
|
||||||
xmlNodePtr ret, text;
|
xmlNodePtr ret, text;
|
||||||
|
@ -1056,7 +1091,7 @@ static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNode
|
||||||
snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
|
snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
|
||||||
xmlNodeSetContent(ret, BAD_CAST(s));
|
xmlNodeSetContent(ret, BAD_CAST(s));
|
||||||
} else {
|
} else {
|
||||||
zend_string *str = zend_long_to_str(zval_get_long(data));
|
zend_string *str = zend_long_to_str(get_serialization_long_from_zval(data));
|
||||||
xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(str)), ZSTR_LEN(str));
|
xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(str)), ZSTR_LEN(str));
|
||||||
zend_string_release_ex(str, 0);
|
zend_string_release_ex(str, 0);
|
||||||
}
|
}
|
||||||
|
|
88
ext/soap/tests/gh15711.phpt
Normal file
88
ext/soap/tests/gh15711.phpt
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
--TEST--
|
||||||
|
GH-15711 (SoapClient can't convert BackedEnum to scalar value)
|
||||||
|
--EXTENSIONS--
|
||||||
|
soap
|
||||||
|
--INI--
|
||||||
|
soap.wsdl_cache_enabled=0
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
|
||||||
|
enum StringBackedEnum: string
|
||||||
|
{
|
||||||
|
case First = 'BackingValue1';
|
||||||
|
case Second = 'BackingValue2';
|
||||||
|
case Third = 'BackingValue3';
|
||||||
|
case Fourth = 'BackingValue4';
|
||||||
|
case Fifth = 'BackingValue5';
|
||||||
|
}
|
||||||
|
|
||||||
|
enum IntBackedEnum: int
|
||||||
|
{
|
||||||
|
case First = 1;
|
||||||
|
case Second = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum NonBackedEnum
|
||||||
|
{
|
||||||
|
case First;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestSoapClient extends SoapClient {
|
||||||
|
function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
|
||||||
|
echo $request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = new TestSoapClient('ext/soap/tests/gh15711.wsdl', ['classmap' => ['book' => 'book']]);
|
||||||
|
|
||||||
|
echo "--- Test with backed enum ---\n";
|
||||||
|
|
||||||
|
$book = new stdClass();
|
||||||
|
$book->base64 = StringBackedEnum::First;
|
||||||
|
$book->string = StringBackedEnum::Second;
|
||||||
|
$book->any = StringBackedEnum::Third;
|
||||||
|
$book->hexbin = StringBackedEnum::Fourth;
|
||||||
|
$book->nmtokens = StringBackedEnum::Fifth;
|
||||||
|
$book->integer = IntBackedEnum::First;
|
||||||
|
$book->short = IntBackedEnum::Second;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$client->dotest($book);
|
||||||
|
} catch (Throwable) {}
|
||||||
|
|
||||||
|
echo "--- Test with non-backed enum ---\n";
|
||||||
|
|
||||||
|
$book = new stdClass();
|
||||||
|
$book->base64 = NonBackedEnum::First;
|
||||||
|
$book->string = NonBackedEnum::First;
|
||||||
|
$book->any = NonBackedEnum::First;
|
||||||
|
$book->hexbin = NonBackedEnum::First;
|
||||||
|
$book->nmtokens = NonBackedEnum::First;
|
||||||
|
$book->integer = NonBackedEnum::First;
|
||||||
|
$book->short = NonBackedEnum::First;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$client->dotest($book);
|
||||||
|
} catch (ValueError $e) {
|
||||||
|
echo "ValueError: ", $e->getMessage(), "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "--- Test with mismatched enum backing type ---\n";
|
||||||
|
|
||||||
|
$book->integer = StringBackedEnum::First;
|
||||||
|
$book->short = StringBackedEnum::First;
|
||||||
|
try {
|
||||||
|
$client->dotest($book);
|
||||||
|
} catch (ValueError $e) {
|
||||||
|
echo "ValueError: ", $e->getMessage(), "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
--EXPECT--
|
||||||
|
--- Test with backed enum ---
|
||||||
|
<?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: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>
|
||||||
|
--- Test with non-backed enum ---
|
||||||
|
ValueError: Non-backed enums have no default serialization
|
||||||
|
--- Test with mismatched enum backing type ---
|
||||||
|
ValueError: String-backed enum cannot be serialized as int
|
39
ext/soap/tests/gh15711.wsdl
Normal file
39
ext/soap/tests/gh15711.wsdl
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<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">
|
||||||
|
<wsdl:types>
|
||||||
|
<xsd:schema targetNamespace="http://schemas.nothing.com">
|
||||||
|
<xsd:complexType name="book">
|
||||||
|
<xsd:all>
|
||||||
|
<xsd:element name="base64" type="xsd:base64Binary"/>
|
||||||
|
<xsd:element name="string" type="xsd:string"/>
|
||||||
|
<xsd:element name="any" type="xsd:any"/>
|
||||||
|
<xsd:element name="hexbin" type="xsd:hexBinary"/>
|
||||||
|
<xsd:element name="nmtokens" type="xsd:NMTOKENS"/>
|
||||||
|
<xsd:element name="integer" type="xsd:integer"/>
|
||||||
|
<xsd:element name="short" type="xsd:short"/>
|
||||||
|
</xsd:all>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:schema>
|
||||||
|
</wsdl:types>
|
||||||
|
<message name="dotestRequest">
|
||||||
|
<part name="dotestReturn" type="tns:book"/>
|
||||||
|
</message>
|
||||||
|
<portType name="testPortType">
|
||||||
|
<operation name="dotest">
|
||||||
|
<input message="tns:dotestRequest"/>
|
||||||
|
</operation>
|
||||||
|
</portType>
|
||||||
|
<binding name="testBinding" type="tns:testPortType">
|
||||||
|
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
|
||||||
|
<operation name="dotest">
|
||||||
|
<soap:operation soapAction="http://localhost:81/test/interface.php?class=test/dotest" style="rpc"/>
|
||||||
|
<input>
|
||||||
|
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
|
||||||
|
</input>
|
||||||
|
</operation>
|
||||||
|
</binding>
|
||||||
|
<service name="test">
|
||||||
|
<port name="testPort" binding="tns:testBinding">
|
||||||
|
<soap:address location="http://localhost:81/test/interface.php?class=test"/>
|
||||||
|
</port>
|
||||||
|
</service>
|
||||||
|
</wsdl:definitions>
|
Loading…
Add table
Add a link
Reference in a new issue