From 052e07a936af8fd835eb55d8e379797a19f5487c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Wed, 5 Mar 2025 21:37:46 +0100 Subject: [PATCH] Support references pointing to invalid files (#2930) --- .changeset/ninety-otters-hug.md | 5 ++++ .../openapi-parser/src/filesystem.test.ts | 5 +++- .../src/fixtures/remote-ref/root/invalid.txt | 1 + .../src/fixtures/remote-ref/root/spec.yaml | 27 +++---------------- .../src/scalar-plugins/fetchURLs.ts | 10 +++++-- 5 files changed, 21 insertions(+), 27 deletions(-) create mode 100644 .changeset/ninety-otters-hug.md create mode 100644 packages/openapi-parser/src/fixtures/remote-ref/root/invalid.txt diff --git a/.changeset/ninety-otters-hug.md b/.changeset/ninety-otters-hug.md new file mode 100644 index 000000000..aa07c882d --- /dev/null +++ b/.changeset/ninety-otters-hug.md @@ -0,0 +1,5 @@ +--- +'@gitbook/openapi-parser': patch +--- + +Support references pointing to invalid files diff --git a/packages/openapi-parser/src/filesystem.test.ts b/packages/openapi-parser/src/filesystem.test.ts index f235872a0..46d4131e5 100644 --- a/packages/openapi-parser/src/filesystem.test.ts +++ b/packages/openapi-parser/src/filesystem.test.ts @@ -17,10 +17,13 @@ describe('#createFileSystem', () => { '/root/spec.yaml': await serveFixture('/remote-ref/root/spec.yaml'), '/root/user.yaml': await serveFixture('/remote-ref/root/user.yaml'), '/root/pet.yaml': await serveFixture('/remote-ref/root/pet.yaml'), + '/root/invalid.yaml': await serveFixture('/remote-ref/root/invalid.txt'), '/tag.yaml': await serveFixture('/remote-ref/tag.yaml'), }, fetch() { - return new Response('404!'); + return new Response('<404>', { + status: 404, + }); }, port: 3020, }); diff --git a/packages/openapi-parser/src/fixtures/remote-ref/root/invalid.txt b/packages/openapi-parser/src/fixtures/remote-ref/root/invalid.txt new file mode 100644 index 000000000..54eea0d5c --- /dev/null +++ b/packages/openapi-parser/src/fixtures/remote-ref/root/invalid.txt @@ -0,0 +1 @@ +{ "invalid" <> } \ No newline at end of file diff --git a/packages/openapi-parser/src/fixtures/remote-ref/root/spec.yaml b/packages/openapi-parser/src/fixtures/remote-ref/root/spec.yaml index a5cc9eabd..85304a755 100644 --- a/packages/openapi-parser/src/fixtures/remote-ref/root/spec.yaml +++ b/packages/openapi-parser/src/fixtures/remote-ref/root/spec.yaml @@ -676,19 +676,6 @@ components: xml: name: address type: object - Category: - x-swagger-router-model: io.swagger.petstore.model.Category - properties: - id: - type: integer - format: int64 - example: 1 - name: - type: string - example: Dogs - xml: - name: category - type: object User: $ref: 'user.yaml#/components/schemas/User' Tag: @@ -696,17 +683,9 @@ components: Pet: $ref: 'http://localhost:3020/root/pet.yaml#/components/schemas/Pet' ApiResponse: - properties: - code: - type: integer - format: int32 - type: - type: string - message: - type: string - xml: - name: '##default' - type: object + $ref: 'http://localhost:3020/root/not-found.yaml' + Category: + $ref: 'http://localhost:3020/root/invalid.txt' requestBodies: Pet: content: diff --git a/packages/openapi-parser/src/scalar-plugins/fetchURLs.ts b/packages/openapi-parser/src/scalar-plugins/fetchURLs.ts index f0d54e18a..e9db5c600 100644 --- a/packages/openapi-parser/src/scalar-plugins/fetchURLs.ts +++ b/packages/openapi-parser/src/scalar-plugins/fetchURLs.ts @@ -1,4 +1,4 @@ -import type { LoadPlugin } from '@scalar/openapi-parser'; +import { type LoadPlugin, normalize } from '@scalar/openapi-parser'; export const fetchUrlsDefaultConfiguration = { limit: 40, @@ -52,7 +52,13 @@ export const fetchURLs: (customConfiguration: { numberOfRequests++; const url = getReferenceUrl({ value, rootURL: configuration.rootURL }); const response = await fetch(url); - return await response.text(); + if (!response.ok) { + return undefined; + } + const text = await response.text(); + // Try to normalize the text to be sure it's a valid JSON or YAML. + await normalize(text); + return text; } catch (_error: any) { return undefined; }