Skip to content

Support references pointing to invalid files #2930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-otters-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/openapi-parser': patch
---

Support references pointing to invalid files
5 changes: 4 additions & 1 deletion packages/openapi-parser/src/filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "invalid" <> }
27 changes: 3 additions & 24 deletions packages/openapi-parser/src/fixtures/remote-ref/root/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -676,37 +676,16 @@ 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:
$ref: '../tag.yaml#/components/schemas/Tag'
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:
Expand Down
10 changes: 8 additions & 2 deletions packages/openapi-parser/src/scalar-plugins/fetchURLs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LoadPlugin } from '@scalar/openapi-parser';
import { type LoadPlugin, normalize } from '@scalar/openapi-parser';

export const fetchUrlsDefaultConfiguration = {
limit: 40,
Expand Down Expand Up @@ -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;
}
Expand Down
Loading