Skip to content

fix(downgrader): replace a percent-encoded $ref with its target - #322

Merged
dinwwwh merged 23 commits into
mainfrom
claude/project-thread-h3q238
Nov 1, 2026
Merged

dinwwwh merged 23 commits into
mainfrom
claude/project-thread-h3q238

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Nov 1, 2026

Copy link
Copy Markdown
Member

Requested by dinwwwh · project thread

Summary

find() percent-decodes a $ref, but isInlined checks for /$defs/, /definitions/, the removed parts, and items/additionalProperties on the ref as written. So a $ref such as #/components/schemas/Pair/%69tems (items) was kept as a $ref, and then read the schema the conversion writes at that spot. Where that schema is looser and sits under a not, the 3.0 output rejects values the 3.1 input accepts.

The fix is one more condition: any $ref containing % is replaced by its target, which find already resolves correctly. That also covers percent-encoded #/webhooks/ and #/components/pathItems/ prefixes, so the README limitation about them is removed.

Tradeoffs

  • A percent-encoded $ref to an ordinary component, such as #/components/schemas/My%20Pet, is now inlined instead of kept. The output is larger and code generators lose that named type there, but percent-encoded refs are rare in generated documents (oRPC never writes them), and the inlined schema accepts exactly what the target does.

Test plan

  • New test: replaces a percent-encoded $ref with its target.
  • vitest run packages/downgrader (299 passed) and pnpm check pass locally.

Generated by Claude Code

claude added 23 commits November 1, 2026 05:44
… no oneOf branch apart, and takes null

Where several $refs enter the same cycle, the first copy made is reused, so
a later one can hold a $ref that was cut to {} while another target was
being inlined. isDisjoint and the nullable pair merge read such a $ref in
the input, saw its target's type or tag, and kept a oneOf that the {} then
let a value match twice. The holders of cut $refs are now recorded, and
follow() and throughRef() read them as keeping nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…operties beside other properties, out of enum merging

A oneOf rejects a value two of its branches share, so mergeEnums keeps a
oneOf whose literals repeat one. It compared them serialized, which tells
equal objects with their keys in another order apart, so it merged such a
oneOf into an enum that took the shared value. A oneOf of object or array
literals now stays as it is. And an additionalProperties in a literal branch
counted as repeating the one around it although that one sits beside other
properties, which it then let through, so it repeats only beside the same
properties.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…side annotations

enumNames read the names at the end of the propertyNames $ref chain, past a
$ref with an enum, maxLength or other keyword beside it that narrows the
names further. The record then got a property for each name of the last
enum, allowing keys the input rejects, and counted as converted exactly, so a
not or oneOf over it stayed. The chain now stops at such a $ref, so the
record loses its key names and counts as loose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…iction around an alias component or a cycle of objects

#309 converts the input again when a not or oneOf read a $ref's target
before that target lost a restriction around a cycle. It missed two cycles.
A component that is only a $ref, an alias, has no output of its own, so the
output recorded for it was undefined; it now records the output of the target
its chain of kept $refs ends at. And a dereferenced document closes a cycle
with the object itself rather than a $ref; convertObject now tells
convertSchema when it meets a schema again, and one that was not loose then
but is once converted makes the next pass read it as loose. First visits pay
nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…ed-union $refs

The rerun recorded nothing for an alias whose chain passes through a $ref the
conversion inlines, such as one into $defs, or that is a $ref to a tagged union
beside only annotations, so a not over it around a cycle was kept over a target
that lost a restriction. sharedOutput now follows every $ref convertSchema
treats as an alias, and reads an inlined hop's output from its inlined copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…form

A schema that loses a restriction around a cycle made the conversion run
again, so a not or oneOf that read it too early could drop or widen. Only
such a choice changes when more schemas read as loose, so a pass that kept
no not, and no oneOf without a loose branch, needs no other. A document with
a recursive schema that loses a restriction and no not or oneOf now converts
in one pass, about half the time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…y property as loose

OAS 3.0 applies a required readOnly property to responses only and a
required writeOnly property to requests only, while 3.1 requires it both
ways. The 3.0 output so loses that restriction, so a not or oneOf over
such an object must not keep it, and the property tells no oneOf branch
apart. The property is read through a chain of $refs too, as 3.0 reads
each target in its place.

Such a schema in a cycle, as a recursive DTO with a required readOnly id,
makes a document that keeps a not or oneOf convert twice, which the README
performance note now says.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
… or a set per name

CodSpeed read the order schema bench 19% slower with the check. It now loops
over `required` once per schema, reads each property's fields directly, and
makes a visited set only past the first `$ref`. Instructions per conversion of
that schema drop from 4% over main to 2% under callgrind with CodSpeed's V8
flags. A cycle of `$ref`s, which never reaches a schema, is tested too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
Marking the schema itself loose lets a $ref to it, and every object around
it, read as loose through the existing checks, so no walk over required
names is needed. Only the tag check still reads through $refs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…stead of converting again

A $ref or object cycle that reaches a schema before its conversion is done
cannot know yet what that schema loses, so it now counts as loose. This
replaces the second pass (#309, #316) and its skip (#317) with two checks,
at the cost of a recursive schema always counting as loose: a not over it is
removed, and a oneOf with it becomes anyOf unless its branches are disjoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
…ties with its target

Checking which items or additionalProperties the conversion replaces needed
a walk over the pointer and a lookup of each holder. Replacing every $ref
into either keeps the same values accepted, with one regex test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
A target that is a $ref again now holds no names, so the record loses its
key names, as it does for any other propertyNames it cannot read. This
replaces the walk along a chain of annotated $refs and its visited set,
which every schema allocated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
find() percent-decodes a $ref, but the checks for $defs, the removed parts,
and items or additionalProperties read it as written. So a $ref such as
#/components/schemas/Pair/%69tems was kept, and read the looser schema the
conversion puts there, which a not over it reads as stricter. Any $ref with
a % is now replaced by its target, which needs no decoding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
… target

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVaw7NmVyQoYtVBNPuE4uV
@dinwwwh dinwwwh self-assigned this Nov 1, 2026
@codspeed

codspeed Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 29 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing claude/project-thread-h3q238 (c607746) with main (6aeeb95)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@codecov

codecov Bot commented Nov 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one minor docs suggestion below.

Reviewed changes

  • Percent-encoded $refs are inlined. isInlined (packages/downgrader/src/v3.1-to-v3.0.ts:285) now returns true for any $ref containing % that resolvePointer resolves, so a ref whose /$defs/, /definitions/, removed-part, or items/additionalProperties segment is percent-encoded is replaced by its target instead of kept as written.
  • README limitation removed. The bullet stating removed-part $refs are recognized only by their literal #/webhooks/ / #/components/pathItems/ prefix is dropped.
  • New test. replaces a percent-encoded $ref with its target covers a percent-encoded pathItems ref, $defs, an items ref under a not, and an encoded property name.

ℹ️ Schema README no longer matches how $defs $refs are recognized

packages/downgrader/README.md:256 still tells readers that a $ref "into $defs is recognized by its /$defs/ segment", but this PR makes a percent-encoded ref such as #/components/schemas/Box/%24defs/Id resolve and inline too, so the statement is now incomplete in the same way the bullet this PR removed from the Paths section was. Worth updating alongside the change, together with a note that any percent-encoded $ref — including one to an ordinary component — is now inlined (larger output, the named type is lost at that site).

Technical details
# README `$defs`-recognition clause is now stale

## Affected sites
- `packages/downgrader/README.md:256` — "Every `$ref` is read as a JSON Pointer from the document root, and one into `$defs` is recognized by its `/$defs/` segment." With the new `%` condition, `isInlined` (`packages/downgrader/src/v3.1-to-v3.0.ts:285`) also recognizes a percent-encoded `$defs` segment (as it does `definitions`, a removed part, or `items`/`additionalProperties`).

## Required outcome
- The schema Limitations section should no longer imply a literal `/$defs/` segment is the only recognition path, and should reflect that a percent-encoded `$ref` is inlined — including one to an ordinary component, which is the tradeoff this PR makes.

## Context
- This PR already removed the parallel paths bullet: "`$ref`s into removed parts are recognized by their `#/webhooks/` or `#/components/pathItems/` prefix as written, not percent-encoded." The `$defs` clause at `README.md:256` is the schema-section analogue left behind.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Re-reviewed the delta since the prior pullfrog review: a docs-only follow-up to the inline-$ref change.

  • README limitation updated. The schema Limitations bullet now notes that a $ref which percent-encodes a character, such as #/%24defs/Pet, is replaced by its target, closing the gap flagged in the prior review. The text matches isInlined (packages/downgrader/src/v3.1-to-v3.0.ts:285), where any %-bearing $ref that resolvePointer resolves is inlined.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@dinwwwh
dinwwwh merged commit b441d6a into main Nov 1, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants