[php-nextgen] Serialize booleans in header and path parameters as true/false
PA
3d ago+173+173+012345678901234567890123456789-16-16-01234567890123456789
Boolean header and path parameters are currently sent as 1 and an empty string instead of true and false. This affects single values as well as arrays of booleans.
ObjectSerializer::toHeaderValue(true); // "1" expected "true"
ObjectSerializer::toPathValue(false); // "" expected "false"
ObjectSerializer::serializeCollection([true, false], 'csv'); // "1," expected "true,false"
ObjectSerializer::serializeCollection([true, false], 'pipes'); // "1|" expected "true|false"
In practice this means:
- An optional header
X-Flag: falseis sent as an empty header, andX-Flag: trueasX-Flag: 1. The echo API'stestHeaderIntegerBooleanStringEnums(boolean_header: true)sendsboolean_header: 1. - A path parameter
falseproduces an empty path segment (/flags//...), which typically leads to a different route or a 404. - Array parameters
[true, false]become1,(or1,1|,1\t, depending on the style), which loses thefalsevalues entirely and makes[true, false]indistinguishable from[true, false, false].
Cause
- Single values:
toHeaderValue()andtoPathValue()declare their parameter asstring. The generated API classes don't usestrict_types, so PHP coercestrue/falseto"1"/""when the method is called, beforetoString()runs.toString()would map booleans to'true'/'false', but it never receives one. - Arrays: header and path arrays are joined by
serializeCollection()before they are passed on (api.mustache).serializeCollection()usesimplode(), which casts booleans the same way. Themultibranch useshttp_build_query(), which turns them into1/0.
Changes
ObjectSerializer.mustache:
toHeaderValue()andtoPathValue()acceptstring|bool. Booleans now reachtoString()and becometrue/false.serializeCollection()converts booleans withtoString()before joining. This covers every style (csv,ssv/spaceDelimited,tsv,pipes/pipeDelimited) and themultibranch, and works on nested arrays (array_walk_recursive), since themultibranch is meant for['name' => [...]]input.
Header and path parameters always use true/false. Configuration::setBooleanFormatForQueryString() only applies to query strings and is deliberately not used here.
Tests
- New spec
src/test/resources/3_0/php-nextgen/boolean-header-path-params.yamlwith a boolean and a boolean-array parameter both in the path and in a header. None of the existing specs had a boolean array in a header or path parameter. PhpNextgenClientCodegenTest::testBooleanHeaderAndPathParamsAreSerializedAsTrueOrFalsegenerates a client from that spec and checks that arrays go throughserializeCollection(), single values throughtoHeaderValue()/toPathValue(), and thatObjectSerializeraccepts and converts booleans.ObjectSerializerTest:testSerializeCollectionWithBooleans: data provider forcsv,ssv,spaceDelimited,tsv,pipes,pipeDelimited,multi(nested) and a mixed array ([true, 0, 'false', false]→true,0,false,false).testToHeaderAndPathValueWithBooleans:true/falsefor both methods.
- Manually verified on the generated petstore client:
FakeApi::testGroupParametersRequest()withrequired_boolean_group => trueandboolean_group => falsenow sends the headerstrueandfalse(previously1and an empty string).
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - 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 PHP NextGen boolean header and path parameters so they serialize as true/false instead of 1 and an empty string.
toHeaderValue()andtoPathValue()now accept booleans, which previously got cast to"1"/""by PHP before serialization.serializeCollection()converts booleans before joining, so boolean arrays no longer losefalsevalues or produce ambiguous output across all collection styles.