Fallback to untrusted validation in case JSON OpenAPI spec is invalid (#2877)

This commit is contained in:
Nolann B.
2025-02-25 17:27:31 +01:00
committed by GitHub
parent e24206ebf0
commit c808bb1483
2 changed files with 9 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/openapi-parser': patch
---
Fallback to untrusted validation in case JSON spec is invalid
+4 -6
View File
@@ -14,7 +14,7 @@ export async function parseOpenAPIV3(
): Promise<Filesystem<OpenAPIV3xDocument>> { ): Promise<Filesystem<OpenAPIV3xDocument>> {
const { value, rootURL, trust } = input; const { value, rootURL, trust } = input;
const specification = trust const specification = trust
? trustedValidate({ value, rootURL }) ? await trustedValidate({ value, rootURL })
: await untrustedValidate({ value, rootURL }); : await untrustedValidate({ value, rootURL });
const filesystem = await createFileSystem({ value: specification, rootURL }); const filesystem = await createFileSystem({ value: specification, rootURL });
@@ -54,17 +54,15 @@ async function untrustedValidate(input: ValidateOpenAPIV3Input) {
* It assumes the specification is already a valid specification. * It assumes the specification is already a valid specification.
* It's faster than `untrustedValidate`. * It's faster than `untrustedValidate`.
*/ */
function trustedValidate(input: ValidateOpenAPIV3Input) { async function trustedValidate(input: ValidateOpenAPIV3Input) {
const { value, rootURL } = input; const { value, rootURL } = input;
const result = (() => { const result = (() => {
if (typeof value === 'string') { if (typeof value === 'string') {
try { try {
return JSON.parse(value); return JSON.parse(value);
} catch (error) { } catch (error) {
throw new OpenAPIParseError('Invalid JSON', { /** In case of an invalid JSON, we fallback to untrusted validation. */
code: 'invalid', return untrustedValidate(input);
rootURL,
});
} }
} }
return value; return value;