Skip to content

[CC-4256] Parse Keypair booleans sent as strings - #251

Merged
Ryouzanpaku merged 2 commits into
mainfrom
CC-4256/keypair-string-boolean-parsing
Nov 18, 2026
Merged

Ryouzanpaku merged 2 commits into
mainfrom
CC-4256/keypair-string-boolean-parsing

Conversation

@Ryouzanpaku

@Ryouzanpaku Ryouzanpaku commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The API can send booleans as strings ("true"/"false"). The non-strict setters coerced any non-empty string to true, so validateBasket:"false" became true. cof had 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 in expose()/jsonSerialize().

Fix: 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. This mirrors the SDK's existing env-var handling in EnvironmentService, which already treats stringified booleans explicitly.

Tests

  • Parameterized booleanFlagsAreParsedRegardlessOfWireType ("true"/"false"/native).
  • missingBooleanFlagsStayNull.
  • php-cs-fixer clean.

Dependencies

  • CC-4255 (payment-type duplication) edits the same handleResponse() method in src/Resources/Keypair.php. This PR currently merges cleanly against main only because CC-4255 is not yet merged. Whichever lands second must be rebased, and the resolution must keep CC-4255's array_values() replace of the paymentTypes append loop. No functional coupling, but a merge-order coordination point.

Follow-up

  • Remove the skip group from validateBasketModelAgreesWithRawResponseTest in php-sdk-test-automation once released.
  • Consider centralizing bool coercion in AbstractUnzerResource::setItemProperty (reflection on setter param type) so all ?bool fields are robust; this per-field helper would then be removed. (Separate ticket — do not widen this bugfix.)

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@Ryouzanpaku
Ryouzanpaku marked this pull request as ready for review November 12, 2026 14:56
@Ryouzanpaku
Ryouzanpaku requested a review from a team as a code owner November 12, 2026 14:56

@sixer1182 sixer1182 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Wire values "1", "0", empty string, an invalid string (e.g. "abc", expected null) and JSON null, for both validateBasket and cof.
  2. 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>
@Ryouzanpaku

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @sixer1182 — pushed 29526f46 addressing the blocking points.

1. cof TypeError for null / empty string — setCof now accepts ?bool (matching the existing bool|null property docblock). {"cof": null} and {"cof": ""} no longer throw in parent::handleResponse().

2. Empty string → null for validateBasket (and cof) — normalizeBoolean() now returns null for null/empty string before filter_var, so an empty flag stays null instead of being coerced to false, matching AbstractUnzerResource's empty-string-to-null behaviour.

3. Missing tests — added:

  • KeypairTest: "1"/"0" wire values, plus a parameterized nonBooleanFlagsStayNull covering empty string, invalid string ("abc"), JSON null and missing — all assert the flag stays null and is not exposed.
  • ResourceServiceTest::fetchKeypairParsesBooleanFlagsSentAsStrings: drives Unzer::fetchKeypair() with a stubbed HTTP adapter returning {"publicKey":"s-pub-stub","validateBasket":"false","cof":"false"} (+ "true", empty-string→null and missing→null cases) — the raw-response reproduction path from the ticket.

Full unit suite green locally: 756 tests, 2639 assertions.

4. Double write — left as-is for now; agreed it's fragile. Tracking the AbstractUnzerResource centralization as the follow-up described in the PR.

5 & 6 (Jira / process) — valid; handling on the ticket: recording the skip-group removal in php-sdk-test-automation as an explicit open item, and reconciling the merge-order note (#250 ↔ #251 conflict on KeypairTest.php) between the PR description and the Jira comment so they agree.

@sixer1182 sixer1182 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. The double write in handleResponse() stays until the follow-up that centralizes boolean coercion.
  2. #250 changes Keypair::handleResponse() and KeypairTest.php as well. The source merges cleanly, the test file conflicts. Whichever PR is merged second needs a rebase.
  3. Please make sure the open item on php-sdk-test-automation (removing the skip group) is recorded on the ticket.

@Ryouzanpaku
Ryouzanpaku merged commit 4e3c20f into main Nov 18, 2026
7 checks passed
@Ryouzanpaku
Ryouzanpaku deleted the CC-4256/keypair-string-boolean-parsing branch November 18, 2026 10:21
Ryouzanpaku added a commit that referenced this pull request Nov 23, 2026
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.
Ryouzanpaku added a commit that referenced this pull request Nov 23, 2026
…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>
Ryouzanpaku added a commit that referenced this pull request Nov 24, 2026
Resolve the KeypairTest.php conflict by keeping both independently added
test methods: the plain-endpoint de-duplication test (CC-4255) and the
string-boolean parsing tests brought in via main (CC-4256, #251).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants