Skip to content

Conversation

Copy link

Copilot AI commented Feb 11, 2026

New @typespec/http-api-docs emitter that generates markdown reference documentation for HTTP-based TypeSpec services, including synthetic JSON request/response examples.

Emitter (packages/http-api-docs)

  • Traverses HttpService/HttpOperation via @typespec/http, emits structured markdown with service metadata, operation signatures, parameter tables, request/response body examples, and property documentation
  • Auto-generates JSON examples from TypeSpec types: models, scalars, enums, arrays, unions, nested objects, with circular reference protection
  • Supports {service-name} interpolation in output filename

Type resolution and example generation

Handles all TypeSpec type kinds including literal types ("String", "Number", "Boolean"), which are distinct from their scalar counterparts:

model Config {
  kind: "standard";   // String literal → example: "standard"
  code: 200;          // Number literal → example: 200
  active: true;       // Boolean literal → example: true
}

Build integration

  • Added to tsconfig.ws.json project references for watch/incremental builds
  • Already autodiscovered by pnpm build:all (workspace glob) and pnpm test (vitest project glob)

Test coverage

19 tests covering service titles, HTTP verbs, path/query parameters, request/response bodies, nested models, arrays, enums, optional properties, date/boolean scalars, multi-operation APIs, @doc propagation, and string/numeric/boolean literal types.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 11, 2026 22:19
…documentation

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Copilot AI changed the title [WIP] Add typespec emitter for HTTP service documentation feat: add @typespec/http-api-docs markdown reference documentation emitter Feb 11, 2026
Copilot AI requested a review from johanste February 11, 2026 22:29
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Copilot AI changed the title feat: add @typespec/http-api-docs markdown reference documentation emitter Add HTTP API docs markdown emitter for TypeSpec services Feb 12, 2026
…ter type resolution and example generation

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Copilot AI changed the title Add HTTP API docs markdown emitter for TypeSpec services Add HTTP API docs markdown emitter with literal type support Feb 12, 2026
* - Escapes pipe characters so they don't break column boundaries
*/
function escapeForTable(value: string): string {
return value.replace(/\r?\n/g, "<br>").replace(/\|/g, "\\|");

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI about 15 hours ago

In general, to correctly escape strings for a markdown table cell, you must escape all characters that have special meaning in that context, including the backslash itself. If you escape | but not \, then any existing backslashes can change how following characters are interpreted (e.g. \| could become a literal | again), so the escaping is incomplete.

The best targeted fix is to extend escapeForTable so that it first escapes all backslashes by doubling them, then applies the existing transformations: converting newlines to <br> and escaping | as \|. The order matters: we must escape \ before introducing any new backslashes (like the ones used to escape |), so we don’t re‑escape them or alter the intended sequences. Concretely, in packages/http-api-docs/src/emitter.ts, update escapeForTable at line 401 to:

  1. First replace all \ with \\ using a global regex.
  2. Then replace newlines with <br>.
  3. Then escape | with \|.

No new imports or helpers are required; this is a simple change to the existing one‑liner.


Suggested changeset 1
packages/http-api-docs/src/emitter.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/http-api-docs/src/emitter.ts b/packages/http-api-docs/src/emitter.ts
--- a/packages/http-api-docs/src/emitter.ts
+++ b/packages/http-api-docs/src/emitter.ts
@@ -394,11 +394,12 @@
 }
 
 /** Escape content for use inside a markdown table cell.
+ *  - Escapes backslashes so they are rendered literally
  *  - Replaces newlines with `<br>` to keep the row on one line
  *  - Escapes pipe characters so they don't break column boundaries
  */
 function escapeForTable(value: string): string {
-  return value.replace(/\r?\n/g, "<br>").replace(/\|/g, "\\|");
+  return value.replace(/\\/g, "\\\\").replace(/\r?\n/g, "<br>").replace(/\|/g, "\\|");
 }
 
 /** Return an extra description for types that lose information when shown as
EOF
@@ -394,11 +394,12 @@
}

/** Escape content for use inside a markdown table cell.
* - Escapes backslashes so they are rendered literally
* - Replaces newlines with `<br>` to keep the row on one line
* - Escapes pipe characters so they don't break column boundaries
*/
function escapeForTable(value: string): string {
return value.replace(/\r?\n/g, "<br>").replace(/\|/g, "\\|");
return value.replace(/\\/g, "\\\\").replace(/\r?\n/g, "<br>").replace(/\|/g, "\\|");
}

/** Return an extra description for types that lose information when shown as
Copilot is powered by AI and may make mistakes. Always verify output.
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.

2 participants