Skip to content

Commit 736bc4a

Browse files
authored
fix(php): fix deserialization of models (#1265)
1 parent 9402855 commit 736bc4a

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

templates/php/ObjectSerializer.mustache

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@ class ObjectSerializer
5555
if (is_object($data)) {
5656
$values = [];
5757
if ($data instanceof ModelInterface) {
58-
$formats = $data::openAPIFormats();
59-
foreach ($data::openAPITypes() as $property => $openAPIType) {
58+
$formats = $data::modelFormats();
59+
foreach ($data::modelTypes() as $property => $modelTypes) {
6060
$getter = $data::getters()[$property];
6161
$value = $data->$getter();
62-
if ($value !== null && !in_array($openAPIType, [{{{primitives}}}], true)) {
63-
$callable = [$openAPIType, 'getAllowableEnumValues'];
62+
if ($value !== null && !in_array($modelTypes, [{{{primitives}}}], true)) {
63+
$callable = [$modelTypes, 'getAllowableEnumValues'];
6464
if (is_callable($callable)) {
6565
/** array $callable */
6666
$allowedEnumTypes = $callable();
6767
if (!in_array($value, $allowedEnumTypes, true)) {
6868
$imploded = implode("', '", $allowedEnumTypes);
69-
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
69+
throw new \InvalidArgumentException("Invalid value for enum '$modelTypes', must be one of: '$imploded'");
7070
}
7171
}
7272
}
7373
if ($value !== null) {
74-
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
74+
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $modelTypes, $formats[$property]);
7575
}
7676
}
7777
} else {
@@ -319,18 +319,10 @@ class ObjectSerializer
319319
return $data;
320320
} else {
321321
$data = is_string($data) ? json_decode($data) : $data;
322-
// If a discriminator is defined and points to a valid subclass, use it.
323-
$discriminator = $class::DISCRIMINATOR;
324-
if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
325-
$subclass = '\{{invokerPackage}}\Model\\' . $data->{$discriminator};
326-
if (is_subclass_of($subclass, $class)) {
327-
$class = $subclass;
328-
}
329-
}
330322
331323
/** @var ModelInterface $instance */
332324
$instance = new $class();
333-
foreach ($instance::openAPITypes() as $property => $type) {
325+
foreach ($instance::modelTypes() as $property => $type) {
334326
$propertySetter = $instance::setters()[$property];
335327
336328
if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {

templates/php/model_generic.mustache

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ class {{classname}} extends \Algolia\AlgoliaSearch\Model\AbstractModel {{^parent
2020
{{/-last}}{{/vars}}
2121
];
2222

23+
/**
24+
* Array of attributes where the key is the local name,
25+
* and the value is the original name
26+
*
27+
* @var string[]
28+
*/
29+
protected static $attributeMap = [
30+
{{#vars}}'{{name}}' => '{{baseName}}'{{^-last}},
31+
{{/-last}}{{/vars}}
32+
];
33+
34+
/**
35+
* Array of attributes where the key is the local name,
36+
* and the value is the original name
37+
*
38+
* @return array
39+
*/
40+
public static function attributeMap()
41+
{
42+
return {{#parentSchema}}parent::attributeMap() + {{/parentSchema}}self::$attributeMap;
43+
}
44+
2345
/**
2446
* Array of property to type mappings. Used for (de)serialization
2547
*

0 commit comments

Comments
 (0)