@gitbook/react-openapi 1.5.3 → 1.5.5
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 +20 -0
- package/dist/OpenAPICodeSample.js +6 -3
- package/dist/OpenAPISchema.js +187 -79
- package/dist/generateSchemaExample.js +20 -3
- package/dist/util/tryit-prefill.js +4 -1
- package/dist/utils.js +8 -6
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @gitbook/react-openapi
|
|
2
2
|
|
|
3
|
+
## 1.5.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [985c1b4]
|
|
8
|
+
- @gitbook/openapi-parser@3.0.7
|
|
9
|
+
|
|
10
|
+
## 1.5.4
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 4766092: Fix missing properties in allOf/oneOf
|
|
15
|
+
- 8761cee: Enhance discriminator handling in OpenAPISchema
|
|
16
|
+
- 461e15f: Add x-gitbook-prefix and x-gitbook-token-placeholder for OpenAPI security scheme
|
|
17
|
+
- 3e40b4d: Fix OpenAPI basic auth placeholder
|
|
18
|
+
- 87d68ea: Fix OpenAPI oneOf/allOf merge
|
|
19
|
+
- 344842f: Improve OpenAPI circular references
|
|
20
|
+
- Updated dependencies [461e15f]
|
|
21
|
+
- @gitbook/openapi-parser@3.0.6
|
|
22
|
+
|
|
3
23
|
## 1.5.3
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
|
@@ -175,25 +175,28 @@ function getSecurityHeaders(args) {
|
|
|
175
175
|
let scheme = security.scheme;
|
|
176
176
|
const format = resolvePrefillCodePlaceholderFromSecurityScheme({
|
|
177
177
|
security,
|
|
178
|
-
defaultPlaceholderValue: scheme?.includes("basic") ? "username:password" : "YOUR_SECRET_TOKEN"
|
|
178
|
+
defaultPlaceholderValue: scheme?.toLowerCase()?.includes("basic") ? "username:password" : "YOUR_SECRET_TOKEN"
|
|
179
179
|
});
|
|
180
180
|
if (scheme?.includes("bearer")) scheme = "Bearer";
|
|
181
181
|
else if (scheme?.includes("basic")) scheme = "Basic";
|
|
182
182
|
else if (scheme?.includes("token")) scheme = "Token";
|
|
183
|
+
else scheme = scheme ?? "";
|
|
183
184
|
headers.Authorization = `${scheme} ${format}`;
|
|
184
185
|
break;
|
|
185
186
|
}
|
|
186
187
|
case "apiKey": {
|
|
187
188
|
if (security.in !== "header") break;
|
|
188
189
|
const name = security.name ?? "Authorization";
|
|
189
|
-
|
|
190
|
+
const placeholder = resolvePrefillCodePlaceholderFromSecurityScheme({
|
|
190
191
|
security,
|
|
191
192
|
defaultPlaceholderValue: "YOUR_API_KEY"
|
|
192
193
|
});
|
|
194
|
+
const prefix = security["x-gitbook-prefix"];
|
|
195
|
+
headers[name] = prefix ? `${prefix} ${placeholder}` : placeholder;
|
|
193
196
|
break;
|
|
194
197
|
}
|
|
195
198
|
case "oauth2":
|
|
196
|
-
headers.Authorization =
|
|
199
|
+
headers.Authorization = `${security["x-gitbook-prefix"] ?? "Bearer"} ${resolvePrefillCodePlaceholderFromSecurityScheme({
|
|
197
200
|
security,
|
|
198
201
|
defaultPlaceholderValue: "YOUR_OAUTH2_TOKEN"
|
|
199
202
|
})}`;
|
package/dist/OpenAPISchema.js
CHANGED
|
@@ -18,34 +18,26 @@ import { useId } from "react";
|
|
|
18
18
|
* Render a property of an OpenAPI schema.
|
|
19
19
|
*/
|
|
20
20
|
function OpenAPISchemaProperty(props) {
|
|
21
|
-
const { circularRefs: parentCircularRefs, context, className, property,...rest } = props;
|
|
21
|
+
const { circularRefs: parentCircularRefs, context, className, property, discriminator, discriminatorValue,...rest } = props;
|
|
22
22
|
const { schema } = property;
|
|
23
23
|
const id = useId();
|
|
24
24
|
const circularRefId = parentCircularRefs.get(schema);
|
|
25
|
-
if (circularRefId) return <
|
|
25
|
+
if (circularRefId) return <OpenAPISchemaPresentation context={context} property={property} circularRefId={circularRefId} />;
|
|
26
26
|
const circularRefs = new Map(parentCircularRefs);
|
|
27
27
|
circularRefs.set(schema, id);
|
|
28
|
-
const properties = getSchemaProperties(schema);
|
|
28
|
+
const properties = getSchemaProperties(schema, discriminator, discriminatorValue);
|
|
29
29
|
const alternatives = getSchemaAlternatives(schema, new Set(circularRefs.keys()));
|
|
30
|
-
const header = <OpenAPISchemaPresentation context={context} property={property} />;
|
|
30
|
+
const header = <OpenAPISchemaPresentation id={id} context={context} property={property} />;
|
|
31
31
|
const content = (() => {
|
|
32
|
-
if (alternatives?.schemas) {
|
|
33
|
-
const { schemas, discriminator } = alternatives;
|
|
34
|
-
return <div className="openapi-schema-alternatives">
|
|
35
|
-
{schemas.map((alternativeSchema, index) => <div key={index} className="openapi-schema-alternative">
|
|
36
|
-
<OpenAPISchemaAlternative schema={alternativeSchema} discriminator={discriminator} circularRefs={circularRefs} context={context} />
|
|
37
|
-
{index < schemas.length - 1 ? <OpenAPISchemaAlternativeSeparator schema={schema} context={context} /> : null}
|
|
38
|
-
</div>)}
|
|
39
|
-
</div>;
|
|
40
|
-
}
|
|
32
|
+
if (alternatives?.schemas) return <OpenAPISchemaAlternatives alternatives={alternatives} schema={schema} circularRefs={circularRefs} context={context} parentDiscriminator={discriminator} parentDiscriminatorValue={discriminatorValue} />;
|
|
41
33
|
if (properties?.length) return <OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context} />;
|
|
42
34
|
return null;
|
|
43
35
|
})();
|
|
44
|
-
if (properties?.length) return <OpenAPIDisclosure icon={context.icons.plus}
|
|
36
|
+
if (properties?.length) return <OpenAPIDisclosure icon={context.icons.plus} header={header} label={(isExpanded) => getDisclosureLabel({
|
|
45
37
|
schema,
|
|
46
38
|
isExpanded,
|
|
47
39
|
context
|
|
48
|
-
})}
|
|
40
|
+
})}>
|
|
49
41
|
{content}
|
|
50
42
|
</OpenAPIDisclosure>;
|
|
51
43
|
return <div id={id} {...rest} className={clsx("openapi-schema", className)}>
|
|
@@ -75,34 +67,75 @@ function OpenAPIRootSchema(props) {
|
|
|
75
67
|
const id = useId();
|
|
76
68
|
const properties = getSchemaProperties(schema);
|
|
77
69
|
const description = resolveDescription(schema);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
70
|
+
const alternatives = getSchemaAlternatives(schema, new Set(parentCircularRefs.keys()));
|
|
71
|
+
const circularRefs = new Map(parentCircularRefs);
|
|
72
|
+
circularRefs.set(schema, id);
|
|
73
|
+
if (alternatives?.schemas) return <>
|
|
74
|
+
{description ? <Markdown source={description} className="openapi-schema-root-description" /> : null}
|
|
75
|
+
<OpenAPISchemaAlternatives alternatives={alternatives} schema={schema} circularRefs={circularRefs} context={context} />
|
|
76
|
+
</>;
|
|
77
|
+
if (properties?.length) return <>
|
|
82
78
|
{description ? <Markdown source={description} className="openapi-schema-root-description" /> : null}
|
|
83
79
|
<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context} />
|
|
84
80
|
</>;
|
|
85
|
-
}
|
|
86
81
|
return <OpenAPISchemaProperty className="openapi-schema-root" property={{ schema }} context={context} circularRefs={parentCircularRefs} />;
|
|
87
82
|
}
|
|
88
83
|
function OpenAPIRootSchemaFromServer(props) {
|
|
89
84
|
return <OpenAPIRootSchema schema={JSON.parse(props.schema, retrocycle())} context={props.context} />;
|
|
90
85
|
}
|
|
91
86
|
/**
|
|
87
|
+
* Get the discriminator value for a schema.
|
|
88
|
+
*/
|
|
89
|
+
function getDiscriminatorValue(schema, discriminator) {
|
|
90
|
+
if (!discriminator) return;
|
|
91
|
+
if (discriminator.mapping) {
|
|
92
|
+
const mappingEntry = Object.entries(discriminator.mapping).find(([key, ref]) => {
|
|
93
|
+
if (schema.title === ref || !!schema.title && ref.endsWith(`/${schema.title}`)) return true;
|
|
94
|
+
if (schema.title?.toLowerCase().replace(/\s/g, "").includes(key.toLowerCase())) return true;
|
|
95
|
+
return false;
|
|
96
|
+
});
|
|
97
|
+
if (mappingEntry) return mappingEntry[0];
|
|
98
|
+
}
|
|
99
|
+
if (!discriminator.propertyName || !schema.properties) return;
|
|
100
|
+
const property = schema.properties[discriminator.propertyName];
|
|
101
|
+
if (!property || checkIsReference(property)) return;
|
|
102
|
+
if (property.const) return String(property.const);
|
|
103
|
+
if (property.enum?.length === 1) return String(property.enum[0]);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Render alternatives (oneOf/allOf/anyOf) for a schema.
|
|
107
|
+
*/
|
|
108
|
+
function OpenAPISchemaAlternatives(props) {
|
|
109
|
+
const { alternatives, schema, circularRefs, context, parentDiscriminator, parentDiscriminatorValue } = props;
|
|
110
|
+
if (!alternatives?.schemas) return null;
|
|
111
|
+
const { schemas, discriminator: alternativeDiscriminator, type } = alternatives;
|
|
112
|
+
return <div className="openapi-schema-alternatives">
|
|
113
|
+
{schemas.map((alternativeSchema, index) => {
|
|
114
|
+
const effectiveDiscriminator = alternativeDiscriminator || (type === "allOf" ? parentDiscriminator : void 0);
|
|
115
|
+
const effectiveDiscriminatorValue = !alternativeDiscriminator && type === "allOf" ? parentDiscriminatorValue : void 0;
|
|
116
|
+
return <div key={index} className="openapi-schema-alternative">
|
|
117
|
+
<OpenAPISchemaAlternative schema={alternativeSchema} discriminator={effectiveDiscriminator} discriminatorValue={effectiveDiscriminatorValue} circularRefs={circularRefs} context={context} />
|
|
118
|
+
{index < schemas.length - 1 ? <OpenAPISchemaAlternativeSeparator schema={schema} context={context} /> : null}
|
|
119
|
+
</div>;
|
|
120
|
+
})}
|
|
121
|
+
</div>;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
92
124
|
* Render a tab for an alternative schema.
|
|
93
125
|
* It renders directly the properties if relevant;
|
|
94
126
|
* for primitives, it renders the schema itself.
|
|
95
127
|
*/
|
|
96
128
|
function OpenAPISchemaAlternative(props) {
|
|
97
129
|
const { schema, discriminator, circularRefs, context } = props;
|
|
98
|
-
const
|
|
130
|
+
const discriminatorValue = props.discriminatorValue || getDiscriminatorValue(schema, discriminator);
|
|
131
|
+
const properties = getSchemaProperties(schema, discriminator, discriminatorValue);
|
|
99
132
|
return properties?.length ? <OpenAPIDisclosure icon={context.icons.plus} header={<OpenAPISchemaPresentation property={{ schema }} context={context} />} label={(isExpanded) => getDisclosureLabel({
|
|
100
133
|
schema,
|
|
101
134
|
isExpanded,
|
|
102
135
|
context
|
|
103
136
|
})}>
|
|
104
137
|
<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context} />
|
|
105
|
-
</OpenAPIDisclosure> : <OpenAPISchemaProperty property={{ schema }} circularRefs={circularRefs} context={context} />;
|
|
138
|
+
</OpenAPIDisclosure> : <OpenAPISchemaProperty property={{ schema }} discriminator={discriminator} discriminatorValue={discriminatorValue} circularRefs={circularRefs} context={context} />;
|
|
106
139
|
}
|
|
107
140
|
function OpenAPISchemaAlternativeSeparator(props) {
|
|
108
141
|
const { schema, context } = props;
|
|
@@ -121,8 +154,8 @@ function OpenAPISchemaAlternativeSeparator(props) {
|
|
|
121
154
|
function OpenAPISchemaCircularRef(props) {
|
|
122
155
|
const { id, schema } = props;
|
|
123
156
|
return <div className="openapi-schema-circular">
|
|
157
|
+
<span className="openapi-schema-circular-glyph">⤷</span>
|
|
124
158
|
Circular reference to <a href={`#${id}`}>{getSchemaTitle(schema)}</a>{" "}
|
|
125
|
-
<span className="openapi-schema-circular-glyph">↩</span>
|
|
126
159
|
</div>;
|
|
127
160
|
}
|
|
128
161
|
/**
|
|
@@ -164,40 +197,81 @@ function OpenAPISchemaEnum(props) {
|
|
|
164
197
|
* Render the top row of a schema. e.g: name, type, and required status.
|
|
165
198
|
*/
|
|
166
199
|
function OpenAPISchemaPresentation(props) {
|
|
167
|
-
const { property: { schema, propertyName, required, isDiscriminatorProperty }, context } = props;
|
|
200
|
+
const { id, property: { schema, propertyName, required, isDiscriminatorProperty }, circularRefId, context } = props;
|
|
168
201
|
const description = resolveDescription(schema);
|
|
169
202
|
const example = resolveFirstExample(schema);
|
|
170
|
-
return <div className="openapi-schema-presentation">
|
|
171
|
-
<OpenAPISchemaName schema={schema} type={getSchemaTitle(schema)} propertyName={propertyName} isDiscriminatorProperty={isDiscriminatorProperty} required={required} context={context} />
|
|
172
|
-
{
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
203
|
+
return <div id={id} className="openapi-schema-presentation">
|
|
204
|
+
<OpenAPISchemaName schema={schema} type={getSchemaTitle(schema, { ignoreAlternatives: !propertyName })} propertyName={propertyName} isDiscriminatorProperty={isDiscriminatorProperty} required={required} context={context} />
|
|
205
|
+
{circularRefId ? <OpenAPISchemaCircularRef id={circularRefId} schema={schema} /> : <>
|
|
206
|
+
{typeof schema["x-deprecated-sunset"] === "string" ? <div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
|
|
207
|
+
Sunset date:{" "}
|
|
208
|
+
<span className="openapi-deprecated-sunset-date">
|
|
209
|
+
{schema["x-deprecated-sunset"]}
|
|
210
|
+
</span>
|
|
211
|
+
</div> : null}
|
|
212
|
+
{description ? <Markdown source={description} className="openapi-schema-description" /> : null}
|
|
213
|
+
{schema.default !== void 0 ? <span className="openapi-schema-default">
|
|
214
|
+
Default:{" "}
|
|
215
|
+
<code>
|
|
216
|
+
{typeof schema.default === "string" && schema.default ? schema.default : stringifyOpenAPI(schema.default)}
|
|
217
|
+
</code>
|
|
218
|
+
</span> : null}
|
|
219
|
+
{typeof example === "string" ? <span className="openapi-schema-example">
|
|
220
|
+
Example: <code>{example}</code>
|
|
221
|
+
</span> : null}
|
|
222
|
+
{schema.pattern ? <span className="openapi-schema-pattern">
|
|
223
|
+
Pattern: <code>{schema.pattern}</code>
|
|
224
|
+
</span> : null}
|
|
225
|
+
<OpenAPISchemaEnum schema={schema} context={context} />
|
|
226
|
+
</>}
|
|
192
227
|
</div>;
|
|
193
228
|
}
|
|
194
229
|
/**
|
|
230
|
+
* Process properties from a schema object into property entries.
|
|
231
|
+
*/
|
|
232
|
+
function processSchemaProperties(schema, discriminator, discriminatorValue, allRequired) {
|
|
233
|
+
const result = [];
|
|
234
|
+
if (schema.properties) Object.entries(schema.properties).forEach(([propertyName, propertySchema]) => {
|
|
235
|
+
const isDiscriminator = discriminator?.propertyName === propertyName;
|
|
236
|
+
if (checkIsReference(propertySchema)) {
|
|
237
|
+
if (!isDiscriminator || !discriminatorValue) return;
|
|
238
|
+
}
|
|
239
|
+
let finalSchema = propertySchema;
|
|
240
|
+
if (isDiscriminator && discriminatorValue) finalSchema = {
|
|
241
|
+
...propertySchema,
|
|
242
|
+
const: discriminatorValue,
|
|
243
|
+
enum: [discriminatorValue]
|
|
244
|
+
};
|
|
245
|
+
result.push({
|
|
246
|
+
propertyName,
|
|
247
|
+
required: Array.isArray(schema.required) ? schema.required.includes(propertyName) : allRequired?.has(propertyName) ? true : void 0,
|
|
248
|
+
isDiscriminatorProperty: isDiscriminator,
|
|
249
|
+
schema: finalSchema
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
if (schema.additionalProperties && !checkIsReference(schema.additionalProperties)) result.push({
|
|
253
|
+
propertyName: "Other properties",
|
|
254
|
+
schema: schema.additionalProperties === true ? {} : schema.additionalProperties
|
|
255
|
+
});
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Merge properties into a result array, with later properties overriding earlier ones.
|
|
260
|
+
*/
|
|
261
|
+
function mergeProperties(result, newProperties) {
|
|
262
|
+
for (const prop of newProperties) {
|
|
263
|
+
const existingIndex = result.findIndex((p) => p.propertyName === prop.propertyName);
|
|
264
|
+
if (existingIndex >= 0) result[existingIndex] = prop;
|
|
265
|
+
else result.push(prop);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
195
269
|
* Get the sub-properties of a schema.
|
|
196
270
|
*/
|
|
197
|
-
function getSchemaProperties(schema, discriminator) {
|
|
271
|
+
function getSchemaProperties(schema, discriminator, discriminatorValue) {
|
|
198
272
|
if (schema.type === "array" && schema.items && !checkIsReference(schema.items)) {
|
|
199
273
|
const items = schema.items;
|
|
200
|
-
const itemProperties = getSchemaProperties(items);
|
|
274
|
+
const itemProperties = getSchemaProperties(items, discriminator, discriminatorValue);
|
|
201
275
|
if (itemProperties) return itemProperties.map((prop) => ({
|
|
202
276
|
...prop,
|
|
203
277
|
isDiscriminatorProperty: discriminator?.propertyName === prop.propertyName
|
|
@@ -208,23 +282,25 @@ function getSchemaProperties(schema, discriminator) {
|
|
|
208
282
|
schema: items
|
|
209
283
|
}];
|
|
210
284
|
}
|
|
211
|
-
if (schema.
|
|
212
|
-
const
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
285
|
+
if (schema.allOf && Array.isArray(schema.allOf)) {
|
|
286
|
+
const allOfSchemas = schema.allOf.filter((s) => !checkIsReference(s));
|
|
287
|
+
if (allOfSchemas.length > 0) {
|
|
288
|
+
const result = [];
|
|
289
|
+
const allRequired = /* @__PURE__ */ new Set();
|
|
290
|
+
for (const allOfSchema of allOfSchemas) {
|
|
291
|
+
if (Array.isArray(allOfSchema.required)) allOfSchema.required.forEach((req) => allRequired.add(req));
|
|
292
|
+
const allOfProperties = getSchemaProperties(allOfSchema, discriminator, discriminatorValue);
|
|
293
|
+
if (allOfProperties) mergeProperties(result, allOfProperties);
|
|
294
|
+
}
|
|
295
|
+
if (Array.isArray(schema.required)) schema.required.forEach((req) => allRequired.add(req));
|
|
296
|
+
mergeProperties(result, processSchemaProperties(schema, discriminator, discriminatorValue, allRequired));
|
|
297
|
+
result.forEach((prop) => {
|
|
298
|
+
if (prop.propertyName && allRequired.has(prop.propertyName)) prop.required = true;
|
|
220
299
|
});
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
propertyName: "Other properties",
|
|
224
|
-
schema: schema.additionalProperties === true ? {} : schema.additionalProperties
|
|
225
|
-
});
|
|
226
|
-
return result;
|
|
300
|
+
return result.length > 0 ? result : null;
|
|
301
|
+
}
|
|
227
302
|
}
|
|
303
|
+
if (schema.type === "object" || schema.properties) return processSchemaProperties(schema, discriminator, discriminatorValue);
|
|
228
304
|
return null;
|
|
229
305
|
}
|
|
230
306
|
/**
|
|
@@ -342,23 +418,55 @@ function mergeAlternatives(alternativeType, schemasOrRefs) {
|
|
|
342
418
|
}
|
|
343
419
|
function flattenAlternatives(alternativeType, schemasOrRefs, ancestors) {
|
|
344
420
|
const latestAncestor = Array.from(ancestors).pop();
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
421
|
+
const result = [];
|
|
422
|
+
for (const schemaOrRef of schemasOrRefs) {
|
|
423
|
+
if (checkIsReference(schemaOrRef)) continue;
|
|
424
|
+
const flattened = flattenSchema(schemaOrRef, alternativeType, ancestors, latestAncestor);
|
|
425
|
+
if (flattened) result.push(...flattened);
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Flatten a schema that is an alternative of another schema.
|
|
431
|
+
*/
|
|
432
|
+
function flattenSchema(schema, alternativeType, ancestors, latestAncestor) {
|
|
433
|
+
if (schema[alternativeType] && !ancestors.has(schema)) {
|
|
434
|
+
const alternatives = getSchemaAlternatives(schema, ancestors);
|
|
435
|
+
if (alternatives?.schemas) return alternatives.schemas.map((s) => {
|
|
436
|
+
const required$2 = mergeRequiredFields(s, latestAncestor);
|
|
437
|
+
return {
|
|
438
|
+
...s,
|
|
439
|
+
...required$2 ? { required: required$2 } : {}
|
|
440
|
+
};
|
|
441
|
+
});
|
|
442
|
+
const required$1 = mergeRequiredFields(schema, latestAncestor);
|
|
443
|
+
return [{
|
|
444
|
+
...schema,
|
|
445
|
+
...required$1 ? { required: required$1 } : {}
|
|
446
|
+
}];
|
|
447
|
+
}
|
|
448
|
+
if ((alternativeType === "oneOf" || alternativeType === "anyOf") && schema.allOf && Array.isArray(schema.allOf) && !ancestors.has(schema)) {
|
|
449
|
+
const allOfSchemas = schema.allOf.filter((s) => !checkIsReference(s));
|
|
450
|
+
if (allOfSchemas.length > 0) {
|
|
451
|
+
const merged = mergeAlternatives("allOf", allOfSchemas);
|
|
452
|
+
if (merged && merged.length > 0) {
|
|
453
|
+
if (merged.length === 1) return merged.map((s) => {
|
|
454
|
+
const required$1 = mergeRequiredFields(s, latestAncestor);
|
|
455
|
+
const result = {
|
|
456
|
+
...s,
|
|
457
|
+
...required$1 ? { required: required$1 } : {}
|
|
458
|
+
};
|
|
459
|
+
if (schema.title && !s.title) result.title = schema.title;
|
|
460
|
+
return result;
|
|
461
|
+
});
|
|
462
|
+
}
|
|
354
463
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}, []);
|
|
464
|
+
}
|
|
465
|
+
const required = mergeRequiredFields(schema, latestAncestor);
|
|
466
|
+
return [{
|
|
467
|
+
...schema,
|
|
468
|
+
...required ? { required } : {}
|
|
469
|
+
}];
|
|
362
470
|
}
|
|
363
471
|
/**
|
|
364
472
|
* Merge the required fields of a schema with the required fields of its latest ancestor.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { checkIsReference } from "./utils.js";
|
|
2
|
+
import { isPlainObject } from "./contentTypeChecks.js";
|
|
2
3
|
|
|
3
4
|
//#region src/generateSchemaExample.ts
|
|
4
5
|
/**
|
|
@@ -81,6 +82,10 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name, re
|
|
|
81
82
|
resultCache.set(schema$1, result);
|
|
82
83
|
return result;
|
|
83
84
|
}
|
|
85
|
+
function mergeAllOfIntoResponse(allOfItems, response, parent) {
|
|
86
|
+
const allOfResults = allOfItems.map((item) => getExampleFromSchema(item, options, level + 1, parent, void 0, resultCache)).filter(isPlainObject);
|
|
87
|
+
if (allOfResults.length > 0) Object.assign(response, ...allOfResults);
|
|
88
|
+
}
|
|
84
89
|
if (resultCache.has(schema)) return resultCache.get(schema);
|
|
85
90
|
if (level === MAX_LEVELS_DEEP + 1) try {
|
|
86
91
|
JSON.stringify(schema);
|
|
@@ -129,9 +134,21 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name, re
|
|
|
129
134
|
if (schema.additionalProperties === true || typeof schema.additionalProperties === "object" && !Object.keys(schema.additionalProperties).length) response.ANY_ADDITIONAL_PROPERTY = "anything";
|
|
130
135
|
else if (schema.additionalProperties !== false) response.ANY_ADDITIONAL_PROPERTY = getExampleFromSchema(schema.additionalProperties, options, level + 1, void 0, void 0, resultCache);
|
|
131
136
|
}
|
|
132
|
-
if (schema.anyOf !== void 0)
|
|
133
|
-
|
|
134
|
-
|
|
137
|
+
if (schema.anyOf !== void 0) {
|
|
138
|
+
const anyOfItem = schema.anyOf[0];
|
|
139
|
+
if (anyOfItem) if (anyOfItem?.allOf !== void 0 && Array.isArray(anyOfItem.allOf)) mergeAllOfIntoResponse(anyOfItem.allOf, response, anyOfItem);
|
|
140
|
+
else {
|
|
141
|
+
const anyOfResult = getExampleFromSchema(anyOfItem, options, level + 1, void 0, void 0, resultCache);
|
|
142
|
+
if (isPlainObject(anyOfResult)) Object.assign(response, anyOfResult);
|
|
143
|
+
}
|
|
144
|
+
} else if (schema.oneOf !== void 0) {
|
|
145
|
+
const oneOfItem = schema.oneOf[0];
|
|
146
|
+
if (oneOfItem) if (oneOfItem?.allOf !== void 0 && Array.isArray(oneOfItem.allOf)) mergeAllOfIntoResponse(oneOfItem.allOf, response, oneOfItem);
|
|
147
|
+
else {
|
|
148
|
+
const oneOfResult = getExampleFromSchema(oneOfItem, options, level + 1, void 0, void 0, resultCache);
|
|
149
|
+
if (isPlainObject(oneOfResult)) Object.assign(response, oneOfResult);
|
|
150
|
+
}
|
|
151
|
+
} else if (schema.allOf !== void 0) mergeAllOfIntoResponse(schema.allOf, response, schema);
|
|
135
152
|
return cache(schema, response);
|
|
136
153
|
}
|
|
137
154
|
if (schema.type === "array" || schema.items !== void 0) {
|
|
@@ -95,7 +95,10 @@ function resolveTryItPrefillServersForOperationServers(args) {
|
|
|
95
95
|
function resolvePrefillCodePlaceholderFromSecurityScheme(args) {
|
|
96
96
|
const { security, defaultPlaceholderValue } = args;
|
|
97
97
|
const prefillExprParts = extractPrefillExpressionPartsFromSecurityScheme(security);
|
|
98
|
-
if (prefillExprParts.length === 0)
|
|
98
|
+
if (prefillExprParts.length === 0) {
|
|
99
|
+
if (security["x-gitbook-token-placeholder"]) return security["x-gitbook-token-placeholder"];
|
|
100
|
+
return defaultPlaceholderValue ?? "";
|
|
101
|
+
}
|
|
99
102
|
return toPrefillCodePlaceholder(templatePartsToExpression(prefillExprParts), defaultPlaceholderValue);
|
|
100
103
|
}
|
|
101
104
|
function extractPrefillExpressionPartsFromSecurityScheme(security) {
|
package/dist/utils.js
CHANGED
|
@@ -122,20 +122,22 @@ function getStatusCodeCategory(statusCode) {
|
|
|
122
122
|
if (Number.isNaN(code) || code < 100 || code >= 600) return "unknown";
|
|
123
123
|
return Math.floor(code / 100);
|
|
124
124
|
}
|
|
125
|
-
function getSchemaTitle(schema) {
|
|
125
|
+
function getSchemaTitle(schema, options) {
|
|
126
126
|
let type = "any";
|
|
127
127
|
if (schema.enum || schema["x-enumDescriptions"] || schema["x-gitbook-enum"]) type = `${schema.type} · enum`;
|
|
128
|
-
else if (schema.type === "array" && !!schema.items) type = `${getSchemaTitle(schema.items)}[]`;
|
|
128
|
+
else if (schema.type === "array" && !!schema.items) type = `${getSchemaTitle(schema.items, options)}[]`;
|
|
129
129
|
else if (Array.isArray(schema.type)) type = schema.type.join(" | ");
|
|
130
130
|
else if (schema.type || schema.properties) {
|
|
131
131
|
type = schema.type ?? "object";
|
|
132
132
|
if (schema.format) type += ` · ${schema.format}`;
|
|
133
133
|
if (type === "object" && schema.title) type += ` · ${schema.title.replaceAll(" ", "")}`;
|
|
134
134
|
}
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
if (!options?.ignoreAlternatives) {
|
|
136
|
+
if ("anyOf" in schema) type = "any of";
|
|
137
|
+
else if ("oneOf" in schema) type = "one of";
|
|
138
|
+
else if ("allOf" in schema) type = "all of";
|
|
139
|
+
else if ("not" in schema) type = "not";
|
|
140
|
+
}
|
|
139
141
|
return type;
|
|
140
142
|
}
|
|
141
143
|
/**
|
package/package.json
CHANGED
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
"default": "./dist/index.js"
|
|
8
8
|
}
|
|
9
9
|
},
|
|
10
|
-
"version": "1.5.
|
|
10
|
+
"version": "1.5.5",
|
|
11
11
|
"sideEffects": false,
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@gitbook/expr": "1.2.4",
|
|
14
|
-
"@gitbook/openapi-parser": "3.0.
|
|
14
|
+
"@gitbook/openapi-parser": "3.0.7",
|
|
15
15
|
"@scalar/api-client-react": "^1.3.46",
|
|
16
16
|
"@scalar/oas-utils": "^0.6.3",
|
|
17
17
|
"@scalar/types": "^0.4.0",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"@types/react": "^19.0.0",
|
|
31
31
|
"@types/react-dom": "^19.0.0",
|
|
32
32
|
"bun-types": "^1.1.20",
|
|
33
|
-
"react": "^19.0.
|
|
34
|
-
"react-dom": "^19.0.
|
|
33
|
+
"react": "^19.0.1",
|
|
34
|
+
"react-dom": "^19.0.1",
|
|
35
35
|
"tsdown": "^0.15.6",
|
|
36
36
|
"typescript": "^5.5.3"
|
|
37
37
|
},
|