Skip to content

Percent-encode "#" in URI fragments - #153

Open
MohitSAGAR11 wants to merge 1 commit into
hyperjump-io:mainfrom
MohitSAGAR11:fix-hash-in-fragments
Open

MohitSAGAR11 wants to merge 1 commit into
hyperjump-io:mainfrom
MohitSAGAR11:fix-hash-in-fragments

Conversation

@MohitSAGAR11

Copy link
Copy Markdown
Contributor

Fixes #147

encodeURI leaves # as is, so a # in a property name produced locations like #/a#b. Going the other way, anchorLocation used decodeURI, which doesn't decode %23, so $ref: "#/$defs/a%23b" couldn't be resolved.

Changes

  • encodeUriFragment in lib/common.js is the helper you suggested: encodeURI(fragment).replaceAll("#", "%23"). It's used everywhere a fragment is built:
    • computeUri in lib/instance.js (instance locations)
    • canonicalUri in lib/schema.js (keyword locations)
    • the $dynamicAnchor and legacy $dynamicAnchor URIs
    • the "No such anchor" error
  • anchorLocation decodes with decodeURIComponent, the same as Instance.get and uriFragment already do.

Tests

Added to lib/output-basic.spec.ts. All three fail without the fix:

  • a '#' in a property name is percent-encoded: instanceLocation is #/a%23b and absoluteKeywordLocation ends in /properties/a%23b/type.
  • a $ref to a percent-encoded '#': $ref: "#/$defs/a%23b" resolves.
  • a $dynamicAnchor under a '#' in a property name: $dynamicRef resolves to an anchor under $defs/a#b.

npm test, type-check and lint pass.

One note, unrelated to this PR: with the current JSON-Schema-Test-Suite main, main here fails one v1 case, "--" in the 3rd and 4th position is valid (ab--cd.example). It was added in json-schema-org/JSON-Schema-Test-Suite#1264 with compatibility 4,<=6,9999, but the v1 hostname handler uses isAsciiIdn. CI seems to still be getting the suite from before that PR, which is why it's green.

encodeURI leaves "#" as is, so a "#" in a property name produced locations like "#/a#b", and a $ref to "#/$defs/a%23b" couldn't be resolved because decodeURI doesn't decode "%23". Fragments are now built with one helper that also encodes "#", and anchorLocation decodes with decodeURIComponent like Instance.get and uriFragment already do.

Fixes hyperjump-io#147
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.

A "#" in a property name isn't encoded in locations and can't be used in $ref

1 participant