[Php] Exclude query params when they're not required (#12120)

* exclude query params when they are not required

* fix check on empty

* update samples

* fix

* add tests

* update test

* Fix style

I guess PHPCodeSniffer would find PSR12 violated but we use CS-Fixer
instead. Anyway, conditions should contain spaces between logical
operators for readability.

* Apply CS-Fixer changes to templates

* Refresh samples

* Add required param to docblock

Co-authored-by: Yuriy Belenko <yura-bely@mail.ru>
This commit is contained in:
Kuzma
2022-04-25 13:16:00 +03:00
committed by GitHub
parent 6f1fa4592b
commit 6b3abd9421
11 changed files with 157 additions and 99 deletions

View File

@@ -28,8 +28,8 @@
namespace OpenAPI\Client;
use OpenAPI\Client\Model\ModelInterface;
use GuzzleHttp\Psr7\Utils;
use OpenAPI\Client\Model\ModelInterface;
/**
* ObjectSerializer Class Doc Comment
@@ -166,6 +166,7 @@ class ObjectSerializer
* @param string $openApiType OpenAPIType eg. array or object
* @param string $style Parameter serialization style
* @param bool $explode Parameter explode option
* @param bool $required Whether query param is required or not
*
* @return array
*/
@@ -174,10 +175,19 @@ class ObjectSerializer
string $paramName,
string $openApiType = 'string',
string $style = 'form',
bool $explode = true
bool $explode = true,
bool $required = true
): array {
// return empty string
if (empty($value)) return ["{$paramName}" => ''];
if (
empty($value)
&& ($value !== false || $openApiType !== 'boolean') // if $value === false and $openApiType ==='boolean' it isn't empty
) {
if ($required) {
return ["{$paramName}" => ''];
} else {
return [];
}
}
$query = [];
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
@@ -359,7 +369,7 @@ class ObjectSerializer
if ($class === 'object') {
settype($data, 'array');
return $data;
} else if ($class === 'mixed') {
} elseif ($class === 'mixed') {
settype($data, gettype($data));
return $data;
}