Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data deserialization problem after receiving response from api #12188

Open
wants to merge 1 commit into
base: 3.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,56 @@ public static function deserialize($data, $class, $httpHeaders = null)
$instance = new $class();
foreach ($instance::swaggerTypes() as $property => $type) {
$propertySetter = $instance::setters()[$property];
$propertyName = self::convertToCamelCase($instance::attributeMap()[$property]);
if (!property_exists($data, $propertyName)) {
if (property_exists($data, self::convertCamelCaseToPascalCase($propertyName))) {
$propertyName = self::convertCamelCaseToPascalCase($propertyName);
} else {
$propertyName = self::convertCamelCaseToSnakeCase($propertyName);
}
}

if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {
if (!isset($propertySetter) || !isset($data->{$propertyName})) {
continue;
}

$propertyValue = $data->{$instance::attributeMap()[$property]};
$propertyValue = $data->{$propertyName};
if (isset($propertyValue)) {
$instance->$propertySetter(self::deserialize($propertyValue, $type, null));
}
}
return $instance;
}
}
}

private static function convertToCamelCase($property): string
{
if (str_contains($property, '_')) {
$arrStrings = explode('_', $property);
$count = count($arrStrings);
if ($count > 1) {
foreach ($arrStrings as $key => &$string) {
if ($key > 0) {
$string = ucfirst($string);
}
}
}
return implode($arrStrings);
}
return lcfirst($property);
}

private static function convertCamelCaseToPascalCase($property): string
{
return ucfirst($property);
}

private static function convertCamelCaseToSnakeCase($property): string
{
$chunks = preg_split('/(?=[A-Z])/', $property);
forEach($chunks as &$chunk) {
$chunk = lcfirst($chunk);
}
return implode('_', $chunks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,56 @@ public static function deserialize($data, $class, $httpHeaders = null)
$instance = new $class();
foreach ($instance::swaggerTypes() as $property => $type) {
$propertySetter = $instance::setters()[$property];
$propertyName = self::convertToCamelCase($instance::attributeMap()[$property]);
if (!property_exists($data, $propertyName)) {
if (property_exists($data, self::convertCamelCaseToPascalCase($propertyName))) {
$propertyName = self::convertCamelCaseToPascalCase($propertyName);
} else {
$propertyName = self::convertCamelCaseToSnakeCase($propertyName);
}
}

if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {
if (!isset($propertySetter) || !isset($data->{$propertyName})) {
continue;
}

$propertyValue = $data->{$instance::attributeMap()[$property]};
$propertyValue = $data->{$propertyName};
if (isset($propertyValue)) {
$instance->$propertySetter(self::deserialize($propertyValue, $type, null));
}
}
return $instance;
}
}
}

private static function convertToCamelCase($property): string
{
if (str_contains($property, '_')) {
$arrStrings = explode('_', $property);
$count = count($arrStrings);
if ($count > 1) {
foreach ($arrStrings as $key => &$string) {
if ($key > 0) {
$string = ucfirst($string);
}
}
}
return implode($arrStrings);
}
return lcfirst($property);
}

private static function convertCamelCaseToPascalCase($property): string
{
return ucfirst($property);
}

private static function convertCamelCaseToSnakeCase($property): string
{
$chunks = preg_split('/(?=[A-Z])/', $property);
forEach($chunks as &$chunk) {
$chunk = lcfirst($chunk);
}
return implode('_', $chunks);
}
}