mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 10:03:31 +00:00
Safe parse OpenAPI JSON schema (#3046)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@gitbook/react-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Safe parse OpenAPI JSON schema
|
||||||
@@ -122,7 +122,7 @@ export function OpenAPISchemaPropertiesFromServer(props: {
|
|||||||
return (
|
return (
|
||||||
<OpenAPISchemaProperties
|
<OpenAPISchemaProperties
|
||||||
id={props.id}
|
id={props.id}
|
||||||
properties={JSON.parse(props.properties, retrocycle())}
|
properties={safeJSONParse(props.properties)}
|
||||||
context={props.context}
|
context={props.context}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@@ -172,12 +172,7 @@ export function OpenAPIRootSchemaFromServer(props: {
|
|||||||
schema: string;
|
schema: string;
|
||||||
context: OpenAPIClientContext;
|
context: OpenAPIClientContext;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return <OpenAPIRootSchema schema={safeJSONParse(props.schema)} context={props.context} />;
|
||||||
<OpenAPIRootSchema
|
|
||||||
schema={JSON.parse(props.schema, retrocycle())}
|
|
||||||
context={props.context}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -465,3 +460,19 @@ function getDisclosureLabel(schema: OpenAPIV3.SchemaObject): string {
|
|||||||
|
|
||||||
return schema.title || 'child attributes';
|
return schema.title || 'child attributes';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely parse a JSON string using retrocycle.
|
||||||
|
* If parsing fails, it falls back to standard JSON.parse.
|
||||||
|
*/
|
||||||
|
function safeJSONParse(jsonString: string) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(jsonString, retrocycle());
|
||||||
|
} catch {
|
||||||
|
try {
|
||||||
|
return JSON.parse(jsonString);
|
||||||
|
} catch (fallbackError) {
|
||||||
|
throw new Error(`Failed to parse JSON string: ${fallbackError}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user