Repository navigation
Fix data-URL strings decoding as binary values - #295
EhsanAzish80 wants to merge 2 commits into
Conversation
|
This fixes the accidental data-URL coercion, but it also makes the public
case let .data(mimeType, value):
try container.encode(value.dataURLEncoded(mimeType: mimeType))while this PR now decodes every JSON string as let original = Value.data(mimeType: "application/octet-stream", Data([0, 1, 2]))
let decoded = try JSONDecoder().decode(Value.self, from: JSONEncoder().encode(original))changes type from If generic JSON strings must never be auto-promoted to binary (which makes sense), could AI-assisted review; checked the current PR head and Value Codable paths before posting. |
|
I kept JSON strings as strings and documented that Value.data is not lossless through generic JSON decoding. The new test shows how to recover the bytes with Data.parseDataURL. All 554 Swift tests passed locally. AI helped write the test; I ran it. Could a maintainer review this choice and approve CI? |
|
Hi @EhsanAzish80, thanks for this PR! I've reviewed it and it looks good to merge. Could you please rebase it onto the latest If you're no longer able to work on this, no problem: just let us know. If we don't hear back in about two weeks, we may carry the change forward in a new PR, with credit to you. |
f7c51f7 to
a925398
Compare
a925398 to
7f454f4
Compare
akhilcb
left a comment
There was a problem hiding this comment.
Verified locally: the new tests fail on main and pass here, and the full suite passes. This matches the MCP spec: binary data only travels in explicitly typed base64 fields (ImageContent.data, BlobResourceContents.blob), so free-form JSON strings should come back exactly as sent.
Optional: since Value decoding no longer produces .data, it might be worth noting this as a behavior change in the PR description (as #123 did), and mentioning Data.parseDataURL(_:) in the .data doc comment for callers who relied on dataValue.
Fixes #277. JSON strings that look like data URLs now stay strings, so tool text is preserved when it passes through
Value. This is an independently tested alternative to #278.This changes how generic JSON decoding handles
Value.data: it encodes as a data-URL string, but decodes back as.string. Callers that need the bytes can useData.parseDataURL(_:).Tests: 561 Swift tests and CI passed. AI was used to write the tests. I ran them locally.