Support references pointing to invalid files (#2930)

This commit is contained in:
Greg Bergé
2025-03-05 21:37:46 +01:00
committed by GitHub
parent 7e67ebc11d
commit 052e07a936
5 changed files with 21 additions and 27 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/openapi-parser': patch
---
Support references pointing to invalid files
@@ -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,
});
@@ -0,0 +1 @@
{ "invalid" <> }
@@ -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:
@@ -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;
}