@gitbook/react-openapi 1.1.7 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @gitbook/react-openapi
2
2
 
3
+ ## 1.1.9
4
+
5
+ ### Patch Changes
6
+
7
+ - da7b369: Fix missing headers in OpenAPIResponses
8
+ - da485f5: Fix read-only in generateSchemaExample
9
+ - 139a805: Fix OpenAPI enum display
10
+
11
+ ## 1.1.8
12
+
13
+ ### Patch Changes
14
+
15
+ - 7d0b422: Handle grouped OpenAPISchemas
16
+
3
17
  ## 1.1.7
4
18
 
5
19
  ### Patch Changes
@@ -34,8 +34,8 @@ export function OpenAPIResponse(props) {
34
34
  return parameterToProperty(__assign({ name: name }, header));
35
35
  })} context={context}/>
36
36
  </OpenAPIDisclosure>) : null}
37
- <div className="openapi-responsebody">
38
- <OpenAPISchemaProperties id={"response-".concat(context.blockKey)} properties={mediaType.schema ? [{ schema: mediaType.schema }] : []} context={context}/>
39
- </div>
37
+ {mediaType.schema && (<div className="openapi-responsebody">
38
+ <OpenAPISchemaProperties id={"response-".concat(context.blockKey)} properties={[{ schema: mediaType.schema }]} context={context}/>
39
+ </div>)}
40
40
  </div>);
41
41
  }
@@ -184,6 +184,7 @@ function getExamplesFromMediaTypeObject(args) {
184
184
  value: (_a = {},
185
185
  _a[root] = generateSchemaExample(mediaTypeObject.schema, {
186
186
  xml: mediaType === 'application/xml',
187
+ mode: 'read',
187
188
  }),
188
189
  _a),
189
190
  },
@@ -193,7 +194,11 @@ function getExamplesFromMediaTypeObject(args) {
193
194
  return [
194
195
  {
195
196
  key: 'default',
196
- example: { value: generateSchemaExample(mediaTypeObject.schema) },
197
+ example: {
198
+ value: generateSchemaExample(mediaTypeObject.schema, {
199
+ mode: 'read',
200
+ }),
201
+ },
197
202
  },
198
203
  ];
199
204
  }
@@ -9,9 +9,29 @@ export function OpenAPIResponses(props) {
9
9
  var responses = props.responses, context = props.context;
10
10
  return (<StaticSection header="Responses" className="openapi-responses">
11
11
  <OpenAPIDisclosureGroup allowsMultipleExpanded icon={context.icons.chevronRight} groups={Object.entries(responses).map(function (_a) {
12
- var _b;
13
12
  var statusCode = _a[0], response = _a[1];
14
- var content = Object.entries((_b = response.content) !== null && _b !== void 0 ? _b : {});
13
+ var tabs = (function () {
14
+ var _a;
15
+ // If there is no content, but there are headers, we need to show the headers
16
+ if ((!response.content || !Object.keys(response.content).length) &&
17
+ response.headers &&
18
+ Object.keys(response.headers).length) {
19
+ return [
20
+ {
21
+ id: 'default',
22
+ body: (<OpenAPIResponse response={response} mediaType={{}} context={context}/>),
23
+ },
24
+ ];
25
+ }
26
+ return Object.entries((_a = response.content) !== null && _a !== void 0 ? _a : {}).map(function (_a) {
27
+ var contentType = _a[0], mediaType = _a[1];
28
+ return ({
29
+ id: contentType,
30
+ label: contentType,
31
+ body: (<OpenAPIResponse response={response} mediaType={mediaType} context={context}/>),
32
+ });
33
+ });
34
+ })();
15
35
  var description = response.description;
16
36
  return {
17
37
  id: statusCode,
@@ -21,14 +41,7 @@ export function OpenAPIResponses(props) {
21
41
  </span>
22
42
  {description ? (<Markdown source={description} className="openapi-response-description"/>) : null}
23
43
  </div>),
24
- tabs: content.map(function (_a) {
25
- var contentType = _a[0], mediaType = _a[1];
26
- return ({
27
- id: contentType,
28
- label: contentType,
29
- body: (<OpenAPIResponse response={response} mediaType={mediaType} context={context}/>),
30
- });
31
- }),
44
+ tabs: tabs,
32
45
  };
33
46
  })}/>
34
47
  </StaticSection>);
@@ -132,16 +132,14 @@ function OpenAPISchemaEnum(props) {
132
132
  if (!(enumValues === null || enumValues === void 0 ? void 0 : enumValues.length)) {
133
133
  return null;
134
134
  }
135
- return (<div className="openapi-schema-enum">
136
- <span>Available options:</span>
137
- <div className="openapi-schema-enum-list">
138
- {enumValues.map(function (item, index) { return (<span key={index} className="openapi-schema-enum-value">
139
- <OpenAPICopyButton value={item.value} label={item.description} withTooltip={!!item.description}>
140
- <code>{"".concat(item.value)}</code>
141
- </OpenAPICopyButton>
142
- </span>); })}
143
- </div>
144
- </div>);
135
+ return (<span className="openapi-schema-enum">
136
+ Available options:{' '}
137
+ {enumValues.map(function (item, index) { return (<span key={index} className="openapi-schema-enum-value">
138
+ <OpenAPICopyButton value={item.value} label={item.description} withTooltip={!!item.description}>
139
+ <code>{"".concat(item.value)}</code>
140
+ </OpenAPICopyButton>
141
+ </span>); })}
142
+ </span>);
145
143
  }
146
144
  /**
147
145
  * Render the top row of a schema. e.g: name, type, and required status.
@@ -41,5 +41,5 @@ declare const getExampleFromSchema: (schema: Record<string, any>, options?: {
41
41
  * @default false
42
42
  */
43
43
  omitEmptyAndOptionalProperties?: boolean;
44
- }, level?: number, parentSchema?: Record<string, any>, name?: string) => any;
44
+ }, level?: number, parentSchema?: Record<string, any>, name?: string, resultCache?: WeakMap<Record<string, any>, any>) => any;
45
45
  export {};
@@ -24,8 +24,7 @@ import { checkIsReference } from './utils';
24
24
  * Generate a JSON example from a schema
25
25
  */
26
26
  export function generateSchemaExample(schema, options) {
27
- return getExampleFromSchema(schema, __assign({ emptyString: 'text' }, options), 3 // Max depth for circular references
28
- );
27
+ return getExampleFromSchema(schema, __assign({ emptyString: 'text' }, options));
29
28
  }
30
29
  /**
31
30
  * Generate an example for a media type.
@@ -96,25 +95,24 @@ function guessFromFormat(schema, fallback) {
96
95
  if (fallback === void 0) { fallback = ''; }
97
96
  return (_a = genericExampleValues[schema.format]) !== null && _a !== void 0 ? _a : fallback;
98
97
  }
99
- /** Map of all the results */
100
- var resultCache = new WeakMap();
101
- /** Store result in the cache, and return the result */
102
- function cache(schema, result) {
103
- // Avoid unnecessary WeakMap operations for primitive values
104
- if (typeof result !== 'object' || result === null) {
105
- return result;
106
- }
107
- resultCache.set(schema, result);
108
- return result;
109
- }
110
98
  /**
111
99
  * This function takes an OpenAPI schema and generates an example from it
112
100
  * Forked from : https://github.com/scalar/scalar/blob/main/packages/oas-utils/src/spec-getters/getExampleFromSchema.ts
113
101
  */
114
- var getExampleFromSchema = function (schema, options, level, parentSchema, name) {
102
+ var getExampleFromSchema = function (schema, options, level, parentSchema, name, resultCache) {
115
103
  var _a, _b, _c, _d;
116
104
  var _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
117
105
  if (level === void 0) { level = 0; }
106
+ if (resultCache === void 0) { resultCache = new WeakMap(); }
107
+ // Store result in the cache, and return the result
108
+ function cache(schema, result) {
109
+ // Avoid unnecessary WeakMap operations for primitive values
110
+ if (typeof result !== 'object' || result === null) {
111
+ return result;
112
+ }
113
+ resultCache.set(schema, result);
114
+ return result;
115
+ }
118
116
  // Check if the result is already cached
119
117
  if (resultCache.has(schema)) {
120
118
  return resultCache.get(schema);
@@ -184,7 +182,7 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
184
182
  if (Object.prototype.hasOwnProperty.call(schema.properties, propertyName)) {
185
183
  var property = schema.properties[propertyName];
186
184
  var propertyXmlTagName = (options === null || options === void 0 ? void 0 : options.xml) ? (_o = property.xml) === null || _o === void 0 ? void 0 : _o.name : undefined;
187
- response[propertyXmlTagName !== null && propertyXmlTagName !== void 0 ? propertyXmlTagName : propertyName] = getExampleFromSchema(property, options, level + 1, schema, propertyName);
185
+ response[propertyXmlTagName !== null && propertyXmlTagName !== void 0 ? propertyXmlTagName : propertyName] = getExampleFromSchema(property, options, level + 1, schema, propertyName, resultCache);
188
186
  if (typeof response[propertyXmlTagName !== null && propertyXmlTagName !== void 0 ? propertyXmlTagName : propertyName] === 'undefined') {
189
187
  delete response[propertyXmlTagName !== null && propertyXmlTagName !== void 0 ? propertyXmlTagName : propertyName];
190
188
  }
@@ -198,7 +196,7 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
198
196
  var property = schema.patternProperties[pattern];
199
197
  // Use the regex pattern as an example key
200
198
  var exampleKey = pattern;
201
- response[exampleKey] = getExampleFromSchema(property, options, level + 1, schema, exampleKey);
199
+ response[exampleKey] = getExampleFromSchema(property, options, level + 1, schema, exampleKey, resultCache);
202
200
  }
203
201
  }
204
202
  }
@@ -214,19 +212,19 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
214
212
  response.ANY_ADDITIONAL_PROPERTY = 'anything';
215
213
  }
216
214
  else if (schema.additionalProperties !== false) {
217
- response.ANY_ADDITIONAL_PROPERTY = getExampleFromSchema(schema.additionalProperties, options, level + 1);
215
+ response.ANY_ADDITIONAL_PROPERTY = getExampleFromSchema(schema.additionalProperties, options, level + 1, undefined, undefined, resultCache);
218
216
  }
219
217
  }
220
218
  if (schema.anyOf !== undefined) {
221
- Object.assign(response, getExampleFromSchema(schema.anyOf[0], options, level + 1));
219
+ Object.assign(response, getExampleFromSchema(schema.anyOf[0], options, level + 1, undefined, undefined, resultCache));
222
220
  }
223
221
  else if (schema.oneOf !== undefined) {
224
- Object.assign(response, getExampleFromSchema(schema.oneOf[0], options, level + 1));
222
+ Object.assign(response, getExampleFromSchema(schema.oneOf[0], options, level + 1, undefined, undefined, resultCache));
225
223
  }
226
224
  else if (schema.allOf !== undefined) {
227
225
  Object.assign.apply(Object, __spreadArray([response], schema.allOf
228
226
  .map(function (item) {
229
- return getExampleFromSchema(item, options, level + 1, schema);
227
+ return getExampleFromSchema(item, options, level + 1, schema, undefined, resultCache);
230
228
  })
231
229
  .filter(function (item) { return item !== undefined; }), false));
232
230
  }
@@ -245,13 +243,13 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
245
243
  if (schema.items.allOf) {
246
244
  // If the first item is an object type, merge all schemas
247
245
  if (schema.items.allOf[0].type === 'object') {
248
- var mergedExample = getExampleFromSchema({ type: 'object', allOf: schema.items.allOf }, options, level + 1, schema);
246
+ var mergedExample = getExampleFromSchema({ type: 'object', allOf: schema.items.allOf }, options, level + 1, schema, undefined, resultCache);
249
247
  return cache(schema, wrapItems ? [(_b = {}, _b[itemsXmlTagName_1] = mergedExample, _b)] : [mergedExample]);
250
248
  }
251
249
  // For non-objects (like strings), collect all examples
252
250
  var examples = schema.items.allOf
253
251
  .map(function (item) {
254
- return getExampleFromSchema(item, options, level + 1, schema);
252
+ return getExampleFromSchema(item, options, level + 1, schema, undefined, resultCache);
255
253
  })
256
254
  .filter(function (item) { return item !== undefined; });
257
255
  return cache(schema, wrapItems
@@ -271,14 +269,14 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
271
269
  var schemas = schema.items[rule].slice(0, 1);
272
270
  var exampleFromRule = schemas
273
271
  .map(function (item) {
274
- return getExampleFromSchema(item, options, level + 1, schema);
272
+ return getExampleFromSchema(item, options, level + 1, schema, undefined, resultCache);
275
273
  })
276
274
  .filter(function (item) { return item !== undefined; });
277
275
  return cache(schema, wrapItems ? [(_c = {}, _c[itemsXmlTagName_1] = exampleFromRule, _c)] : exampleFromRule);
278
276
  }
279
277
  }
280
278
  if ((_s = schema.items) === null || _s === void 0 ? void 0 : _s.type) {
281
- var exampleFromSchema = getExampleFromSchema(schema.items, options, level + 1);
279
+ var exampleFromSchema = getExampleFromSchema(schema.items, options, level + 1, undefined, undefined, resultCache);
282
280
  return wrapItems ? [(_d = {}, _d[itemsXmlTagName_1] = exampleFromSchema, _d)] : [exampleFromSchema];
283
281
  }
284
282
  return [];
@@ -299,7 +297,7 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
299
297
  // Get the first item from the `oneOf` | `anyOf` array
300
298
  var firstOneOfItem = discriminateSchema[0];
301
299
  // Return an example for the first item
302
- return getExampleFromSchema(firstOneOfItem, options, level + 1);
300
+ return getExampleFromSchema(firstOneOfItem, options, level + 1, undefined, undefined, resultCache);
303
301
  }
304
302
  // Check if schema has the `allOf` key
305
303
  if (Array.isArray(schema.allOf)) {
@@ -307,7 +305,7 @@ var getExampleFromSchema = function (schema, options, level, parentSchema, name)
307
305
  // Loop through all `allOf` schemas
308
306
  schema.allOf.forEach(function (allOfItem) {
309
307
  // Return an example from the schema
310
- var newExample = getExampleFromSchema(allOfItem, options, level + 1);
308
+ var newExample = getExampleFromSchema(allOfItem, options, level + 1, undefined, undefined, resultCache);
311
309
  // Merge or overwrite the example
312
310
  example_1 =
313
311
  typeof newExample === 'object' && typeof example_1 === 'object'
@@ -7,5 +7,9 @@ export declare function OpenAPISchemas(props: {
7
7
  className?: string;
8
8
  data: OpenAPISchemasData;
9
9
  context: OpenAPISchemasContextProps;
10
+ /**
11
+ * Whether to show the schema directly if there is only one.
12
+ */
13
+ grouped?: boolean;
10
14
  }): import("react").JSX.Element | null;
11
15
  export {};
@@ -6,7 +6,7 @@ import { Section, SectionBody } from '../StaticSection';
6
6
  * Display OpenAPI Schemas.
7
7
  */
8
8
  export function OpenAPISchemas(props) {
9
- var className = props.className, data = props.data, context = props.context;
9
+ var className = props.className, data = props.data, context = props.context, grouped = props.grouped;
10
10
  var schemas = data.schemas;
11
11
  var clientContext = {
12
12
  defaultInteractiveOpened: context.defaultInteractiveOpened,
@@ -17,7 +17,7 @@ export function OpenAPISchemas(props) {
17
17
  return null;
18
18
  }
19
19
  return (<div className={clsx('openapi-schemas', className)}>
20
- <OpenAPIRootSchemasSchema schemas={schemas} context={clientContext}/>
20
+ <OpenAPIRootSchemasSchema grouped={grouped} schemas={schemas} context={clientContext}/>
21
21
  </div>);
22
22
  }
23
23
  /**
@@ -26,9 +26,9 @@ export function OpenAPISchemas(props) {
26
26
  */
27
27
  function OpenAPIRootSchemasSchema(props) {
28
28
  var _a;
29
- var schemas = props.schemas, context = props.context;
30
- // If there is only one model, we show it directly.
31
- if (schemas.length === 1) {
29
+ var schemas = props.schemas, context = props.context, grouped = props.grouped;
30
+ // If there is only one model and we are not grouping, we show it directly.
31
+ if (schemas.length === 1 && !grouped) {
32
32
  var schema = (_a = schemas === null || schemas === void 0 ? void 0 : schemas[0]) === null || _a === void 0 ? void 0 : _a.schema;
33
33
  if (!schema) {
34
34
  return null;