[Php] Convert boolean value for query string (Configuration) (#12385)

* convert bool to query string format

* add variable

* update samples

* add test
This commit is contained in:
Kuzma 2022-05-25 05:36:21 +03:00 committed by GitHub
parent 4ec14706df
commit 45cbd5f7a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 0 deletions

View File

@ -29,6 +29,9 @@ namespace {{invokerPackage}};
*/
class Configuration
{
public const BOOLEAN_FORMAT_INT = 'int';
public const BOOLEAN_FORMAT_STRING = 'string';
/**
* @var Configuration
*/
@ -55,6 +58,13 @@ class Configuration
*/
protected $accessToken = '';
/**
* Boolean format for query string
*
* @var string
*/
protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_INT;
/**
* Username for HTTP basic authentication
*
@ -187,6 +197,30 @@ class Configuration
return $this->accessToken;
}
/**
* Sets boolean format for query string.
*
* @param string $booleanFormatForQueryString Boolean format for query string
*
* @return $this
*/
public function setBooleanFormatForQueryString(string $booleanFormat)
{
$this->booleanFormatForQueryString = $booleanFormat;
return $this;
}
/**
* Gets boolean format for query string.
*
* @return string Boolean format for query string
*/
public function getBooleanFormatForQueryString(): string
{
return $this->booleanFormatForQueryString;
}
/**
* Sets the username for HTTP basic authentication
*

View File

@ -210,12 +210,32 @@ class ObjectSerializer
return $value;
}
if ('boolean' === $openApiType && is_bool($value)) {
$value = self::convertBoolToQueryStringFormat($value);
}
// handle style in serializeCollection
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
return $query;
}
/**
* Convert boolean value to format for query string.
*
* @param bool $value Boolean value
*
* @return int|string Boolean value in format
*/
public static function convertBoolToQueryStringFormat(bool $value)
{
if (Configuration::BOOLEAN_FORMAT_STRING == Configuration::getDefaultConfiguration()->getBooleanFormatForQueryString()) {
return $value ? 'true' : 'false';
}
return (int) $value;
}
/**
* Take value and turn it into a string suitable for inclusion in
* the header. If it's a string, pass through unchanged

View File

@ -38,6 +38,9 @@ namespace OpenAPI\Client;
*/
class Configuration
{
public const BOOLEAN_FORMAT_INT = 'int';
public const BOOLEAN_FORMAT_STRING = 'string';
/**
* @var Configuration
*/
@ -64,6 +67,13 @@ class Configuration
*/
protected $accessToken = '';
/**
* Boolean format for query string
*
* @var string
*/
protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_INT;
/**
* Username for HTTP basic authentication
*
@ -196,6 +206,30 @@ class Configuration
return $this->accessToken;
}
/**
* Sets boolean format for query string.
*
* @param string $booleanFormatForQueryString Boolean format for query string
*
* @return $this
*/
public function setBooleanFormatForQueryString(string $booleanFormat)
{
$this->booleanFormatForQueryString = $booleanFormat;
return $this;
}
/**
* Gets boolean format for query string.
*
* @return string Boolean format for query string
*/
public function getBooleanFormatForQueryString(): string
{
return $this->booleanFormatForQueryString;
}
/**
* Sets the username for HTTP basic authentication
*

View File

@ -219,12 +219,32 @@ class ObjectSerializer
return $value;
}
if ('boolean' === $openApiType && is_bool($value)) {
$value = self::convertBoolToQueryStringFormat($value);
}
// handle style in serializeCollection
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
return $query;
}
/**
* Convert boolean value to format for query string.
*
* @param bool $value Boolean value
*
* @return int|string Boolean value in format
*/
public static function convertBoolToQueryStringFormat(bool $value)
{
if (Configuration::BOOLEAN_FORMAT_STRING == Configuration::getDefaultConfiguration()->getBooleanFormatForQueryString()) {
return $value ? 'true' : 'false';
}
return (int) $value;
}
/**
* Take value and turn it into a string suitable for inclusion in
* the header. If it's a string, pass through unchanged

View File

@ -389,4 +389,46 @@ class ObjectSerializerTest extends TestCase
],
];
}
/**
* @covers ObjectSerializer::toQueryValue
* @dataProvider provideQueryParamsWithStringBooleanFormatForQueryString
*/
public function testToQueryValueWithStringBooleanFormatForQueryString(
$data,
string $paramName,
string $openApiType,
string $style,
bool $explode,
bool $required,
$expected
): void
{
$config = new Configuration();
$config->setBooleanFormatForQueryString(Configuration::BOOLEAN_FORMAT_STRING);
$config::setDefaultConfiguration($config);
$value = ObjectSerializer::toQueryValue($data, $paramName, $openApiType, $style, $explode, $required);
$query = ObjectSerializer::buildQuery($value);
$this->assertEquals($expected, $query);
}
public function provideQueryParamsWithStringBooleanFormatForQueryString(): array
{
return [
// style form
// skipValidation
'form boolean, required false' => [
false, 'skipValidation', 'boolean', 'form', true, false, 'skipValidation=false',
],
'form empty boolean, required false' => [
null, 'skipValidation', 'boolean', 'form', true, false, '',
],
'form empty boolean, required true' => [
null, 'skipValidation', 'boolean', 'form', true, true, 'skipValidation=',
],
'form true boolean, required true' => [
true, 'skipValidation', 'boolean', 'form', true, false, 'skipValidation=true',
],
];
}
}