From 1c6652ae9afb3fa053f4104f783e59282004ffba Mon Sep 17 00:00:00 2001 From: Nolann Biron Date: Wed, 25 Nov 2026 10:40:50 +0100 Subject: [PATCH] Sanitize descriptions in legacy OpenAPI specs --- .../gitbook/src/lib/openapi/enrich.test.ts | 57 +++++++++++++++++++ packages/gitbook/src/lib/openapi/enrich.ts | 16 +++--- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/packages/gitbook/src/lib/openapi/enrich.test.ts b/packages/gitbook/src/lib/openapi/enrich.test.ts index 171f7563f..0d53a8135 100644 --- a/packages/gitbook/src/lib/openapi/enrich.test.ts +++ b/packages/gitbook/src/lib/openapi/enrich.test.ts @@ -35,4 +35,61 @@ describe('#enrichFilesystem', () => { expect(paths['/a'].get['x-gitbook-description-html']).toBe('

constructor

'); expect(paths['/b'].get['x-gitbook-description-html']).toBe('

constructor

'); }); + + async function enrichSchemas(schemas: Record) { + const { filesystem } = await parseOpenAPI({ + value: JSON.stringify({ + openapi: '3.0.0', + info: { title: 'Test', version: '1.0.0' }, + paths: {}, + components: { schemas }, + }), + rootURL: null, + }); + const enriched = await enrichFilesystem(filesystem); + return enriched[0]?.specification.components.schemas; + } + + it('drops description fields supplied by the specification', async () => { + const schemas = await enrichSchemas({ + Pet: { + type: 'object', + 'x-gitbook-description-html': 'spec', + 'x-gitbook-description-document': { nodes: [] }, + }, + }); + expect(schemas.Pet['x-gitbook-description-html']).toBeUndefined(); + expect(schemas.Pet['x-gitbook-description-document']).toBeUndefined(); + }); + + it('sanitizes descriptions of schemas named example', async () => { + const schemas = await enrichSchemas({ + example: { type: 'string', description: '' }, + }); + const html = schemas.example['x-gitbook-description-html']; + expect(html).toContain(' { + const schemas = await enrichSchemas({ + Pet: { + type: 'object', + example: { + description: '', + 'x-gitbook-description-html': '', + }, + }, + }); + const html = schemas.Pet.example['x-gitbook-description-html']; + expect(html).toBeDefined(); + expect(html).not.toContain('onerror'); + }); + + it('keeps example values without HTML as written', async () => { + const schemas = await enrichSchemas({ + Pet: { type: 'object', example: { description: 'A **good** dog' } }, + }); + expect(schemas.Pet.example).toEqual({ description: 'A **good** dog' }); + }); }); diff --git a/packages/gitbook/src/lib/openapi/enrich.ts b/packages/gitbook/src/lib/openapi/enrich.ts index 527230938..29a1a0520 100644 --- a/packages/gitbook/src/lib/openapi/enrich.ts +++ b/packages/gitbook/src/lib/openapi/enrich.ts @@ -8,19 +8,21 @@ import { parseMarkdown } from '../markdown'; export async function enrichFilesystem(filesystem: Filesystem) { const parseMarkdownWithCache = createMarkdownParser(); return traverse(filesystem, async (node, path) => { + // Examples included: a `$ref` can render an example value as a schema. + delete node['x-gitbook-description-html']; + delete node['x-gitbook-description-document']; + if ( path !== undefined && 'description' in node && typeof node.description === 'string' && node.description ) { - const key = path[path.length - 1]; - // Avoid parsing descriptions in examples. - if (key !== 'example') { - const description = node.description.trim(); - if (description) { - node['x-gitbook-description-html'] = await parseMarkdownWithCache(description); - } + const description = node.description.trim(); + // Examples stay as written, except where their raw description could render a tag. + const isExample = path[path.length - 1] === 'example'; + if (description && (!isExample || description.includes('<'))) { + node['x-gitbook-description-html'] = await parseMarkdownWithCache(description); } } return node;