@gitbook/react-openapi 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "default": "./dist/index.js"
9
9
  }
10
10
  },
11
- "version": "1.1.8",
11
+ "version": "1.1.9",
12
12
  "sideEffects": false,
13
13
  "dependencies": {
14
14
  "@gitbook/openapi-parser": "workspace:*",
@@ -36,13 +36,15 @@ export function OpenAPIResponse(props: {
36
36
  />
37
37
  </OpenAPIDisclosure>
38
38
  ) : null}
39
- <div className="openapi-responsebody">
40
- <OpenAPISchemaProperties
41
- id={`response-${context.blockKey}`}
42
- properties={mediaType.schema ? [{ schema: mediaType.schema }] : []}
43
- context={context}
44
- />
45
- </div>
39
+ {mediaType.schema && (
40
+ <div className="openapi-responsebody">
41
+ <OpenAPISchemaProperties
42
+ id={`response-${context.blockKey}`}
43
+ properties={[{ schema: mediaType.schema }]}
44
+ context={context}
45
+ />
46
+ </div>
47
+ )}
46
48
  </div>
47
49
  );
48
50
  }
@@ -268,6 +268,7 @@ function getExamplesFromMediaTypeObject(args: {
268
268
  value: {
269
269
  [root]: generateSchemaExample(mediaTypeObject.schema, {
270
270
  xml: mediaType === 'application/xml',
271
+ mode: 'read',
271
272
  }),
272
273
  },
273
274
  },
@@ -277,7 +278,11 @@ function getExamplesFromMediaTypeObject(args: {
277
278
  return [
278
279
  {
279
280
  key: 'default',
280
- example: { value: generateSchemaExample(mediaTypeObject.schema) },
281
+ example: {
282
+ value: generateSchemaExample(mediaTypeObject.schema, {
283
+ mode: 'read',
284
+ }),
285
+ },
281
286
  },
282
287
  ];
283
288
  }
@@ -21,7 +21,42 @@ export function OpenAPIResponses(props: {
21
21
  icon={context.icons.chevronRight}
22
22
  groups={Object.entries(responses).map(
23
23
  ([statusCode, response]: [string, OpenAPIV3.ResponseObject]) => {
24
- const content = Object.entries(response.content ?? {});
24
+ const tabs = (() => {
25
+ // If there is no content, but there are headers, we need to show the headers
26
+ if (
27
+ (!response.content || !Object.keys(response.content).length) &&
28
+ response.headers &&
29
+ Object.keys(response.headers).length
30
+ ) {
31
+ return [
32
+ {
33
+ id: 'default',
34
+ body: (
35
+ <OpenAPIResponse
36
+ response={response}
37
+ mediaType={{}}
38
+ context={context}
39
+ />
40
+ ),
41
+ },
42
+ ];
43
+ }
44
+
45
+ return Object.entries(response.content ?? {}).map(
46
+ ([contentType, mediaType]) => ({
47
+ id: contentType,
48
+ label: contentType,
49
+ body: (
50
+ <OpenAPIResponse
51
+ response={response}
52
+ mediaType={mediaType}
53
+ context={context}
54
+ />
55
+ ),
56
+ })
57
+ );
58
+ })();
59
+
25
60
  const description = response.description;
26
61
 
27
62
  return {
@@ -39,17 +74,7 @@ export function OpenAPIResponses(props: {
39
74
  ) : null}
40
75
  </div>
41
76
  ),
42
- tabs: content.map(([contentType, mediaType]) => ({
43
- id: contentType,
44
- label: contentType,
45
- body: (
46
- <OpenAPIResponse
47
- response={response}
48
- mediaType={mediaType}
49
- context={context}
50
- />
51
- ),
52
- })),
77
+ tabs,
53
78
  };
54
79
  }
55
80
  )}
@@ -275,22 +275,20 @@ function OpenAPISchemaEnum(props: {
275
275
  }
276
276
 
277
277
  return (
278
- <div className="openapi-schema-enum">
279
- <span>Available options:</span>
280
- <div className="openapi-schema-enum-list">
281
- {enumValues.map((item, index) => (
282
- <span key={index} className="openapi-schema-enum-value">
283
- <OpenAPICopyButton
284
- value={item.value}
285
- label={item.description}
286
- withTooltip={!!item.description}
287
- >
288
- <code>{`${item.value}`}</code>
289
- </OpenAPICopyButton>
290
- </span>
291
- ))}
292
- </div>
293
- </div>
278
+ <span className="openapi-schema-enum">
279
+ Available options:{' '}
280
+ {enumValues.map((item, index) => (
281
+ <span key={index} className="openapi-schema-enum-value">
282
+ <OpenAPICopyButton
283
+ value={item.value}
284
+ label={item.description}
285
+ withTooltip={!!item.description}
286
+ >
287
+ <code>{`${item.value}`}</code>
288
+ </OpenAPICopyButton>
289
+ </span>
290
+ ))}
291
+ </span>
294
292
  );
295
293
  }
296
294
 
@@ -16,14 +16,10 @@ export function generateSchemaExample(
16
16
  schema: OpenAPIV3.SchemaObject,
17
17
  options?: GenerateSchemaExampleOptions
18
18
  ): JSONValue | undefined {
19
- return getExampleFromSchema(
20
- schema,
21
- {
22
- emptyString: 'text',
23
- ...options,
24
- },
25
- 3 // Max depth for circular references
26
- );
19
+ return getExampleFromSchema(schema, {
20
+ emptyString: 'text',
21
+ ...options,
22
+ });
27
23
  }
28
24
 
29
25
  /**
@@ -103,21 +99,6 @@ function guessFromFormat(schema: Record<string, any>, fallback = '') {
103
99
  return genericExampleValues[schema.format] ?? fallback;
104
100
  }
105
101
 
106
- /** Map of all the results */
107
- const resultCache = new WeakMap<Record<string, any>, any>();
108
-
109
- /** Store result in the cache, and return the result */
110
- function cache(schema: Record<string, any>, result: unknown) {
111
- // Avoid unnecessary WeakMap operations for primitive values
112
- if (typeof result !== 'object' || result === null) {
113
- return result;
114
- }
115
-
116
- resultCache.set(schema, result);
117
-
118
- return result;
119
- }
120
-
121
102
  /**
122
103
  * This function takes an OpenAPI schema and generates an example from it
123
104
  * Forked from : https://github.com/scalar/scalar/blob/main/packages/oas-utils/src/spec-getters/getExampleFromSchema.ts
@@ -152,8 +133,20 @@ const getExampleFromSchema = (
152
133
  },
153
134
  level = 0,
154
135
  parentSchema?: Record<string, any>,
155
- name?: string
136
+ name?: string,
137
+ resultCache = new WeakMap<Record<string, any>, any>()
156
138
  ): any => {
139
+ // Store result in the cache, and return the result
140
+ function cache(schema: Record<string, any>, result: unknown) {
141
+ // Avoid unnecessary WeakMap operations for primitive values
142
+ if (typeof result !== 'object' || result === null) {
143
+ return result;
144
+ }
145
+
146
+ resultCache.set(schema, result);
147
+ return result;
148
+ }
149
+
157
150
  // Check if the result is already cached
158
151
  if (resultCache.has(schema)) {
159
152
  return resultCache.get(schema);
@@ -245,7 +238,8 @@ const getExampleFromSchema = (
245
238
  options,
246
239
  level + 1,
247
240
  schema,
248
- propertyName
241
+ propertyName,
242
+ resultCache
249
243
  );
250
244
 
251
245
  if (typeof response[propertyXmlTagName ?? propertyName] === 'undefined') {
@@ -269,7 +263,8 @@ const getExampleFromSchema = (
269
263
  options,
270
264
  level + 1,
271
265
  schema,
272
- exampleKey
266
+ exampleKey,
267
+ resultCache
273
268
  );
274
269
  }
275
270
  }
@@ -290,21 +285,51 @@ const getExampleFromSchema = (
290
285
  response.ANY_ADDITIONAL_PROPERTY = getExampleFromSchema(
291
286
  schema.additionalProperties,
292
287
  options,
293
- level + 1
288
+ level + 1,
289
+ undefined,
290
+ undefined,
291
+ resultCache
294
292
  );
295
293
  }
296
294
  }
297
295
 
298
296
  if (schema.anyOf !== undefined) {
299
- Object.assign(response, getExampleFromSchema(schema.anyOf[0], options, level + 1));
297
+ Object.assign(
298
+ response,
299
+ getExampleFromSchema(
300
+ schema.anyOf[0],
301
+ options,
302
+ level + 1,
303
+ undefined,
304
+ undefined,
305
+ resultCache
306
+ )
307
+ );
300
308
  } else if (schema.oneOf !== undefined) {
301
- Object.assign(response, getExampleFromSchema(schema.oneOf[0], options, level + 1));
309
+ Object.assign(
310
+ response,
311
+ getExampleFromSchema(
312
+ schema.oneOf[0],
313
+ options,
314
+ level + 1,
315
+ undefined,
316
+ undefined,
317
+ resultCache
318
+ )
319
+ );
302
320
  } else if (schema.allOf !== undefined) {
303
321
  Object.assign(
304
322
  response,
305
323
  ...schema.allOf
306
324
  .map((item: Record<string, any>) =>
307
- getExampleFromSchema(item, options, level + 1, schema)
325
+ getExampleFromSchema(
326
+ item,
327
+ options,
328
+ level + 1,
329
+ schema,
330
+ undefined,
331
+ resultCache
332
+ )
308
333
  )
309
334
  .filter((item: any) => item !== undefined)
310
335
  );
@@ -335,7 +360,9 @@ const getExampleFromSchema = (
335
360
  { type: 'object', allOf: schema.items.allOf },
336
361
  options,
337
362
  level + 1,
338
- schema
363
+ schema,
364
+ undefined,
365
+ resultCache
339
366
  );
340
367
 
341
368
  return cache(
@@ -346,7 +373,14 @@ const getExampleFromSchema = (
346
373
  // For non-objects (like strings), collect all examples
347
374
  const examples = schema.items.allOf
348
375
  .map((item: Record<string, any>) =>
349
- getExampleFromSchema(item, options, level + 1, schema)
376
+ getExampleFromSchema(
377
+ item,
378
+ options,
379
+ level + 1,
380
+ schema,
381
+ undefined,
382
+ resultCache
383
+ )
350
384
  )
351
385
  .filter((item: any) => item !== undefined);
352
386
 
@@ -368,7 +402,14 @@ const getExampleFromSchema = (
368
402
  const schemas = schema.items[rule].slice(0, 1);
369
403
  const exampleFromRule = schemas
370
404
  .map((item: Record<string, any>) =>
371
- getExampleFromSchema(item, options, level + 1, schema)
405
+ getExampleFromSchema(
406
+ item,
407
+ options,
408
+ level + 1,
409
+ schema,
410
+ undefined,
411
+ resultCache
412
+ )
372
413
  )
373
414
  .filter((item: any) => item !== undefined);
374
415
 
@@ -380,7 +421,14 @@ const getExampleFromSchema = (
380
421
  }
381
422
 
382
423
  if (schema.items?.type) {
383
- const exampleFromSchema = getExampleFromSchema(schema.items, options, level + 1);
424
+ const exampleFromSchema = getExampleFromSchema(
425
+ schema.items,
426
+ options,
427
+ level + 1,
428
+ undefined,
429
+ undefined,
430
+ resultCache
431
+ );
384
432
 
385
433
  return wrapItems ? [{ [itemsXmlTagName]: exampleFromSchema }] : [exampleFromSchema];
386
434
  }
@@ -407,7 +455,14 @@ const getExampleFromSchema = (
407
455
  const firstOneOfItem = discriminateSchema[0];
408
456
 
409
457
  // Return an example for the first item
410
- return getExampleFromSchema(firstOneOfItem, options, level + 1);
458
+ return getExampleFromSchema(
459
+ firstOneOfItem,
460
+ options,
461
+ level + 1,
462
+ undefined,
463
+ undefined,
464
+ resultCache
465
+ );
411
466
  }
412
467
 
413
468
  // Check if schema has the `allOf` key
@@ -417,7 +472,14 @@ const getExampleFromSchema = (
417
472
  // Loop through all `allOf` schemas
418
473
  schema.allOf.forEach((allOfItem: Record<string, any>) => {
419
474
  // Return an example from the schema
420
- const newExample = getExampleFromSchema(allOfItem, options, level + 1);
475
+ const newExample = getExampleFromSchema(
476
+ allOfItem,
477
+ options,
478
+ level + 1,
479
+ undefined,
480
+ undefined,
481
+ resultCache
482
+ );
421
483
 
422
484
  // Merge or overwrite the example
423
485
  example =