$ref under #/definitions/ and #/$defs/ is not URI/JSON-Pointer decoded; quotes in property names emit invalid TypeScript

Open
#1,900 0 comments 0 reactions 0 assignees View on GitHub

@bardrop is already working on this.

Since Nov 25, 2026.

  • #1906 by @bardrop — open

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript
Domain
tooling

Research direction

Reproduce with the repro.js script in the issue, then read extractDefinitionName in src/type-generator.ts — it slices the #/definitions/ prefix without decodeURIComponent or ~1/~0 unescaping, so that is the first fix. DEFAULT_RENDER_TYPE_NAME only splits on ., which is where the it's member name comes from; decide what valid identifier it should render to. The escaping of the emitted key and accessor lives in emitProperty and ToJsonFunction.emit in src/tojson.ts. Done when all four repro cases resolve and the generated TypeScript passes tsc/jsii; run the existing test suite to check the ref-name changes break nothing.

Written by the indexing model from the issue text.

Description

good first issue p2

TypeGenerator does not URI-decode or JSON-Pointer-unescape $ref fragments under #/definitions/ or #/$defs/ (RFC 6901 section 6), and emits invalid TypeScript when a property name contains ' (or \).

Save as repro.js and run npm install [email protected] && node repro.js:

const { TypeGenerator } = require("json2jsii");

const gen = (schema) => {
  try {
    return TypeGenerator.forStruct("Root", schema).render();
  } catch (e) {
    return e.message;
  }
};

const Y = { type: "object", properties: { y: { type: "string" } } };
console.log(gen({ type: "object", properties: { x: { $ref: "#/definitions/a~1b" } }, definitions: { "a/b": Y } }));
console.log(gen({ type: "object", properties: { x: { $ref: "#/definitions/a~0b" } }, definitions: { "a~b": Y } }));
console.log(gen({ type: "object", properties: { x: { $ref: "#/definitions/a%20b" } }, definitions: { "a b": Y } }));
console.log(
  gen({ type: "object", properties: { "it's": { type: "string" } } })
    .split("\n")
    .filter((l) => l.includes("it's"))
    .join("\n"),
);

Actual output:

unable to find a definition for the $ref "a~1b"
unable to find a definition for the $ref "a~0b"
unable to find a definition for the $ref "a%20b"
   * @schema Root#it's
  readonly it's?: string;
    'it's': obj.it's,

Expected: the first three resolve to the a/b, a~b, and a b definitions and emit valid TypeScript type names, and "it's" is emitted as a valid camelCase identifier on Root with an escaped string key in toJson_Root so the generated code compiles with tsc and jsii.

Versions: [email protected] and main (5c3e02b), Node.js v26.13.0.

In src/type-generator.ts, extractDefinitionName slices the prefix without decodeURIComponent or ~1/~0 unescaping (and DEFAULT_RENDER_TYPE_NAME only splits on .), while emitProperty and ToJsonFunction.emit (src/tojson.ts) leave ' and \ unescaped in the member name and '${k}' key.

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
24
Forks
15
Avg merge
8h 21m
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cdklabs/json2jsii

All issues in cdklabs/json2jsii

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.