Skip to content

fix(json-schema): decode RFC 6901 escapes when extracting $defs names - #6180

Merged
zzstoatzz merged 3 commits into
PrefectHQ:mainfrom
AP24110011695:fix/compress-schema-json-pointer-escapes
Nov 10, 2026
Merged

zzstoatzz merged 3 commits into
PrefectHQ:mainfrom
AP24110011695:fix/compress-schema-json-pointer-escapes

Conversation

@AP24110011695

Copy link
Copy Markdown
Contributor

Fixes #6034

Problem

compress_schema() decided which are referenced by reading the raw pointer text of each (ref.split("/")[-1] in _single_pass_optimize). A definition referenced through a properly encoded JSON Pointer therefore looked unused and was pruned, leaving dangling ``s in the returned schema:

  • RFC 6901 escapes: #/$defs/a~1b (definition a/b), #/$defs/c~0d (definition c~d)
  • percent-encoding: #/$defs/Alias%20One (definition Alias One)
  • sub-pointers into a definition: #/$defs/Container/properties/value — the raw last segment is properties, not Container

The same raw-text extraction is used in two OpenAPI paths, with the same effect: OpenAPIParser._extract_schema_dependencies (collecting component schemas by name) and the request-body pruner inside `_combine_schemas_and_map_params`. A component schema named `a/b`, referenced as `#/components/schemas/a~1b`, was never collected and its rewritten dangled.

Fix

Add ref_target_name() to fastmcp.utilities.json_schema. It returns the first pointer segment after a definition root (`` by default, components/schemas for raw OpenAPI documents), decoded the same way `_resolve_local_ref` decodes pointers: percent-decode the fragment, then unescape `~1`/`~0` per RFC 6901. Matching the resolver's own convention keeps the collectors consistent with what actually resolves — anything `_resolve_local_ref` can resolve is never treated as unused. A pointer may continue past the name, so a sub-pointer names the container definition.

The helper is used at all three extraction sites:

  • _single_pass_optimize (compress_schema `` pruning)
  • OpenAPIParser._extract_schema_dependencies
  • _combine_schemas_and_map_params request-body `` pruning

Raw `` values are untouched everywhere; only the used/unused decision changes.

Tests

  • TestRefTargetName: the decoder itself — escapes, ~01 not re-decoded, percent-encoding, sub-pointers, non-definition pointers, alternate roots
  • TestCompressSchemaEscapedRefNames: the issue's schema keeps all four referenced definitions; def→def escaped references keep both definitions; unused escaped definitions are still pruned
  • TestEscapedSchemaNameReferences (OpenAPI): schemas named a/b and Alias One are collected from the document and survive _combine_schemas_and_map_params with their references intact

The compress_schema regression fails on main for the reported reason (all four referenced definitions pruned, so result.get("$defs") is None) and passes with this change.

Verification

  • uv run pytest tests/utilities/ -n 0 — 1312 passed, 1 skipped
  • uv run pytest tests/tools tests/resources tests/prompts -n 0 — 893 passed, 1 skipped
  • uv run prek run --files <changed files> — ruff check/format, prettier, codespell, loq passed; ty check passes on all changed files when run directly (the prek runner hit a transient allocator OOM on ty, not a type error); gitleaks was skipped locally because the binary is not installed

compress_schema() collected referenced definition names from the raw
pointer text (ref.split("/")[-1]), so definitions referenced through
RFC 6901 escapes (#/$defs/a~1b), percent-encoding
(#/$defs/Alias%20One), or sub-pointers
(#/$defs/Container/properties/value) were treated as unused and pruned,
leaving dangling $refs in the returned schema. The same raw-text
extraction in the OpenAPI parser's schema dependency collector and in
the request-body $defs pruner missed component schemas whose names
contain those characters.

Add ref_target_name(), which returns the decoded first segment after a
definition root using _resolve_local_ref's convention (percent-decode
the fragment, then unescape ~1/~0), and use it at all three extraction
sites. A sub-pointer now names the container definition instead of its
last path segment.

Fixes PrefectHQ#6034

Co-authored-by: RS-ITACHI <276741997+RS-ITACHI@users.noreply.github.com>
@github-actions

This comment has been minimized.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T17:48:04.097287Z a4f2989 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@marvin-context-protocol marvin-context-protocol Bot added the bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. label Oct 30, 2026
@AP24110011695

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@AP24110011695

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@AP24110011695

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: a4f2989462

ℹ️ 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".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzstoatzz
zzstoatzz merged commit 70271c1 into PrefectHQ:main Nov 10, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. bypass-issue-check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compress_schema prunes used $defs when $ref uses JSON Pointer escapes (~0, ~1), percent-encoding, or sub-paths

2 participants