[php-nextgen] Serialize booleans in header and path parameters as true/false

PA
3d ago
+173-16

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: false is sent as an empty header, and X-Flag: true as X-Flag: 1. The echo API's testHeaderIntegerBooleanStringEnums(boolean_header: true) sends boolean_header: 1.
  • A path parameter false produces an empty path segment (/flags//...), which typically leads to a different route or a 404.
  • Array parameters [true, false] become 1, (or 1 , 1|, 1\t, depending on the style), which loses the false values entirely and makes [true, false] indistinguishable from [true, false, false].

Cause

  • Single values: toHeaderValue() and toPathValue() declare their parameter as string. The generated API classes don't use strict_types, so PHP coerces true/false to "1"/"" when the method is called, before toString() 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() uses implode(), which casts booleans the same way. The multi branch uses http_build_query(), which turns them into 1/0.

Changes

ObjectSerializer.mustache:

  • toHeaderValue() and toPathValue() accept string|bool. Booleans now reach toString() and become true/false.
  • serializeCollection() converts booleans with toString() before joining. This covers every style (csv, ssv/spaceDelimited, tsv, pipes/pipeDelimited) and the multi branch, and works on nested arrays (array_walk_recursive), since the multi branch 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.yaml with 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::testBooleanHeaderAndPathParamsAreSerializedAsTrueOrFalse generates a client from that spec and checks that arrays go through serializeCollection(), single values through toHeaderValue()/toPathValue(), and that ObjectSerializer accepts and converts booleans.
  • ObjectSerializerTest:
    • testSerializeCollectionWithBooleans: data provider for csv, ssv, spaceDelimited, tsv, pipes, pipeDelimited, multi (nested) and a mixed array ([true, 0, 'false', false] → true,0,false,false).
    • testToHeaderAndPathValueWithBooleans: true/false for both methods.
  • Manually verified on the generated petstore client: FakeApi::testGroupParametersRequest() with required_boolean_group => true and boolean_group => false now sends the headers true and false (previously 1 and an empty string).

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
    
    (For Windows users, please run the script in WSL)
    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() and toPathValue() now accept booleans, which previously got cast to "1"/"" by PHP before serialization.
  • serializeCollection() converts booleans before joining, so boolean arrays no longer lose false values or produce ambiguous output across all collection styles.