fix(pydantic): decode escaped local JSON schema references - #3602
fix(pydantic): decode escaped local JSON schema references#3602weivwang wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 97c05c2015
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| raise ValueError(f"Unexpected $ref format {ref!r}; Does not start with #/") | ||
|
|
||
| path = ref[2:].split("/") | ||
| path = [unquote(key).replace("~1", "/").replace("~0", "~") for key in ref[2:].split("/")] |
There was a problem hiding this comment.
Decode the fragment before splitting pointer tokens
When a valid URI fragment percent-encodes a JSON Pointer separator, such as #/$defs%2FGroup%2FItem, percent-decoding produces the pointer /$defs/Group/Item. Splitting the raw fragment first instead treats $defs/Group/Item as one dictionary key and raises KeyError rather than traversing the nested schema. Decode the fragment before tokenization; literal slashes in a reference token remain represented by JSON Pointer's ~1 escape.
Useful? React with 👍 / 👎.
Changes being requested
Decode URI fragments and RFC 6901 escape sequences while resolving local JSON Schema references in the handwritten Pydantic helper.
resolve_ref()currently splits a local$refand uses each raw segment as a dictionary key. Definitions containing/,~, spaces, or other escaped characters therefore fail withKeyError, for example:{"$ref":"#/$defs/path~1to%20model~0v1"}The change decodes each segment after splitting it, then applies JSON Pointer's
~1→/and~0→~substitutions. Decoding after the split is important: a percent-encoded/belongs to the definition key and must not become an extra path segment.Two regressions cover both direct resolution and the real strict-schema inlining path with sibling properties.
Additional context & links
No matching issue or open PR was found for escaped local
$refhandling. The modified source is undersrc/openai/lib/, whichCONTRIBUTING.mdidentifies as outside the generated SDK surface.Validation:
pytest -q tests/lib/test_pydantic.py— 5 passedruff check src/openai/lib/_pydantic.py tests/lib/test_pydantic.pyruff format --check src/openai/lib/_pydantic.py tests/lib/test_pydantic.pygit diff --check