Replace array_map with Parse::to_list for robust list parsing - #489
Draft
razor-x wants to merge 2 commits into
Draft
Replace array_map with Parse::to_list for robust list parsing#489razor-x wants to merge 2 commits into
razor-x wants to merge 2 commits into
Conversation
…uses Seam adds event types, action types, and error codes between SDK releases, so a payload this version does not recognize should stay readable rather than cost the caller the whole response. A list property the API sends as a scalar no longer fails the whole response. The generated classes mapped these with array_map, which raises a TypeError when handed anything but an array, so a single unexpected field took down every other field alongside it. They now route through Seam\Parse::to_list, which reads a non-list as empty. Waiting on an action attempt whose status is neither pending, success, nor error raises the new ActionAttemptUnknownStatusError. The resolver previously treated an unrecognized status as non-terminal and polled until the deadline, then reported a timeout that misdescribed what happened. The error subclasses ActionAttemptError, so existing handlers for that base keep working. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M2kJ4nGaM8imZCVMEKjmXA
The added comments ran to roughly double the density of the code around them and mostly restated what the line below already said. Kept the ones carrying information the code cannot: why svix/util has to be required, why both key shapes are accepted after symbolize_names, and why array_map needed replacing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M2kJ4nGaM8imZCVMEKjmXA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This change improves the robustness of JSON parsing throughout the SDK by replacing direct
array_mapcalls with a newParse::to_listhelper method. This prevents TypeErrors when the API sends list properties as scalars or null values, allowing responses to degrade gracefully instead of failing entirely.Key Changes
New
Parse::to_listhelper: Createdsrc/Parse.phpwith a utility method that safely converts values to lists, returning an empty array if the value is not an array. This preventsarray_mapTypeErrors when encountering unexpected data shapes.Updated all resource classes: Replaced
array_map($callback, $json->property ?? [])patterns withParse::to_list($json->property ?? null, $callback)across all resource files including:New error class: Added
ActionAttemptUnknownStatusErrorinsrc/ActionAttemptUnknownStatusError.phpto handle action attempts with unrecognized status values. This prevents the polling mechanism from incorrectly claiming success or timing out when encountering new status codes added after the SDK release.Updated polling logic: Modified
src/Http/ResolveActionAttempt.phpto throwActionAttemptUnknownStatusErrorwhen encountering an unrecognized status during polling, rather than treating it as pending.Comprehensive test coverage: Added
tests/TotalParsingTest.phpwith test cases covering:Updated code generator: Modified
codegen/lib/layouts/resource.tsto generate the newParse::to_listpattern for future resource generation.Implementation Details
The
Parse::to_listmethod provides a single point of control for safe list parsing, ensuring that any unexpected data shape (scalar, null, or other non-array value) degrades to an empty list rather than raising an exception. This aligns with the SDK's philosophy of never failing the entire response over a single field, allowing callers to work with partial data when the API sends unexpected shapes.https://claude.ai/code/session_01M2kJ4nGaM8imZCVMEKjmXA