forked from loafle/openapi-generator-original
[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:
@@ -29,6 +29,9 @@ namespace {{invokerPackage}};
|
|||||||
*/
|
*/
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
|
public const BOOLEAN_FORMAT_INT = 'int';
|
||||||
|
public const BOOLEAN_FORMAT_STRING = 'string';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Configuration
|
* @var Configuration
|
||||||
*/
|
*/
|
||||||
@@ -55,6 +58,13 @@ class Configuration
|
|||||||
*/
|
*/
|
||||||
protected $accessToken = '';
|
protected $accessToken = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boolean format for query string
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_INT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Username for HTTP basic authentication
|
* Username for HTTP basic authentication
|
||||||
*
|
*
|
||||||
@@ -187,6 +197,30 @@ class Configuration
|
|||||||
return $this->accessToken;
|
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
|
* Sets the username for HTTP basic authentication
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -210,12 +210,32 @@ class ObjectSerializer
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('boolean' === $openApiType && is_bool($value)) {
|
||||||
|
$value = self::convertBoolToQueryStringFormat($value);
|
||||||
|
}
|
||||||
|
|
||||||
// handle style in serializeCollection
|
// handle style in serializeCollection
|
||||||
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
|
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
|
||||||
|
|
||||||
return $query;
|
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
|
* Take value and turn it into a string suitable for inclusion in
|
||||||
* the header. If it's a string, pass through unchanged
|
* the header. If it's a string, pass through unchanged
|
||||||
|
|||||||
@@ -38,6 +38,9 @@ namespace OpenAPI\Client;
|
|||||||
*/
|
*/
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
|
public const BOOLEAN_FORMAT_INT = 'int';
|
||||||
|
public const BOOLEAN_FORMAT_STRING = 'string';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Configuration
|
* @var Configuration
|
||||||
*/
|
*/
|
||||||
@@ -64,6 +67,13 @@ class Configuration
|
|||||||
*/
|
*/
|
||||||
protected $accessToken = '';
|
protected $accessToken = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boolean format for query string
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_INT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Username for HTTP basic authentication
|
* Username for HTTP basic authentication
|
||||||
*
|
*
|
||||||
@@ -196,6 +206,30 @@ class Configuration
|
|||||||
return $this->accessToken;
|
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
|
* Sets the username for HTTP basic authentication
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -219,12 +219,32 @@ class ObjectSerializer
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('boolean' === $openApiType && is_bool($value)) {
|
||||||
|
$value = self::convertBoolToQueryStringFormat($value);
|
||||||
|
}
|
||||||
|
|
||||||
// handle style in serializeCollection
|
// handle style in serializeCollection
|
||||||
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
|
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
|
||||||
|
|
||||||
return $query;
|
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
|
* Take value and turn it into a string suitable for inclusion in
|
||||||
* the header. If it's a string, pass through unchanged
|
* the header. If it's a string, pass through unchanged
|
||||||
|
|||||||
@@ -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',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user