diff --git a/packages/oas/src/analyzer/index.ts b/packages/oas/src/analyzer/index.ts index e19b91f6..281bc063 100644 --- a/packages/oas/src/analyzer/index.ts +++ b/packages/oas/src/analyzer/index.ts @@ -30,6 +30,7 @@ export default async function analyzer(definition: OASDocument): Promise refizePointer(res.pointer)); } + +/** + * Determine if a given API definition previously had references by checking if we added the + * `x-readme-ref-name` extension after dereferencing. + */ +export function refNames(definition: OASDocument) { + return query(["$..['x-readme-ref-name']"], definition).map(res => refizePointer(res.pointer)); +} diff --git a/packages/oas/src/analyzer/types.ts b/packages/oas/src/analyzer/types.ts index 2cd6780a..7566f343 100644 --- a/packages/oas/src/analyzer/types.ts +++ b/packages/oas/src/analyzer/types.ts @@ -33,6 +33,12 @@ export interface OASAnalysis { */ raw_body?: OASAnalysisFeature; 'x-default': OASAnalysisFeature; + + /** + * x-readme-ref-name is added by our tooling after dereferencing so our UI can display the + * reference name. + */ + 'x-readme-ref-name': OASAnalysisFeature; 'x-readme.code-samples': OASAnalysisFeature; 'x-readme.explorer-enabled': OASAnalysisFeature; 'x-readme.headers': OASAnalysisFeature; diff --git a/packages/oas/test/analyzer/__snapshots__/index.test.ts.snap b/packages/oas/test/analyzer/__snapshots__/index.test.ts.snap index 126a8998..ed2ea4a1 100644 --- a/packages/oas/test/analyzer/__snapshots__/index.test.ts.snap +++ b/packages/oas/test/analyzer/__snapshots__/index.test.ts.snap @@ -88,6 +88,10 @@ exports[`analyzer > should should analyzer an OpenAPI definition 1`] = ` "locations": [], "present": false, }, + "x-readme-ref-name": { + "locations": [], + "present": false, + }, "x-readme.code-samples": { "locations": [], "present": false, diff --git a/packages/oas/test/analyzer/queries/readme.test.ts b/packages/oas/test/analyzer/queries/readme.test.ts index f4223a4b..72a644a9 100644 --- a/packages/oas/test/analyzer/queries/readme.test.ts +++ b/packages/oas/test/analyzer/queries/readme.test.ts @@ -3,6 +3,7 @@ import type { OASDocument } from '../../../src/types.js'; import { describe, beforeAll, expect, it } from 'vitest'; import * as QUERIES from '../../../src/analyzer/queries/readme.js'; +import Oas from '../../../src/index.js'; function loadSpec(r: any) { return r.default as unknown as OASDocument; @@ -189,4 +190,20 @@ describe('analyzer queries (ReadMe)', () => { expect(QUERIES.authDefaults(petstore)).toHaveLength(0); }); }); + + describe('`x-readme-ref-name` extension', () => { + it('should detect usage of `x-readme-ref-name` for defining reference names', () => { + const oas = Oas.init(petstore); + // Need to dereference it for this extension to be added + oas.dereference(); + expect(QUERIES.refNames(oas.api)).toStrictEqual([ + '#/components/schemas/ApiResponse/x-readme-ref-name', + '#/components/schemas/Category/x-readme-ref-name', + '#/components/schemas/Order/x-readme-ref-name', + '#/components/schemas/Pet/x-readme-ref-name', + '#/components/schemas/Tag/x-readme-ref-name', + '#/components/schemas/User/x-readme-ref-name', + ]); + }); + }); });