fix: preserve data-URL-looking JSON strings - #278
Open
bitbemol wants to merge 1 commit into
Open
Conversation
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.
Fixes #277.
Preserves JSON strings decoded through
Valueas.string, including strings that match data-URL syntax. ExplicitValue.dataencoding and the data-URL helper APIs remain available, and the public API documentation now clarifies that parsing is opt-in.Motivation and Context
Value.init(from:)currently interprets any parseable data-URL-looking JSON string as.data. Because generic JSON contains no discriminator between an ordinary string and an encodedValue.data, this can silently change both theValuecase and the string's spelling.For example:
This can also affect content explicitly tagged as MCP text when it passes through generic
Valueconversion.The change makes the string branch of
Value.init(from:)always produce.string. Applications that intentionally accept data URLs can continue to opt in throughData.parseDataURL(_:), while explicitly constructedValue.datavalues retain their existing encoding.How Has This Been Tested?
Added regression coverage for:
application/octet-streamdata-URL-looking stringsValue.dataencoding.datadiscriminator on the generic JSON wireValueerasureVerification performed:
swift test --filter ValueTests: 8/8 tests passedgit diff --check: cleanThe regression tests were also run against the previous decoder implementation to confirm that they reproduce the reported behavior.
Breaking Changes
This intentionally changes observable behavior for callers that relied on generic
Valuedecoding to automatically convert data-URL-looking JSON strings into.data.Those callers will need to explicitly call
Data.parseDataURL(_:)in fields where data URLs are expected. Encoding an explicitly constructedValue.dataremains unchanged, as does typed image and audio content serialization.Types of changes
Checklist
Additional context
This behavior was encountered in bitbemol/second-brain-mcp, which currently vendors the patched SDK pending an upstream resolution.
This is my first contribution to
swift-sdk. I may be missing context behind the original implicit decoding behavior and am happy to adjust the approach based on maintainer feedback.AI assistance disclosure: This bug was initially identified with LLM assistance, and Codex assisted with implementation analysis and regression-test development. I personally reviewed the relevant SDK behavior, reproduced the failure in Xcode with the previous decoder, and verified the fix with the complete test suite.