[php-nextgen] Use the API's configuration for the boolean format of query parameters
Configuration::setBooleanFormatForQueryString() only takes effect on the default configuration. ObjectSerializer::toQueryValue(), ObjectSerializer::convertBoolToQueryStringFormat() and ObjectSerializer::buildQuery() always read the format from Configuration::getDefaultConfiguration(), so the setting is ignored on a configuration that is passed to an API:
$config = (new Configuration())
->setBooleanFormatForQueryString(Configuration::BOOLEAN_FORMAT_STRING);
$api = new SomeApi(null, $config);
$api->someOperation(archived: true); // sends ?archived=1 instead of ?archived=true
Changes
ObjectSerializer::toQueryValue(),ObjectSerializer::convertBoolToQueryStringFormat()andObjectSerializer::buildQuery()take an optional?Configuration $configas their last parameter. If it isnull, they fall back toConfiguration::getDefaultConfiguration(), so existing callers behave as before.- Generated APIs pass
$this->configtotoQueryValue()andbuildQuery(), for both the query string andapplication/x-www-form-urlencodedbodies -> -application/x-www-form-urlencodedbodies are not affected:FormDataProcessoralready converts booleans totrue/falseviaObjectSerializer::toString()(#2257), and the option only applies to query strings. - An API created without a configuration still uses the default configuration (
$config ?: Configuration::getDefaultConfiguration()), so setting the format globally keeps working. toQueryValue()now also converts booleans in non-exploded values (explode: false, e.g.?flags=true,false) with the configured format beforeserializeCollection()joins them. Previously,implode()turned them into1and an empty string (?flags=1,), regardless of the configuration.
Tests
PhpNextgenClientCodegenTest::testQueryParametersUsesConfigurationchecks that the generated API passes its configuration, and thatObjectSerializerno longer reads the format from the default configuration directly.ObjectSerializerTest::testBooleanQueryValuesUseFormatOfGivenConfigurationchecks that a given configuration overrides the default configuration, and that the default configuration is still used when none is given.ObjectSerializerTest::testNonExplodedBooleanQueryValuescovers non-exploded arrays and objects with both formats.
Notes
- The option was introduced in #12385.
int(1/0) became the default becauseguzzlehttp/psr7encodes booleans in query strings as1/0. - #18520 later extended the option to raw booleans in
buildQuery(). - No other client generator sends
1/0. They all sendtrue/false. I'll propose makingtrue/falsethe default for php-nextgen in a separate PR (#25312), keeping theintformat as a deprecated fallback, and removing the option in 8.0.0. - Since
guzzlehttp/psr72.7.0Query::build()has a$treatBoolsAsIntsparameter. Its default is stilltrue. - This PR only changes
php-nextgen. Thephpgenerator has the same behavior.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon
Summary by cubic
Fixes the php-nextgen boolean query format so setBooleanFormatForQueryString() on an API's own configuration is respected, instead of always reading the format from the default configuration.
Previously, ObjectSerializer::toQueryValue(), convertBoolToQueryStringFormat(), and buildQuery() always read the boolean format from Configuration::getDefaultConfiguration(), ignoring the configuration passed to an API instance. These methods now accept an optional ?Configuration parameter and fall back to the default configuration when none is given, so existing callers keep working unchanged. Generated APIs pass $this->config for query strings. application/x-www-form-urlencoded bodies are unaffected: FormDataProcessor already converts booleans to true/false, and the option only applies to query strings.