Skip to content

Commit c808bb1

Browse files
authored
Fallback to untrusted validation in case JSON OpenAPI spec is invalid (#2877)
1 parent e24206e commit c808bb1

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

.changeset/angry-berries-fail.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+
Fallback to untrusted validation in case JSON spec is invalid

packages/openapi-parser/src/v3.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function parseOpenAPIV3(
1414
): Promise<Filesystem<OpenAPIV3xDocument>> {
1515
const { value, rootURL, trust } = input;
1616
const specification = trust
17-
? trustedValidate({ value, rootURL })
17+
? await trustedValidate({ value, rootURL })
1818
: await untrustedValidate({ value, rootURL });
1919

2020
const filesystem = await createFileSystem({ value: specification, rootURL });
@@ -54,17 +54,15 @@ async function untrustedValidate(input: ValidateOpenAPIV3Input) {
5454
* It assumes the specification is already a valid specification.
5555
* It's faster than `untrustedValidate`.
5656
*/
57-
function trustedValidate(input: ValidateOpenAPIV3Input) {
57+
async function trustedValidate(input: ValidateOpenAPIV3Input) {
5858
const { value, rootURL } = input;
5959
const result = (() => {
6060
if (typeof value === 'string') {
6161
try {
6262
return JSON.parse(value);
6363
} catch (error) {
64-
throw new OpenAPIParseError('Invalid JSON', {
65-
code: 'invalid',
66-
rootURL,
67-
});
64+
/** In case of an invalid JSON, we fallback to untrusted validation. */
65+
return untrustedValidate(input);
6866
}
6967
}
7068
return value;

0 commit comments

Comments
 (0)