Skip to content

Commit 052e07a

Browse files
authored
Support references pointing to invalid files (#2930)
1 parent 7e67ebc commit 052e07a

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

.changeset/ninety-otters-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/openapi-parser': patch
3+
---
4+
5+
Support references pointing to invalid files

packages/openapi-parser/src/filesystem.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ describe('#createFileSystem', () => {
1717
'/root/spec.yaml': await serveFixture('/remote-ref/root/spec.yaml'),
1818
'/root/user.yaml': await serveFixture('/remote-ref/root/user.yaml'),
1919
'/root/pet.yaml': await serveFixture('/remote-ref/root/pet.yaml'),
20+
'/root/invalid.yaml': await serveFixture('/remote-ref/root/invalid.txt'),
2021
'/tag.yaml': await serveFixture('/remote-ref/tag.yaml'),
2122
},
2223
fetch() {
23-
return new Response('404!');
24+
return new Response('<404>', {
25+
status: 404,
26+
});
2427
},
2528
port: 3020,
2629
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "invalid" <> }

packages/openapi-parser/src/fixtures/remote-ref/root/spec.yaml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -676,37 +676,16 @@ components:
676676
xml:
677677
name: address
678678
type: object
679-
Category:
680-
x-swagger-router-model: io.swagger.petstore.model.Category
681-
properties:
682-
id:
683-
type: integer
684-
format: int64
685-
example: 1
686-
name:
687-
type: string
688-
example: Dogs
689-
xml:
690-
name: category
691-
type: object
692679
User:
693680
$ref: 'user.yaml#/components/schemas/User'
694681
Tag:
695682
$ref: '../tag.yaml#/components/schemas/Tag'
696683
Pet:
697684
$ref: 'http://localhost:3020/root/pet.yaml#/components/schemas/Pet'
698685
ApiResponse:
699-
properties:
700-
code:
701-
type: integer
702-
format: int32
703-
type:
704-
type: string
705-
message:
706-
type: string
707-
xml:
708-
name: '##default'
709-
type: object
686+
$ref: 'http://localhost:3020/root/not-found.yaml'
687+
Category:
688+
$ref: 'http://localhost:3020/root/invalid.txt'
710689
requestBodies:
711690
Pet:
712691
content:

packages/openapi-parser/src/scalar-plugins/fetchURLs.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LoadPlugin } from '@scalar/openapi-parser';
1+
import { type LoadPlugin, normalize } from '@scalar/openapi-parser';
22

33
export const fetchUrlsDefaultConfiguration = {
44
limit: 40,
@@ -52,7 +52,13 @@ export const fetchURLs: (customConfiguration: {
5252
numberOfRequests++;
5353
const url = getReferenceUrl({ value, rootURL: configuration.rootURL });
5454
const response = await fetch(url);
55-
return await response.text();
55+
if (!response.ok) {
56+
return undefined;
57+
}
58+
const text = await response.text();
59+
// Try to normalize the text to be sure it's a valid JSON or YAML.
60+
await normalize(text);
61+
return text;
5662
} catch (_error: any) {
5763
return undefined;
5864
}

0 commit comments

Comments
 (0)