@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/CHANGELOG.md +8 -0
- package/dist/OpenAPIResponse.jsx +3 -3
- package/dist/OpenAPIResponseExample.jsx +6 -1
- package/dist/OpenAPIResponses.jsx +23 -10
- package/dist/OpenAPISchema.jsx +8 -10
- package/dist/generateSchemaExample.d.ts +1 -1
- package/dist/generateSchemaExample.js +24 -26
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/OpenAPIResponse.tsx +9 -7
- package/src/OpenAPIResponseExample.tsx +6 -1
- package/src/OpenAPIResponses.tsx +37 -12
- package/src/OpenAPISchema.tsx +14 -16
- package/src/generateSchemaExample.ts +98 -36
package/CHANGELOG.md
CHANGED
package/dist/OpenAPIResponse.jsx
CHANGED
|
@@ -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
|
-
|
|
39
|
-
|
|
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: {
|
|
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
|
|
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:
|
|
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>);
|
package/dist/OpenAPISchema.jsx
CHANGED
|
@@ -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 (<
|
|
136
|
-
|
|
137
|
-
<
|
|
138
|
-
|
|
139
|
-
<
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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)
|
|
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'
|