Skip to content

Commit 098ca85

Browse files
authored
Merge pull request #8144 from adplugg/php-stdclass-serialization
[PHP] stdClass Serialization
2 parents 907dc9a + 40ea57c commit 098ca85

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class ObjectSerializer
4949
$data[$property] = self::sanitizeForSerialization($value);
5050
}
5151
return $data;
52+
} elseif ($data instanceof \stdClass) {
53+
foreach ($data as $property => $value) {
54+
$data->$property = self::sanitizeForSerialization($value);
55+
}
56+
return $data;
5257
} elseif (is_object($data)) {
5358
$values = [];
5459
$formats = $data::swaggerFormats();

samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public static function sanitizeForSerialization($data, $type = null, $format = n
5959
$data[$property] = self::sanitizeForSerialization($value);
6060
}
6161
return $data;
62+
} elseif ($data instanceof \stdClass) {
63+
foreach ($data as $property => $value) {
64+
$data->$property = self::sanitizeForSerialization($value);
65+
}
66+
return $data;
6267
} elseif (is_object($data)) {
6368
$values = [];
6469
$formats = $data::swaggerFormats();

samples/client/petstore/php/SwaggerClient-php/tests/ObjectSerializerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55
// test object serializer
66
class ObjectSerializerTest extends \PHPUnit_Framework_TestCase
77
{
8+
/**
9+
* Test the sanitizeForSerialization method with a stdClass.
10+
*/
11+
public function testSanitizeForSerializationWithStdClass()
12+
{
13+
// Initialize the ObjectSerializer.
14+
$s = new ObjectSerializer();
15+
16+
// Build a stdClass object.
17+
$obj = new \stdClass();
18+
$obj->prop1 = 'val1';
19+
$obj->prop2 = 'val2';
20+
21+
// Call the method.
22+
$serialized = $s->sanitizeForSerialization($obj);
23+
24+
// Assert that the stdClass object is sanitized as expected.
25+
$this->assertEquals('val1', $serialized->prop1);
26+
$this->assertEquals('val2', $serialized->prop2);
27+
}
28+
829
// test sanitizeFilename
930
public function testSanitizeFilename()
1031
{

0 commit comments

Comments
 (0)