mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 05:48:57 +00:00
Fallback to untrusted validation in case JSON OpenAPI spec is invalid (#2877)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@gitbook/openapi-parser': patch
|
||||
---
|
||||
|
||||
Fallback to untrusted validation in case JSON spec is invalid
|
||||
@@ -14,7 +14,7 @@ export async function parseOpenAPIV3(
|
||||
): Promise<Filesystem<OpenAPIV3xDocument>> {
|
||||
const { value, rootURL, trust } = input;
|
||||
const specification = trust
|
||||
? trustedValidate({ value, rootURL })
|
||||
? await trustedValidate({ value, rootURL })
|
||||
: await untrustedValidate({ value, 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's faster than `untrustedValidate`.
|
||||
*/
|
||||
function trustedValidate(input: ValidateOpenAPIV3Input) {
|
||||
async function trustedValidate(input: ValidateOpenAPIV3Input) {
|
||||
const { value, rootURL } = input;
|
||||
const result = (() => {
|
||||
if (typeof value === 'string') {
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
} catch (error) {
|
||||
throw new OpenAPIParseError('Invalid JSON', {
|
||||
code: 'invalid',
|
||||
rootURL,
|
||||
});
|
||||
/** In case of an invalid JSON, we fallback to untrusted validation. */
|
||||
return untrustedValidate(input);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
|
||||
Reference in New Issue
Block a user