docusaurus-plugin-openapi-docs 0.0.0-1011 → 0.0.0-1013
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/lib/openapi/createRequestExample.js +2 -201
- package/lib/openapi/createResponseExample.js +2 -202
- package/lib/openapi/createSchemaExample.d.ts +7 -0
- package/lib/openapi/createSchemaExample.js +231 -0
- package/package.json +2 -2
- package/src/openapi/createRequestExample.ts +2 -251
- package/src/openapi/createResponseExample.ts +2 -254
- package/src/openapi/createSchemaExample.ts +292 -0
|
@@ -5,209 +5,10 @@
|
|
|
5
5
|
* This source code is licensed under the MIT license found in the
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
* ========================================================================== */
|
|
8
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.sampleRequestFromSchema = void 0;
|
|
13
|
-
const
|
|
14
|
-
const merge_1 = __importDefault(require("lodash/merge"));
|
|
15
|
-
const createSchema_1 = require("../markdown/createSchema");
|
|
16
|
-
const primitives = {
|
|
17
|
-
string: {
|
|
18
|
-
default: () => "string",
|
|
19
|
-
email: () => "user@example.com",
|
|
20
|
-
date: () => "2024-07-29",
|
|
21
|
-
"date-time": () => "2024-07-29T15:51:28.071Z",
|
|
22
|
-
uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
|
23
|
-
hostname: () => "example.com",
|
|
24
|
-
ipv4: () => "198.51.100.42",
|
|
25
|
-
ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
|
|
26
|
-
},
|
|
27
|
-
number: {
|
|
28
|
-
default: () => 0,
|
|
29
|
-
float: () => 0.0,
|
|
30
|
-
},
|
|
31
|
-
integer: {
|
|
32
|
-
default: () => 0,
|
|
33
|
-
},
|
|
34
|
-
boolean: {
|
|
35
|
-
default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
|
|
36
|
-
},
|
|
37
|
-
object: {},
|
|
38
|
-
array: {},
|
|
39
|
-
null: {
|
|
40
|
-
default: () => "null",
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
function sampleRequestFromProp(name, prop, obj) {
|
|
44
|
-
// Handle resolved circular props
|
|
45
|
-
if (typeof prop === "object" && Object.keys(prop).length === 0) {
|
|
46
|
-
obj[name] = prop;
|
|
47
|
-
return obj;
|
|
48
|
-
}
|
|
49
|
-
// TODO: handle discriminators
|
|
50
|
-
if (prop.oneOf) {
|
|
51
|
-
obj[name] = (0, exports.sampleRequestFromSchema)(prop.oneOf[0]);
|
|
52
|
-
}
|
|
53
|
-
else if (prop.anyOf) {
|
|
54
|
-
obj[name] = (0, exports.sampleRequestFromSchema)(prop.anyOf[0]);
|
|
55
|
-
}
|
|
56
|
-
else if (prop.allOf) {
|
|
57
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(prop);
|
|
58
|
-
sampleRequestFromProp(name, mergedSchemas, obj);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
obj[name] = (0, exports.sampleRequestFromSchema)(prop);
|
|
62
|
-
}
|
|
63
|
-
return obj;
|
|
64
|
-
}
|
|
10
|
+
const createSchemaExample_1 = require("./createSchemaExample");
|
|
65
11
|
const sampleRequestFromSchema = (schema = {}) => {
|
|
66
|
-
|
|
67
|
-
// deep copy schema before processing
|
|
68
|
-
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
69
|
-
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
70
|
-
if (example !== undefined) {
|
|
71
|
-
return example;
|
|
72
|
-
}
|
|
73
|
-
if (oneOf) {
|
|
74
|
-
if (properties) {
|
|
75
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, oneOf[0]);
|
|
76
|
-
delete combinedSchemas.oneOf;
|
|
77
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchemas);
|
|
78
|
-
}
|
|
79
|
-
// Just go with first schema
|
|
80
|
-
return (0, exports.sampleRequestFromSchema)(oneOf[0]);
|
|
81
|
-
}
|
|
82
|
-
if (anyOf) {
|
|
83
|
-
if (properties) {
|
|
84
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, anyOf[0]);
|
|
85
|
-
delete combinedSchemas.anyOf;
|
|
86
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchemas);
|
|
87
|
-
}
|
|
88
|
-
// Just go with first schema
|
|
89
|
-
return (0, exports.sampleRequestFromSchema)(anyOf[0]);
|
|
90
|
-
}
|
|
91
|
-
if (allOf) {
|
|
92
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(schemaCopy);
|
|
93
|
-
if (mergedSchemas.properties) {
|
|
94
|
-
for (const [key, value] of Object.entries(mergedSchemas.properties)) {
|
|
95
|
-
if ((value.readOnly && value.readOnly === true) || value.deprecated) {
|
|
96
|
-
delete mergedSchemas.properties[key];
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (properties) {
|
|
101
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, mergedSchemas);
|
|
102
|
-
delete combinedSchemas.allOf;
|
|
103
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchemas);
|
|
104
|
-
}
|
|
105
|
-
return (0, exports.sampleRequestFromSchema)(mergedSchemas);
|
|
106
|
-
}
|
|
107
|
-
if (!type) {
|
|
108
|
-
if (properties) {
|
|
109
|
-
type = "object";
|
|
110
|
-
}
|
|
111
|
-
else if (items) {
|
|
112
|
-
type = "array";
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (type === "object") {
|
|
119
|
-
let obj = {};
|
|
120
|
-
for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
|
|
121
|
-
if (prop.properties) {
|
|
122
|
-
for (const [key, value] of Object.entries(prop.properties)) {
|
|
123
|
-
if ((value.readOnly && value.readOnly === true) ||
|
|
124
|
-
value.deprecated) {
|
|
125
|
-
delete prop.properties[key];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (prop.items && prop.items.properties) {
|
|
130
|
-
for (const [key, value] of Object.entries(prop.items.properties)) {
|
|
131
|
-
if ((value.readOnly && value.readOnly === true) ||
|
|
132
|
-
value.deprecated) {
|
|
133
|
-
delete prop.items.properties[key];
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (prop.readOnly && prop.readOnly === true) {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
if (prop.deprecated) {
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
// Resolve schema from prop recursively
|
|
144
|
-
obj = sampleRequestFromProp(name, prop, obj);
|
|
145
|
-
}
|
|
146
|
-
return obj;
|
|
147
|
-
}
|
|
148
|
-
if (type === "array") {
|
|
149
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
|
|
150
|
-
return processArrayItems(items, "anyOf");
|
|
151
|
-
}
|
|
152
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
|
|
153
|
-
return processArrayItems(items, "oneOf");
|
|
154
|
-
}
|
|
155
|
-
return normalizeArray((0, exports.sampleRequestFromSchema)(items));
|
|
156
|
-
}
|
|
157
|
-
if (schemaCopy.enum) {
|
|
158
|
-
if (schemaCopy.default) {
|
|
159
|
-
return schemaCopy.default;
|
|
160
|
-
}
|
|
161
|
-
return normalizeArray(schemaCopy.enum)[0];
|
|
162
|
-
}
|
|
163
|
-
if ((schema.readOnly && schema.readOnly === true) ||
|
|
164
|
-
schemaCopy.deprecated) {
|
|
165
|
-
return undefined;
|
|
166
|
-
}
|
|
167
|
-
return primitive(schemaCopy);
|
|
168
|
-
}
|
|
169
|
-
catch (err) {
|
|
170
|
-
console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
12
|
+
return (0, createSchemaExample_1.sampleFromSchema)(schema, { type: "request" });
|
|
173
13
|
};
|
|
174
14
|
exports.sampleRequestFromSchema = sampleRequestFromSchema;
|
|
175
|
-
function primitive(schema = {}) {
|
|
176
|
-
let { type, format } = schema;
|
|
177
|
-
if (type === undefined) {
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
let fn = schema.default ? () => schema.default : primitives[type].default;
|
|
181
|
-
if (format !== undefined) {
|
|
182
|
-
fn = primitives[type][format] || fn;
|
|
183
|
-
}
|
|
184
|
-
if (fn) {
|
|
185
|
-
return fn(schema);
|
|
186
|
-
}
|
|
187
|
-
return "Unknown Type: " + schema.type;
|
|
188
|
-
}
|
|
189
|
-
function normalizeArray(arr) {
|
|
190
|
-
if (Array.isArray(arr)) {
|
|
191
|
-
return arr;
|
|
192
|
-
}
|
|
193
|
-
return [arr];
|
|
194
|
-
}
|
|
195
|
-
function processArrayItems(items, schemaType) {
|
|
196
|
-
const itemsArray = items[schemaType];
|
|
197
|
-
return itemsArray.map((item) => {
|
|
198
|
-
// If items has properties, merge them with each item
|
|
199
|
-
if (items.properties) {
|
|
200
|
-
const combinedSchema = {
|
|
201
|
-
...item,
|
|
202
|
-
properties: {
|
|
203
|
-
...items.properties, // Common properties from parent
|
|
204
|
-
...item.properties, // Specific properties from this anyOf/oneOf item
|
|
205
|
-
},
|
|
206
|
-
};
|
|
207
|
-
// Remove anyOf/oneOf to prevent infinite recursion when calling sampleRequestFromSchema
|
|
208
|
-
delete combinedSchema[schemaType];
|
|
209
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchema);
|
|
210
|
-
}
|
|
211
|
-
return (0, exports.sampleRequestFromSchema)(item);
|
|
212
|
-
});
|
|
213
|
-
}
|
|
@@ -5,210 +5,10 @@
|
|
|
5
5
|
* This source code is licensed under the MIT license found in the
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
* ========================================================================== */
|
|
8
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.sampleResponseFromSchema = void 0;
|
|
13
|
-
const
|
|
14
|
-
const merge_1 = __importDefault(require("lodash/merge"));
|
|
15
|
-
const createSchema_1 = require("../markdown/createSchema");
|
|
16
|
-
const primitives = {
|
|
17
|
-
string: {
|
|
18
|
-
default: () => "string",
|
|
19
|
-
email: () => "user@example.com",
|
|
20
|
-
date: () => "2024-07-29",
|
|
21
|
-
"date-time": () => "2024-07-29T15:51:28.071Z",
|
|
22
|
-
uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
|
23
|
-
hostname: () => "example.com",
|
|
24
|
-
ipv4: () => "198.51.100.42",
|
|
25
|
-
ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
|
|
26
|
-
},
|
|
27
|
-
number: {
|
|
28
|
-
default: () => 0,
|
|
29
|
-
float: () => 0.0,
|
|
30
|
-
},
|
|
31
|
-
integer: {
|
|
32
|
-
default: () => 0,
|
|
33
|
-
},
|
|
34
|
-
boolean: {
|
|
35
|
-
default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
|
|
36
|
-
},
|
|
37
|
-
object: {},
|
|
38
|
-
array: {},
|
|
39
|
-
null: {
|
|
40
|
-
default: () => "null",
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
function sampleResponseFromProp(name, prop, obj) {
|
|
44
|
-
// Handle resolved circular props
|
|
45
|
-
if (typeof prop === "object" && Object.keys(prop).length === 0) {
|
|
46
|
-
obj[name] = prop;
|
|
47
|
-
return obj;
|
|
48
|
-
}
|
|
49
|
-
// TODO: handle discriminators
|
|
50
|
-
if (prop.oneOf) {
|
|
51
|
-
obj[name] = (0, exports.sampleResponseFromSchema)(prop.oneOf[0]);
|
|
52
|
-
}
|
|
53
|
-
else if (prop.anyOf) {
|
|
54
|
-
obj[name] = (0, exports.sampleResponseFromSchema)(prop.anyOf[0]);
|
|
55
|
-
}
|
|
56
|
-
else if (prop.allOf) {
|
|
57
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(prop);
|
|
58
|
-
sampleResponseFromProp(name, mergedSchemas, obj);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
obj[name] = (0, exports.sampleResponseFromSchema)(prop);
|
|
62
|
-
}
|
|
63
|
-
return obj;
|
|
64
|
-
}
|
|
10
|
+
const createSchemaExample_1 = require("./createSchemaExample");
|
|
65
11
|
const sampleResponseFromSchema = (schema = {}) => {
|
|
66
|
-
|
|
67
|
-
// deep copy schema before processing
|
|
68
|
-
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
69
|
-
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
70
|
-
if (example !== undefined) {
|
|
71
|
-
return example;
|
|
72
|
-
}
|
|
73
|
-
if (allOf) {
|
|
74
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(schemaCopy);
|
|
75
|
-
if (mergedSchemas.properties) {
|
|
76
|
-
for (const [key, value] of Object.entries(mergedSchemas.properties)) {
|
|
77
|
-
if ((value.writeOnly && value.writeOnly === true) ||
|
|
78
|
-
value.deprecated) {
|
|
79
|
-
delete mergedSchemas.properties[key];
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (properties) {
|
|
84
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, mergedSchemas);
|
|
85
|
-
delete combinedSchemas.allOf;
|
|
86
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchemas);
|
|
87
|
-
}
|
|
88
|
-
return (0, exports.sampleResponseFromSchema)(mergedSchemas);
|
|
89
|
-
}
|
|
90
|
-
if (oneOf) {
|
|
91
|
-
if (properties) {
|
|
92
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, oneOf[0]);
|
|
93
|
-
delete combinedSchemas.oneOf;
|
|
94
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchemas);
|
|
95
|
-
}
|
|
96
|
-
// Just go with first schema
|
|
97
|
-
return (0, exports.sampleResponseFromSchema)(oneOf[0]);
|
|
98
|
-
}
|
|
99
|
-
if (anyOf) {
|
|
100
|
-
if (properties) {
|
|
101
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, anyOf[0]);
|
|
102
|
-
delete combinedSchemas.anyOf;
|
|
103
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchemas);
|
|
104
|
-
}
|
|
105
|
-
// Just go with first schema
|
|
106
|
-
return (0, exports.sampleResponseFromSchema)(anyOf[0]);
|
|
107
|
-
}
|
|
108
|
-
if (!type) {
|
|
109
|
-
if (properties) {
|
|
110
|
-
type = "object";
|
|
111
|
-
}
|
|
112
|
-
else if (items) {
|
|
113
|
-
type = "array";
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (type === "object") {
|
|
120
|
-
let obj = {};
|
|
121
|
-
for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
|
|
122
|
-
if (prop.properties) {
|
|
123
|
-
for (const [key, value] of Object.entries(prop.properties)) {
|
|
124
|
-
if ((value.writeOnly && value.writeOnly === true) ||
|
|
125
|
-
value.deprecated) {
|
|
126
|
-
delete prop.properties[key];
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (prop.items && prop.items.properties) {
|
|
131
|
-
for (const [key, value] of Object.entries(prop.items.properties)) {
|
|
132
|
-
if ((value.writeOnly && value.writeOnly === true) ||
|
|
133
|
-
value.deprecated) {
|
|
134
|
-
delete prop.items.properties[key];
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (prop.writeOnly && prop.writeOnly === true) {
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (prop.deprecated) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
// Resolve schema from prop recursively
|
|
145
|
-
obj = sampleResponseFromProp(name, prop, obj);
|
|
146
|
-
}
|
|
147
|
-
return obj;
|
|
148
|
-
}
|
|
149
|
-
if (type === "array") {
|
|
150
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
|
|
151
|
-
return processArrayItems(items, "anyOf");
|
|
152
|
-
}
|
|
153
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
|
|
154
|
-
return processArrayItems(items, "oneOf");
|
|
155
|
-
}
|
|
156
|
-
return [(0, exports.sampleResponseFromSchema)(items)];
|
|
157
|
-
}
|
|
158
|
-
if (schemaCopy.enum) {
|
|
159
|
-
if (schemaCopy.default) {
|
|
160
|
-
return schemaCopy.default;
|
|
161
|
-
}
|
|
162
|
-
return normalizeArray(schemaCopy.enum)[0];
|
|
163
|
-
}
|
|
164
|
-
if ((schemaCopy.writeOnly && schemaCopy.writeOnly === true) ||
|
|
165
|
-
schemaCopy.deprecated) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
return primitive(schemaCopy);
|
|
169
|
-
}
|
|
170
|
-
catch (err) {
|
|
171
|
-
console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
12
|
+
return (0, createSchemaExample_1.sampleFromSchema)(schema, { type: "response" });
|
|
174
13
|
};
|
|
175
14
|
exports.sampleResponseFromSchema = sampleResponseFromSchema;
|
|
176
|
-
function primitive(schema = {}) {
|
|
177
|
-
let { type, format } = schema;
|
|
178
|
-
if (type === undefined) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
let fn = schema.default ? () => schema.default : primitives[type].default;
|
|
182
|
-
if (format !== undefined) {
|
|
183
|
-
fn = primitives[type][format] || fn;
|
|
184
|
-
}
|
|
185
|
-
if (fn) {
|
|
186
|
-
return fn(schema);
|
|
187
|
-
}
|
|
188
|
-
return "Unknown Type: " + schema.type;
|
|
189
|
-
}
|
|
190
|
-
function normalizeArray(arr) {
|
|
191
|
-
if (Array.isArray(arr)) {
|
|
192
|
-
return arr;
|
|
193
|
-
}
|
|
194
|
-
return [arr];
|
|
195
|
-
}
|
|
196
|
-
function processArrayItems(items, schemaType) {
|
|
197
|
-
const itemsArray = items[schemaType];
|
|
198
|
-
return itemsArray.map((item) => {
|
|
199
|
-
// If items has properties, merge them with each item
|
|
200
|
-
if (items.properties) {
|
|
201
|
-
const combinedSchema = {
|
|
202
|
-
...item,
|
|
203
|
-
properties: {
|
|
204
|
-
...items.properties, // Common properties from parent
|
|
205
|
-
...item.properties, // Specific properties from this anyOf/oneOf item
|
|
206
|
-
},
|
|
207
|
-
};
|
|
208
|
-
// Remove anyOf/oneOf to prevent infinite recursion when calling sampleResponseFromSchema
|
|
209
|
-
delete combinedSchema[schemaType];
|
|
210
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchema);
|
|
211
|
-
}
|
|
212
|
-
return (0, exports.sampleResponseFromSchema)(item);
|
|
213
|
-
});
|
|
214
|
-
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* ============================================================================
|
|
3
|
+
* Copyright (c) Palo Alto Networks
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
* ========================================================================== */
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.sampleFromSchema = void 0;
|
|
13
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
14
|
+
const merge_1 = __importDefault(require("lodash/merge"));
|
|
15
|
+
const createSchema_1 = require("../markdown/createSchema");
|
|
16
|
+
const primitives = {
|
|
17
|
+
string: {
|
|
18
|
+
default: () => "string",
|
|
19
|
+
email: () => "user@example.com",
|
|
20
|
+
date: () => "2024-07-29",
|
|
21
|
+
"date-time": () => "2024-07-29T15:51:28.071Z",
|
|
22
|
+
uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
|
23
|
+
hostname: () => "example.com",
|
|
24
|
+
ipv4: () => "198.51.100.42",
|
|
25
|
+
ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
|
|
26
|
+
},
|
|
27
|
+
number: {
|
|
28
|
+
default: () => 0,
|
|
29
|
+
float: () => 0.0,
|
|
30
|
+
},
|
|
31
|
+
integer: {
|
|
32
|
+
default: () => 0,
|
|
33
|
+
},
|
|
34
|
+
boolean: {
|
|
35
|
+
default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
|
|
36
|
+
},
|
|
37
|
+
object: {},
|
|
38
|
+
array: {},
|
|
39
|
+
null: {
|
|
40
|
+
default: () => "null",
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
function shouldExcludeProperty(prop, context) {
|
|
44
|
+
if (prop.deprecated) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (context.type === "request") {
|
|
48
|
+
return prop.readOnly === true;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return prop.writeOnly === true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function sampleFromProp(name, prop, obj, context) {
|
|
55
|
+
// Handle resolved circular props
|
|
56
|
+
if (typeof prop === "object" && Object.keys(prop).length === 0) {
|
|
57
|
+
obj[name] = prop;
|
|
58
|
+
return obj;
|
|
59
|
+
}
|
|
60
|
+
// TODO: handle discriminators
|
|
61
|
+
if (prop.oneOf) {
|
|
62
|
+
obj[name] = (0, exports.sampleFromSchema)(prop.oneOf[0], context);
|
|
63
|
+
}
|
|
64
|
+
else if (prop.anyOf) {
|
|
65
|
+
obj[name] = (0, exports.sampleFromSchema)(prop.anyOf[0], context);
|
|
66
|
+
}
|
|
67
|
+
else if (prop.allOf) {
|
|
68
|
+
const mergedSchemas = (0, createSchema_1.mergeAllOf)(prop);
|
|
69
|
+
sampleFromProp(name, mergedSchemas, obj, context);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
obj[name] = (0, exports.sampleFromSchema)(prop, context);
|
|
73
|
+
}
|
|
74
|
+
return obj;
|
|
75
|
+
}
|
|
76
|
+
const sampleFromSchema = (schema = {}, context) => {
|
|
77
|
+
try {
|
|
78
|
+
// deep copy schema before processing
|
|
79
|
+
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
80
|
+
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
81
|
+
if (example !== undefined) {
|
|
82
|
+
return example;
|
|
83
|
+
}
|
|
84
|
+
if (oneOf) {
|
|
85
|
+
if (properties) {
|
|
86
|
+
const combinedSchemas = (0, merge_1.default)(schemaCopy, oneOf[0]);
|
|
87
|
+
delete combinedSchemas.oneOf;
|
|
88
|
+
return (0, exports.sampleFromSchema)(combinedSchemas, context);
|
|
89
|
+
}
|
|
90
|
+
// Just go with first schema
|
|
91
|
+
return (0, exports.sampleFromSchema)(oneOf[0], context);
|
|
92
|
+
}
|
|
93
|
+
if (anyOf) {
|
|
94
|
+
if (properties) {
|
|
95
|
+
const combinedSchemas = (0, merge_1.default)(schemaCopy, anyOf[0]);
|
|
96
|
+
delete combinedSchemas.anyOf;
|
|
97
|
+
return (0, exports.sampleFromSchema)(combinedSchemas, context);
|
|
98
|
+
}
|
|
99
|
+
// Just go with first schema
|
|
100
|
+
return (0, exports.sampleFromSchema)(anyOf[0], context);
|
|
101
|
+
}
|
|
102
|
+
if (allOf) {
|
|
103
|
+
const mergedSchemas = (0, createSchema_1.mergeAllOf)(schemaCopy);
|
|
104
|
+
if (mergedSchemas.properties) {
|
|
105
|
+
for (const [key, value] of Object.entries(mergedSchemas.properties)) {
|
|
106
|
+
if (shouldExcludeProperty(value, context)) {
|
|
107
|
+
delete mergedSchemas.properties[key];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (properties) {
|
|
112
|
+
const combinedSchemas = (0, merge_1.default)(schemaCopy, mergedSchemas);
|
|
113
|
+
delete combinedSchemas.allOf;
|
|
114
|
+
return (0, exports.sampleFromSchema)(combinedSchemas, context);
|
|
115
|
+
}
|
|
116
|
+
return (0, exports.sampleFromSchema)(mergedSchemas, context);
|
|
117
|
+
}
|
|
118
|
+
if (!type) {
|
|
119
|
+
if (properties) {
|
|
120
|
+
type = "object";
|
|
121
|
+
}
|
|
122
|
+
else if (items) {
|
|
123
|
+
type = "array";
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (type === "object") {
|
|
130
|
+
let obj = {};
|
|
131
|
+
for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
|
|
132
|
+
if (prop.properties) {
|
|
133
|
+
for (const [key, value] of Object.entries(prop.properties)) {
|
|
134
|
+
if (shouldExcludeProperty(value, context)) {
|
|
135
|
+
delete prop.properties[key];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (prop.items && prop.items.properties) {
|
|
140
|
+
for (const [key, value] of Object.entries(prop.items.properties)) {
|
|
141
|
+
if (shouldExcludeProperty(value, context)) {
|
|
142
|
+
delete prop.items.properties[key];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (shouldExcludeProperty(prop, context)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// Resolve schema from prop recursively
|
|
150
|
+
obj = sampleFromProp(name, prop, obj, context);
|
|
151
|
+
}
|
|
152
|
+
return obj;
|
|
153
|
+
}
|
|
154
|
+
if (type === "array") {
|
|
155
|
+
if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
|
|
156
|
+
return processArrayItems(items, "anyOf", context);
|
|
157
|
+
}
|
|
158
|
+
if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
|
|
159
|
+
return processArrayItems(items, "oneOf", context);
|
|
160
|
+
}
|
|
161
|
+
return normalizeArray((0, exports.sampleFromSchema)(items, context));
|
|
162
|
+
}
|
|
163
|
+
if (schemaCopy.enum) {
|
|
164
|
+
if (schemaCopy.default) {
|
|
165
|
+
return schemaCopy.default;
|
|
166
|
+
}
|
|
167
|
+
return normalizeArray(schemaCopy.enum)[0];
|
|
168
|
+
}
|
|
169
|
+
if (shouldExcludeProperty(schemaCopy, context)) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
return primitive(schemaCopy);
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
exports.sampleFromSchema = sampleFromSchema;
|
|
180
|
+
function primitive(schema = {}) {
|
|
181
|
+
let { type, format } = schema;
|
|
182
|
+
if (type === undefined) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// If type is an array, use the first type
|
|
186
|
+
if (Array.isArray(type)) {
|
|
187
|
+
type = type[0];
|
|
188
|
+
if (type === undefined) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// Use schema default if available, otherwise use type default
|
|
193
|
+
if (schema.default !== undefined) {
|
|
194
|
+
return schema.default;
|
|
195
|
+
}
|
|
196
|
+
const typeConfig = primitives[type];
|
|
197
|
+
if (typeConfig) {
|
|
198
|
+
if (format !== undefined && typeConfig[format] !== undefined) {
|
|
199
|
+
return typeConfig[format](schema);
|
|
200
|
+
}
|
|
201
|
+
if (typeConfig.default !== undefined) {
|
|
202
|
+
return typeConfig.default(schema);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return "Unknown Type: " + schema.type;
|
|
206
|
+
}
|
|
207
|
+
function normalizeArray(arr) {
|
|
208
|
+
if (Array.isArray(arr)) {
|
|
209
|
+
return arr;
|
|
210
|
+
}
|
|
211
|
+
return [arr];
|
|
212
|
+
}
|
|
213
|
+
function processArrayItems(items, schemaType, context) {
|
|
214
|
+
const itemsArray = items[schemaType];
|
|
215
|
+
return itemsArray.map((item) => {
|
|
216
|
+
// If items has properties, merge them with each item
|
|
217
|
+
if (items.properties) {
|
|
218
|
+
const combinedSchema = {
|
|
219
|
+
...item,
|
|
220
|
+
properties: {
|
|
221
|
+
...items.properties, // Common properties from parent
|
|
222
|
+
...item.properties, // Specific properties from this anyOf/oneOf item
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
// Remove anyOf/oneOf to prevent infinite recursion when calling sampleFromSchema
|
|
226
|
+
delete combinedSchema[schemaType];
|
|
227
|
+
return (0, exports.sampleFromSchema)(combinedSchema, context);
|
|
228
|
+
}
|
|
229
|
+
return (0, exports.sampleFromSchema)(item, context);
|
|
230
|
+
});
|
|
231
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "docusaurus-plugin-openapi-docs",
|
|
3
3
|
"description": "OpenAPI plugin for Docusaurus.",
|
|
4
|
-
"version": "0.0.0-
|
|
4
|
+
"version": "0.0.0-1013",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"openapi",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"engines": {
|
|
66
66
|
"node": ">=14"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "fbbf74a2c238e78008b551a5671feb0bd35f83b6"
|
|
69
69
|
}
|