fix: handle property names with percent signs in JSON Pointers#2828
Open
chatman-media wants to merge 1 commit into
Open
fix: handle property names with percent signs in JSON Pointers#2828chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
A schema property name containing a literal percent sign (e.g. "25%") crashed codegen with "URIError: URI malformed" whenever the property resolved to an object, because the accumulated path was re-parsed through Redocly's parseRef -> decodeURIComponent. Wrap parseRef so a failed decodeURIComponent falls back to the raw, un-decoded pointer segment (mirroring escapePointer, which does not percent-encode). Valid percent-encoded segments still decode correctly. Closes openapi-ts#2251
🦋 Changeset detectedLatest commit: 63b2f98 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
👷 Deploy request for openapi-ts pending review.Visit the deploys page to approve it
|
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.
Changes
Fixes #2251.
A schema property name containing a literal percent sign (e.g.
"25%") crashes codegen withURIError: URI malformedwhenever that property resolves to an object:Root cause
createRefbuilds JSON Pointer paths by escaping each segment with Redocly'sescapePointer, which escapes~and/but does not percent-encode. When a nested object is transformed, the accumulated path (e.g.#/components/schemas/response/25%) is fed back intoparseRef, whoseunescapePointerrunsdecodeURIComponenton each segment.decodeURIComponent("25%")throwsURIError: URI malformed, crashing the whole run.The same
parseRefround-trip is used in several places (createRef,oapiRef, enum-name generation, discriminator property inference), so any of them could hit this with a%-containing key.Fix
Add a small resilient
parseRefwrapper (insrc/lib/ts.ts) that delegates to Redocly'sparseRef, but on failure falls back to parsing the pointer withoutdecodeURIComponent— and per segment, only decodes when it can, otherwise returns the raw segment. This mirrors the asymmetry ofescapePointer(which never percent-encodes), so:%segments (e.g.25%) pass through unchanged instead of throwing.%7Bid%7D->{id}) still decode correctly.All internal
parseRefcall sites now use this wrapper.How to Review
test/transform/schema-object/object.test.ts("property name with percent sign (nested object)"). It throwsURIError: URI malformedonmainand passes with this change.test/lib/ts.test.ts(describe("parseRef")): confirms normal parsing, percent-decoding, and no-throw on a bare%.pnpm testpasses for theopenapi-typescriptpackage.Checklist
docs/updated (if necessary) — n/apnpm run update:examplesrun — n/a (no example schema output changes).changeset/percent-sign-property-name.md)