feat(php): allow to pass raw boolean to api (#18520)

This allows users to use APIs that require booleans in query params not to be cast to int, e.g. `&foo=true`. Currently, `true` is cast to `1` so it's passed as `&foo=1`. That might not be supported by the target API.

The fix contains copy-pasted function from guzzlehttp/psr7 `Query::build()` with minor tweak.
This commit is contained in:
Simon Podlipsky
2024-05-01 12:10:00 +02:00
committed by GitHub
parent 0768ddcd7c
commit 98d026118c
17 changed files with 528 additions and 179 deletions

View File

@@ -3,6 +3,7 @@
namespace OpenAPI\Client;
use DateTime;
use Generator;
use GuzzleHttp\Psr7\Utils;
use OpenAPI\Client\Model\Pet;
use OpenAPI\Client\Model\Tag;
@@ -571,6 +572,37 @@ class ObjectSerializerTest extends TestCase
];
}
/**
* @covers ObjectSerializer::buildQuery
* @dataProvider provideBuildQuery
*/
public function testToBuildQuery(
string $expected,
array $data,
string|null $booleanFormatString = null,
): void
{
$config = new Configuration();
if ($booleanFormatString !== null) {
$config->setBooleanFormatForQueryString($booleanFormatString);
}
$config::setDefaultConfiguration($config);
$query = ObjectSerializer::buildQuery($data);
$this->assertEquals($expected, $query);
}
/** @return Generator<string, array{string, array<string, mixed>, 2?: Configuration::BOOLEAN_FORMAT_*}> */
public function provideBuildQuery(): Generator
{
yield 'true as int' => ['foo=1', ['foo' => true]];
yield 'true as int as default' => ['foo=1', ['foo' => true], Configuration::BOOLEAN_FORMAT_INT];
yield 'false as int' => ['foo=0', ['foo' => false]];
yield 'false as int as default' => ['foo=0', ['foo' => false], Configuration::BOOLEAN_FORMAT_INT];
yield 'true as string' => ['foo=true', ['foo' => true], Configuration::BOOLEAN_FORMAT_STRING];
yield 'false as string' => ['foo=false', ['foo' => false], Configuration::BOOLEAN_FORMAT_STRING];
}
/**
* Test array to class deserialization.
*