diff --git a/modules/openapi-generator/src/main/resources/php/Configuration.mustache b/modules/openapi-generator/src/main/resources/php/Configuration.mustache index 97a464fc31f..88799976593 100644 --- a/modules/openapi-generator/src/main/resources/php/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/php/Configuration.mustache @@ -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 * diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index 123182a3331..2c6aa836fb9 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -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 diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index 9047b24cadf..5493714b47f 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -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 * diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index 74e6ad9e852..ef602a0f62a 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -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 diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/ObjectSerializerTest.php b/samples/client/petstore/php/OpenAPIClient-php/tests/ObjectSerializerTest.php index de5be5cd787..78eab440584 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/ObjectSerializerTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/ObjectSerializerTest.php @@ -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', + ], + ]; + } }