Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ class ObjectSerializer
};
}

/**
* Convert objects into arrays, recursively.
*
* sanitizeForSerialization() returns an object per model, but query parameters
* are flattened from arrays.
*
* @param mixed $data
*
* @return mixed the data with every object converted to an array
*/
private static function toArrayRecursive(mixed $data): mixed
{
if (is_array($data) || is_object($data)) {
$result = [];

foreach ($data as $key => $value) {
$result[$key] = self::toArrayRecursive($value);
}

return $result;
}

return $data;
}

/**
* Take query parameter properties and turn it into an array suitable for
* native http_build_query or GuzzleHttp\Psr7\Query::build.
Expand Down Expand Up @@ -237,8 +262,19 @@ class ObjectSerializer
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

// A model is typed with its class name rather than "object", but serializes as one.
if ($value instanceof ModelInterface) {
$openApiType = 'object';
}

$query = [];
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
if ($openApiType === 'object' && is_object($value)) {
// Read the model's values through its getters; a plain (array) cast
// would expose only the protected $container holding them.
$value = self::toArrayRecursive(self::sanitizeForSerialization($value));
} elseif (in_array($openApiType, ['object', 'array'], true)) {
$value = (array) $value;
}

// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</coverage>
<testsuites>
<testsuite name="tests">
<directory>{{apiTestPath}}</directory>
<directory>{{modelTestPath}}</directory>
<directory>./{{testBasePath}}</directory>
</testsuite>
</testsuites>
<php>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</coverage>
<testsuites>
<testsuite name="tests">
<directory>./tests/Api</directory>
<directory>./tests/Model</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,31 @@ private static function isEmptyValue(mixed $value, string $openApiType): bool
};
}

/**
* Convert objects into arrays, recursively.
*
* sanitizeForSerialization() returns an object per model, but query parameters
* are flattened from arrays.
*
* @param mixed $data
*
* @return mixed the data with every object converted to an array
*/
private static function toArrayRecursive(mixed $data): mixed
{
if (is_array($data) || is_object($data)) {
$result = [];

foreach ($data as $key => $value) {
$result[$key] = self::toArrayRecursive($value);
}

return $result;
}

return $data;
}

/**
* Take query parameter properties and turn it into an array suitable for
* native http_build_query or GuzzleHttp\Psr7\Query::build.
Expand Down Expand Up @@ -246,8 +271,19 @@ public static function toQueryValue(
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

// A model is typed with its class name rather than "object", but serializes as one.
if ($value instanceof ModelInterface) {
$openApiType = 'object';
}

$query = [];
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
if ($openApiType === 'object' && is_object($value)) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
// Read the model's values through its getters; a plain (array) cast
// would expose only the protected $container holding them.
$value = self::toArrayRecursive(self::sanitizeForSerialization($value));
} elseif (in_array($openApiType, ['object', 'array'], true)) {
$value = (array) $value;
}

// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
Expand Down
3 changes: 1 addition & 2 deletions samples/client/echo_api/php-nextgen/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</coverage>
<testsuites>
<testsuite name="tests">
<directory>./tests/Api</directory>
<directory>./tests/Model</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
Expand Down
38 changes: 37 additions & 1 deletion samples/client/echo_api/php-nextgen/src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,31 @@ private static function isEmptyValue(mixed $value, string $openApiType): bool
};
}

/**
* Convert objects into arrays, recursively.
*
* sanitizeForSerialization() returns an object per model, but query parameters
* are flattened from arrays.
*
* @param mixed $data
*
* @return mixed the data with every object converted to an array
*/
private static function toArrayRecursive(mixed $data): mixed
{
if (is_array($data) || is_object($data)) {
$result = [];

foreach ($data as $key => $value) {
$result[$key] = self::toArrayRecursive($value);
}

return $result;
}

return $data;
}

/**
* Take query parameter properties and turn it into an array suitable for
* native http_build_query or GuzzleHttp\Psr7\Query::build.
Expand Down Expand Up @@ -246,8 +271,19 @@ public static function toQueryValue(
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

// A model is typed with its class name rather than "object", but serializes as one.
if ($value instanceof ModelInterface) {
$openApiType = 'object';
}

$query = [];
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
if ($openApiType === 'object' && is_object($value)) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
// Read the model's values through its getters; a plain (array) cast
// would expose only the protected $container holding them.
$value = self::toArrayRecursive(self::sanitizeForSerialization($value));
} elseif (in_array($openApiType, ['object', 'array'], true)) {
$value = (array) $value;
Comment thread
Frankisgek marked this conversation as resolved.
}

// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
</coverage>
<testsuites>
<testsuite name="tests">
<directory>./tests/Api</directory>
<directory>./tests/Model</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ private static function isEmptyValue(mixed $value, string $openApiType): bool
};
}

/**
* Convert objects into arrays, recursively.
*
* sanitizeForSerialization() returns an object per model, but query parameters
* are flattened from arrays.
*
* @param mixed $data
*
* @return mixed the data with every object converted to an array
*/
private static function toArrayRecursive(mixed $data): mixed
{
if (is_array($data) || is_object($data)) {
$result = [];

foreach ($data as $key => $value) {
$result[$key] = self::toArrayRecursive($value);
}

return $result;
}

return $data;
}

/**
* Take query parameter properties and turn it into an array suitable for
* native http_build_query or GuzzleHttp\Psr7\Query::build.
Expand Down Expand Up @@ -245,8 +270,19 @@ public static function toQueryValue(
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
}

// A model is typed with its class name rather than "object", but serializes as one.
if ($value instanceof ModelInterface) {
$openApiType = 'object';
}

$query = [];
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
if ($openApiType === 'object' && is_object($value)) {
// Read the model's values through its getters; a plain (array) cast
// would expose only the protected $container holding them.
$value = self::toArrayRecursive(self::sanitizeForSerialization($value));
} elseif (in_array($openApiType, ['object', 'array'], true)) {
$value = (array) $value;
}

// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
Expand Down
Loading
Loading