$ref under #/definitions/ and #/$defs/ is not URI/JSON-Pointer decoded; quotes in property names emit invalid TypeScript
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
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cdklabs/json2jsii
-
good first issue p2
Difficulty 3/5 Half a day Newbie friendliness 68/100
-
good first issue p2
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
good first issue p2
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
good first issue p2
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
good first issue p2
Difficulty 3/5 1-2 days Newbie friendliness 45/100
All issues in cdklabs/json2jsii
Similar issues
-
triage
Difficulty 1/5 Under an hour Newbie friendliness 64/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
ranxianglei/billion-context#7860 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
agent-oc-find-bugs api complexity → simple payment-methods recurring contributions reviewed stripe
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
opencollective/opencollective#9177 ·