[PHP] ObjectSerializer fix for array of objects (#6331)

* [PHP] ObjectSerializer fix for array of objects 

Array of objects translate to "map[string,object][]" and they fail to deserialize. This is because the deserialization does not parse the mapping string correctly. A quick fix is trying moving array deserialization before object deserialization.


Example object
    ObjectExample:
      type: object
      properties:
        data:
          type: array
          items:
            type: object
            additionalProperties: true

* Update sample

Co-authored-by: Alexandru Negrila <alex@arntech.ro>
This commit is contained in:
alxnegrila 2020-05-18 12:23:27 +03:00 committed by GitHub
parent 634291f4b8
commit e5c72a0ab6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 16 deletions

View File

@ -250,6 +250,14 @@ class ObjectSerializer
{ {
if (null === $data) { if (null === $data) {
return null; return null;
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$data = is_string($data) ? json_decode($data) : $data;
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {
$values[] = self::deserialize($value, $subClass, null);
}
return $values;
} elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int] } elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int]
$data = is_string($data) ? json_decode($data) : $data; $data = is_string($data) ? json_decode($data) : $data;
settype($data, 'array'); settype($data, 'array');
@ -263,14 +271,6 @@ class ObjectSerializer
} }
} }
return $deserialized; return $deserialized;
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$data = is_string($data) ? json_decode($data) : $data;
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {
$values[] = self::deserialize($value, $subClass, null);
}
return $values;
} elseif ($class === 'object') { } elseif ($class === 'object') {
settype($data, 'array'); settype($data, 'array');
return $data; return $data;

View File

@ -260,6 +260,14 @@ class ObjectSerializer
{ {
if (null === $data) { if (null === $data) {
return null; return null;
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$data = is_string($data) ? json_decode($data) : $data;
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {
$values[] = self::deserialize($value, $subClass, null);
}
return $values;
} elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int] } elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int]
$data = is_string($data) ? json_decode($data) : $data; $data = is_string($data) ? json_decode($data) : $data;
settype($data, 'array'); settype($data, 'array');
@ -273,14 +281,6 @@ class ObjectSerializer
} }
} }
return $deserialized; return $deserialized;
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$data = is_string($data) ? json_decode($data) : $data;
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {
$values[] = self::deserialize($value, $subClass, null);
}
return $values;
} elseif ($class === 'object') { } elseif ($class === 'object') {
settype($data, 'array'); settype($data, 'array');
return $data; return $data;