fix: skip JSON pre-parsing for str union annotations in pre_parse_json#2462
Open
Christian-Sidak wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
fix: skip JSON pre-parsing for str union annotations in pre_parse_json#2462Christian-Sidak wants to merge 3 commits intomodelcontextprotocol:mainfrom
Christian-Sidak wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
The `pre_parse_json` method used an identity check (`field_info.annotation is not str`) to decide whether to attempt `json.loads` on string values. For union types like `str | None`, this check passed because `str | None` is not identical to `str`, causing string values to be unnecessarily JSON-parsed. This could corrupt string identifiers (UUIDs, etc.) when the parsed result was a non-scalar type like a dict or list. Replace the identity check with a `_should_pre_parse_json` helper that inspects union annotations. Unions containing only simple scalar types (str, int, float, bool, NoneType) skip pre-parsing entirely, while complex unions like `list[str] | None` still benefit from it. Closes modelcontextprotocol#1873
Remove extra blank line before _SIMPLE_TYPES and between test functions to satisfy pre-commit linting. Remove # pragma: no cover from test inner functions that are exercised by the tests.
The inner functions func_optional_str and func_optional_list are only used for metadata extraction and pre_parse_json testing -- their bodies are never actually called, so coverage cannot track them.
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
pre_parse_json()usesfield_info.annotation is not strto decide whether to attemptjson.loads()on incoming string values. For union types likestr | None, this identity check passes becausestr | Noneis notstr, so string values get JSON-parsed when they should not be._should_pre_parse_json()helper that inspects union annotations: unions of only simple scalar types (str, int, float, bool, NoneType) skip pre-parsing, while complex unions likelist[str] | Nonestill get pre-parsed.Closes #1873
Test plan
test_str_union_pre_parse_preserves_strings-- verifies JSON object strings, JSON array strings, plain strings, and UUID strings are all preserved as-is when annotation isstr | Nonetest_complex_union_still_pre_parses-- verifieslist[str] | Nonestill deserializes a JSON-encoded list from a stringtest_str_union_uuid_end_to_end-- end-to-end validation that a UUID passed to astr | Noneparameter arrives intact throughcall_fn_with_arg_validation