Skip to content

fix(server): escape descriptions embedded in generated TypeScript comments - #73

Open
encodedz wants to merge 2 commits into
masterfrom
fix/odedgo/aggregator-comment-escaping
Open

encodedz wants to merge 2 commits into
masterfrom
fix/odedgo/aggregator-comment-escaping

Conversation

@encodedz

@encodedz encodedz commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #72

Problem

Descriptions were inserted into TypeScript comments without escaping, so /api/definitions and client.getTypeDefinitions() could return invalid TypeScript:

  • a line break in an input property description escaped the // comment (TS1005)
  • */, e.g. a glob like src/**/*.ts, closed the /** */ and /* */ comments early (TS1131 / TS1161)
  • the existing .replace(/\n/g, ' ') missed \r, U+2028 and U+2029, which TypeScript also treats as line terminators. The issue didn't mention these.

Fix

One sanitizer, now in atp-runtime (packages/runtime/src/metadata/comment-safe.ts) so the runtime and the server share it:

  • toCommentSafeLines(text) splits on every line terminator (\r\n, \r, \n, U+2028, U+2029) and escapes */ as *\/ in each line.
  • toCommentSafe(text) is the same thing joined into one line, for single-line comments.

Where it's applied:

  • Server (APIAggregator), where the reported bug was: generateFunctionTypes, generateAPINamespace, generateCompactInputType, generateCompactOutputType. Escaping runs after the existing truncation.
  • Runtime (generateRuntimeTypes): the API description (keeps its multi-line JSDoc), the method description and @param descriptions. The built-in metadata is clean today, so this part is defensive.

Tests

All regression tests are in __tests__/unit, so they run under yarn test:unit in CI. The vitest suites in packages/server/__tests__ and e2e/server don't run in CI today.

Layer File What it checks
Unit __tests__/unit/comment-safe.test.ts Both helpers: each line terminator, repeated */, idempotence, blank lines kept, the two helpers agree
Generator (server) __tests__/unit/aggregator-comment-escaping.test.ts Runs generateTypeScript() and the compact signature through the TypeScript parser (zero syntax errors), including the exact repro from the issue; description text is kept (escaped). Adds a fast-check property test (300 runs) with arbitrary descriptions weighted toward characters that break comments
Generator (runtime) __tests__/unit/runtime-types-comment-escaping.test.ts Made-up metadata with nasty API/method/param descriptions parses; multi-line API descriptions keep their lines. A guard test checks that the output from the real GENERATED_METADATA parses
E2E __tests__/e2e/server/definitions-comment-escaping.test.ts A real server: GET /api/definitions and client.getTypeDefinitions() return TypeScript that parses

fast-check (already in the lockfile through atp-compiler) is now also a root devDependency.

Mutation checks (put the old code back, confirm the tests catch it):

  • Server sanitizer replaced with an identity function: 22/27 server unit tests and 2/2 e2e tests fail. fast-check shrinks the failure to ["", "*/", ""].
  • Original runtime code put back at the 3 call sites: 9/11 runtime tests fail. The two that pass are expected: the plain-text baseline, and the guard on today's metadata, which is clean.

Local results: yarn test:unit passes (268 jest + 351 atp-compiler). packages/server vitest: 111 passed. E2E server/definitions-comment-escaping, api and explorer: 30 passed. tsc --build is clean.

API change

atp-runtime now exports toCommentSafe and toCommentSafeLines. This only adds exports; nothing existing changes.

🤖 Generated with Claude Code

encodedz and others added 2 commits November 23, 2026 13:20
…ments

APIAggregator interpolated user-supplied descriptions into TypeScript comments
without sanitizing them, so /api/definitions and client.getTypeDefinitions()
could return invalid TypeScript:

- a line break in an input property description escaped the `//` comment
- `*/` (e.g. a glob like `src/**/*.ts`) closed `/** */` and `/* */` comments early
- the existing `.replace(/\n/g, ' ')` missed `\r`, U+2028 and U+2029, which
  TypeScript also treats as line terminators

Add a `toCommentSafe` helper that collapses all line terminators to a space and
escapes `*/` as `*\/`, and use it at every site that embeds a description.

Tests:
- unit: toCommentSafe behavior
- generator: TS-parser validation of generateTypeScript() and the compact
  signature, plus a fast-check property test over arbitrary descriptions
- e2e: GET /api/definitions and client.getTypeDefinitions() parse cleanly

Fixes #72

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
generateRuntimeTypes() embedded runtime API, method and @PARAM descriptions
in JSDoc without escaping `*/` or handling all line terminators -- the same
class of bug as #72. Built-in metadata is clean today, so this is defensive.

- Move the comment sanitizer into atp-runtime (metadata/comment-safe.ts) so
  runtime and server share one implementation; atp-server now imports
  toCommentSafe from @mondaydotcomorg/atp-runtime.
- Add toCommentSafeLines() for multi-line JSDoc: splits on CRLF/CR/LF/
  U+2028/U+2029 and escapes `*/` per line, preserving line structure.
- Apply at the API description, method description and @PARAM sites.
- Tests: runtime-types-comment-escaping.test.ts (synthetic metadata +
  guard that the real GENERATED_METADATA output parses), plus
  toCommentSafeLines unit tests.

Refs #72

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@yarinmonday yarinmonday 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.

Lovely 🇲🇩

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.

APIAggregator.generateTypeScript: multi-line property descriptions and */ produce invalid TypeScript

2 participants