File tree 2 files changed +23
-7
lines changed
packages/react-openapi/src
2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @gitbook/react-openapi ' : patch
3
+ ---
4
+
5
+ Safe parse OpenAPI JSON schema
Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ export function OpenAPISchemaPropertiesFromServer(props: {
122
122
return (
123
123
< OpenAPISchemaProperties
124
124
id = { props . id }
125
- properties = { JSON . parse ( props . properties , retrocycle ( ) ) }
125
+ properties = { safeJSONParse ( props . properties ) }
126
126
context = { props . context }
127
127
/>
128
128
) ;
@@ -172,12 +172,7 @@ export function OpenAPIRootSchemaFromServer(props: {
172
172
schema : string ;
173
173
context : OpenAPIClientContext ;
174
174
} ) {
175
- return (
176
- < OpenAPIRootSchema
177
- schema = { JSON . parse ( props . schema , retrocycle ( ) ) }
178
- context = { props . context }
179
- />
180
- ) ;
175
+ return < OpenAPIRootSchema schema = { safeJSONParse ( props . schema ) } context = { props . context } /> ;
181
176
}
182
177
183
178
/**
@@ -465,3 +460,19 @@ function getDisclosureLabel(schema: OpenAPIV3.SchemaObject): string {
465
460
466
461
return schema . title || 'child attributes' ;
467
462
}
463
+
464
+ /**
465
+ * Safely parse a JSON string using retrocycle.
466
+ * If parsing fails, it falls back to standard JSON.parse.
467
+ */
468
+ function safeJSONParse ( jsonString : string ) {
469
+ try {
470
+ return JSON . parse ( jsonString , retrocycle ( ) ) ;
471
+ } catch {
472
+ try {
473
+ return JSON . parse ( jsonString ) ;
474
+ } catch ( fallbackError ) {
475
+ throw new Error ( `Failed to parse JSON string: ${ fallbackError } ` ) ;
476
+ }
477
+ }
478
+ }
You can’t perform that action at this time.
0 commit comments