Repository navigation
[CC-4256] Parse Keypair booleans sent as strings - #251
Conversation
validateBasket (and cof) arrive from the API as strings ("true"/"false").
The non-strict setters coerced any non-empty string to true, so "false"
became true. Normalise both flags in handleResponse() via a new
normalizeBoolean() helper (filter_var FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE), accepting native and string booleans; a missing
field stays null. expose() keeps native booleans.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The Keypair boolean normalization change has no active review issues.
No open issues 🎉
What changed in this PR
Fixes Keypair response parsing so string "false" values are not incorrectly coerced to true for validateBasket and cof.
Changes:
- Adds a shared boolean normalization helper for both flags.
- Adds tests for string and native booleans, native boolean exposure, and missing flags remaining
null.
| File | Description |
|---|---|
test/unit/Resources/KeypairTest.php |
Tests boolean parsing, exposed types, and missing flags. |
src/Resources/Keypair.php |
Normalizes both boolean flags when handling API responses. |
Review effort: Balanced
sixer1182
left a comment
There was a problem hiding this comment.
Review: changes requested
The fix does what the ticket asks: validateBasket and cof sent as "true"/"false" (or as native booleans) are parsed correctly, expose() keeps native booleans, and a missing field stays null. I ran the PR head locally: 747 unit tests pass, and "true", "false", "TRUE", "False", "1", "0", "yes", "no", "off" as well as native 1/0 give the right result for both fields. The points below should be addressed before merge.
1. cof still throws a TypeError for null and empty string
For {"cof": null} and {"cof": ""}, Keypair::handleResponse() fails with:
TypeError: Keypair::setCof(): Argument #1 ($cof) must be of type bool, null given
The error happens in parent::handleResponse(), before the new normalization runs, because setCof(bool $cof) is not nullable. This already existed before the PR, but the PR states that cof is made robust, and the ticket asks to check and fix the other bool fields of Keypair. Please make setCof accept ?bool (the property docblock already says bool|null) and add a test.
2. Empty string for validateBasket now gives false instead of null
AbstractUnzerResource turns an empty string into null, so before this PR "validateBasket": "" resulted in null. filter_var('', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) returns false, so the new code changes this to false. That contradicts the acceptance criterion "a missing field leaves the value null". Please treat an empty string as null in normalizeBoolean() (same for cof) and cover it with a test.
3. Missing tests
- Wire values
"1","0", empty string, an invalid string (e.g."abc", expectednull) and JSONnull, for bothvalidateBasketandcof. - A test through
Unzer::fetchKeypair()with a stubbed HTTP adapter returning{"publicKey":"s-pub-stub","validateBasket":"false"}. This is the reproduction path from the ticket and also covers the raw response handling.
4. Suggestion (not blocking): avoid the double write
parent::handleResponse() first writes the wrongly coerced value through the setter, and the new code then overwrites it. It works, but it is fragile. Either fix the setters themselves or normalize the values before calling the parent. Centralizing this in AbstractUnzerResource as a follow-up, as described in the PR, is fine.
5. Open acceptance criterion
The ticket requires removing the skip group from validateBasketModelAgreesWithRawResponseTest in php-sdk-test-automation. The PR defers this until after the release. That is reasonable, but please record it as an explicit open item on the ticket so it is not forgotten.
6. Merge order with #250
The PR description says that #250 needs coordination, while the Jira comment on the ticket says there is no dependency. These contradict each other. I checked: #250 and #251 both change Keypair::handleResponse() and both append to KeypairTest.php. The source file merges cleanly, the test file has a textual conflict. Whichever PR is merged second needs a rebase. Please correct the Jira comment so it matches the PR description.
Changes requested for points 1 to 3. Points 4 to 6 are suggestions or clean-up.
- setCof now accepts ?bool so a null/empty cof no longer throws a TypeError in parent::handleResponse (property docblock already declared bool|null). - normalizeBoolean treats null and empty string as null, so an empty validateBasket/cof stays null instead of being coerced to false, matching AbstractUnzerResource's empty-string-to-null behaviour. - Extend KeypairTest with "1"/"0" wire values and a parameterized test for empty string, invalid string, JSON null and missing flags (stay null, not exposed). - Add ResourceServiceTest coverage for fetchKeypair parsing string booleans, the raw-response reproduction path from the ticket. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for the thorough review @sixer1182 — pushed 1. 2. Empty string → 3. Missing tests — added:
Full unit suite green locally: 756 tests, 2639 assertions. 4. Double write — left as-is for now; agreed it's fragile. Tracking the 5 & 6 (Jira / process) — valid; handling on the ticket: recording the |
sixer1182
left a comment
There was a problem hiding this comment.
All points from my previous review are addressed. I re-ran the PR head (29526f4) locally: 756 unit tests pass. {"cof": null} and {"cof": ""} no longer throw, and an empty string, null and an invalid string give null for both validateBasket and cof. The string and native boolean values still parse correctly.
Not blocking, for the record:
- The double write in
handleResponse()stays until the follow-up that centralizes boolean coercion. - #250 changes
Keypair::handleResponse()andKeypairTest.phpas well. The source merges cleanly, the test file conflicts. Whichever PR is merged second needs a rebase. - Please make sure the open item on php-sdk-test-automation (removing the
skipgroup) is recorded on the ticket.
…G) (#248) * [CC-4078] Rename unreleased changelog section to planned 4.1.0 The PHP SDK release is planned as 4.1.0, not 5.0.0. Rename the top unreleased CHANGELOG section header (and its compare URL) accordingly. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * [CC-4078] Add recurring entry to 4.1.0 changelog Scope the SepaDirectDebit recurring changelog entry under the release ticket (CC-4078) alongside the version rename, so the feature PR #247 (CC-4239) stays code-only and the two don't conflict. * [CC-4078] Complete the 4.1.0 changelog with all released changes Add the Keypair getRawResponse() addition (CC-4151) and the PIS payment-type deprecation (CC-4064) to the 4.1.0 section, alongside the recurring flag (CC-4239) and the PHP 8.1 removal (CC-4228) — the full set of merged changes for the release. * [CC-4078] Add CC-4256 Keypair boolean-parsing fix to 4.1.0 changelog Document the merged fix (#251): Keypair boolean flags sent as strings (validateBasket, cof) were coerced so "false" became true; now parsed correctly. CC-4255 (payment-type duplication, #250) will be added once merged. * [CC-4078] Prepare 4.1.0 release: bump SDK version and complete changelog Bump Unzer::SDK_VERSION to 4.1.0 and stage the remaining 4.1.0 changelog entries: deprecate Sofort (CC-4344, #252) and the Keypair plain-fetch payment-type de-duplication fix (CC-4255, #250). Those two feature PRs must merge before this release-preparation PR. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
The API can send booleans as strings (
"true"/"false"). The non-strict setters coerced any non-empty string totrue, sovalidateBasket:"false"becametrue.cofhad the same latent exposure.Pre-existing bug (not a CC-4151 regression). Note: CC-3759 made the property
protected, which is what first surfaced the wrong value inexpose()/jsonSerialize().Fix: normalise both flags in
handleResponse()via a newnormalizeBoolean()helper (filter_var(FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)), accepting native and string booleans; a missing field staysnull.expose()keeps native booleans. This mirrors the SDK's existing env-var handling inEnvironmentService, which already treats stringified booleans explicitly.Tests
booleanFlagsAreParsedRegardlessOfWireType("true"/"false"/native).missingBooleanFlagsStayNull.php-cs-fixerclean.Dependencies
handleResponse()method insrc/Resources/Keypair.php. This PR currently merges cleanly againstmainonly because CC-4255 is not yet merged. Whichever lands second must be rebased, and the resolution must keep CC-4255'sarray_values()replace of thepaymentTypesappend loop. No functional coupling, but a merge-order coordination point.Follow-up
skipgroup fromvalidateBasketModelAgreesWithRawResponseTestin php-sdk-test-automation once released.AbstractUnzerResource::setItemProperty(reflection on setter param type) so all?boolfields are robust; this per-field helper would then be removed. (Separate ticket — do not widen this bugfix.)🤖 Generated with Claude Code