mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +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>> {
|
): 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user