docusaurus-plugin-openapi-docs 0.0.0-1011 → 0.0.0-1012

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.
@@ -5,258 +5,9 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
 
8
- import chalk from "chalk";
9
- import merge from "lodash/merge";
10
-
8
+ import { sampleFromSchema } from "./createSchemaExample";
11
9
  import { SchemaObject } from "./types";
12
- import { mergeAllOf } from "../markdown/createSchema";
13
-
14
- interface OASTypeToTypeMap {
15
- string: string;
16
- number: number;
17
- integer: number;
18
- boolean: boolean;
19
- object: any;
20
- array: any[];
21
- null: string | null;
22
- }
23
-
24
- type Primitives = {
25
- [OASType in keyof OASTypeToTypeMap]: {
26
- [format: string]: (schema: SchemaObject) => OASTypeToTypeMap[OASType];
27
- };
28
- };
29
-
30
- const primitives: Primitives = {
31
- string: {
32
- default: () => "string",
33
- email: () => "user@example.com",
34
- date: () => "2024-07-29",
35
- "date-time": () => "2024-07-29T15:51:28.071Z",
36
- uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
37
- hostname: () => "example.com",
38
- ipv4: () => "198.51.100.42",
39
- ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
40
- },
41
- number: {
42
- default: () => 0,
43
- float: () => 0.0,
44
- },
45
- integer: {
46
- default: () => 0,
47
- },
48
- boolean: {
49
- default: (schema) =>
50
- typeof schema.default === "boolean" ? schema.default : true,
51
- },
52
- object: {},
53
- array: {},
54
- null: {
55
- default: () => "null",
56
- },
57
- };
58
-
59
- function sampleRequestFromProp(name: string, prop: any, obj: any): any {
60
- // Handle resolved circular props
61
- if (typeof prop === "object" && Object.keys(prop).length === 0) {
62
- obj[name] = prop;
63
- return obj;
64
- }
65
-
66
- // TODO: handle discriminators
67
-
68
- if (prop.oneOf) {
69
- obj[name] = sampleRequestFromSchema(prop.oneOf[0]);
70
- } else if (prop.anyOf) {
71
- obj[name] = sampleRequestFromSchema(prop.anyOf[0]);
72
- } else if (prop.allOf) {
73
- const mergedSchemas = mergeAllOf(prop) as SchemaObject;
74
- sampleRequestFromProp(name, mergedSchemas, obj);
75
- } else {
76
- obj[name] = sampleRequestFromSchema(prop);
77
- }
78
- return obj;
79
- }
80
10
 
81
11
  export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
82
- try {
83
- // deep copy schema before processing
84
- let schemaCopy = JSON.parse(JSON.stringify(schema));
85
- let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
86
-
87
- if (example !== undefined) {
88
- return example;
89
- }
90
-
91
- if (oneOf) {
92
- if (properties) {
93
- const combinedSchemas = merge(schemaCopy, oneOf[0]);
94
- delete combinedSchemas.oneOf;
95
- return sampleRequestFromSchema(combinedSchemas);
96
- }
97
- // Just go with first schema
98
- return sampleRequestFromSchema(oneOf[0]);
99
- }
100
-
101
- if (anyOf) {
102
- if (properties) {
103
- const combinedSchemas = merge(schemaCopy, anyOf[0]);
104
- delete combinedSchemas.anyOf;
105
- return sampleRequestFromSchema(combinedSchemas);
106
- }
107
- // Just go with first schema
108
- return sampleRequestFromSchema(anyOf[0]);
109
- }
110
-
111
- if (allOf) {
112
- const mergedSchemas = mergeAllOf(schemaCopy) as SchemaObject;
113
- if (mergedSchemas.properties) {
114
- for (const [key, value] of Object.entries(mergedSchemas.properties)) {
115
- if ((value.readOnly && value.readOnly === true) || value.deprecated) {
116
- delete mergedSchemas.properties[key];
117
- }
118
- }
119
- }
120
- if (properties) {
121
- const combinedSchemas = merge(schemaCopy, mergedSchemas);
122
- delete combinedSchemas.allOf;
123
- return sampleRequestFromSchema(combinedSchemas);
124
- }
125
- return sampleRequestFromSchema(mergedSchemas);
126
- }
127
-
128
- if (!type) {
129
- if (properties) {
130
- type = "object";
131
- } else if (items) {
132
- type = "array";
133
- } else {
134
- return;
135
- }
136
- }
137
-
138
- if (type === "object") {
139
- let obj: any = {};
140
- for (let [name, prop] of Object.entries(properties ?? {}) as any) {
141
- if (prop.properties) {
142
- for (const [key, value] of Object.entries(prop.properties) as any) {
143
- if (
144
- (value.readOnly && value.readOnly === true) ||
145
- value.deprecated
146
- ) {
147
- delete prop.properties[key];
148
- }
149
- }
150
- }
151
-
152
- if (prop.items && prop.items.properties) {
153
- for (const [key, value] of Object.entries(
154
- prop.items.properties
155
- ) as any) {
156
- if (
157
- (value.readOnly && value.readOnly === true) ||
158
- value.deprecated
159
- ) {
160
- delete prop.items.properties[key];
161
- }
162
- }
163
- }
164
-
165
- if (prop.readOnly && prop.readOnly === true) {
166
- continue;
167
- }
168
-
169
- if (prop.deprecated) {
170
- continue;
171
- }
172
-
173
- // Resolve schema from prop recursively
174
- obj = sampleRequestFromProp(name, prop, obj);
175
- }
176
- return obj;
177
- }
178
-
179
- if (type === "array") {
180
- if (Array.isArray(items?.anyOf)) {
181
- return processArrayItems(items, "anyOf");
182
- }
183
-
184
- if (Array.isArray(items?.oneOf)) {
185
- return processArrayItems(items, "oneOf");
186
- }
187
-
188
- return normalizeArray(sampleRequestFromSchema(items));
189
- }
190
-
191
- if (schemaCopy.enum) {
192
- if (schemaCopy.default) {
193
- return schemaCopy.default;
194
- }
195
- return normalizeArray(schemaCopy.enum)[0];
196
- }
197
-
198
- if (
199
- (schema.readOnly && schema.readOnly === true) ||
200
- schemaCopy.deprecated
201
- ) {
202
- return undefined;
203
- }
204
-
205
- return primitive(schemaCopy);
206
- } catch (err) {
207
- console.error(
208
- chalk.yellow("WARNING: failed to create example from schema object:", err)
209
- );
210
- return;
211
- }
12
+ return sampleFromSchema(schema, { type: "request" });
212
13
  };
213
-
214
- function primitive(schema: SchemaObject = {}) {
215
- let { type, format } = schema;
216
-
217
- if (type === undefined) {
218
- return;
219
- }
220
-
221
- let fn = schema.default ? () => schema.default : primitives[type].default;
222
-
223
- if (format !== undefined) {
224
- fn = primitives[type][format] || fn;
225
- }
226
-
227
- if (fn) {
228
- return fn(schema);
229
- }
230
-
231
- return "Unknown Type: " + schema.type;
232
- }
233
-
234
- function normalizeArray(arr: any) {
235
- if (Array.isArray(arr)) {
236
- return arr;
237
- }
238
- return [arr];
239
- }
240
-
241
- function processArrayItems(
242
- items: SchemaObject,
243
- schemaType: "anyOf" | "oneOf"
244
- ): any[] {
245
- const itemsArray = items[schemaType] as SchemaObject[];
246
- return itemsArray.map((item: SchemaObject) => {
247
- // If items has properties, merge them with each item
248
- if (items.properties) {
249
- const combinedSchema = {
250
- ...item,
251
- properties: {
252
- ...items.properties, // Common properties from parent
253
- ...item.properties, // Specific properties from this anyOf/oneOf item
254
- },
255
- };
256
- // Remove anyOf/oneOf to prevent infinite recursion when calling sampleRequestFromSchema
257
- delete combinedSchema[schemaType];
258
- return sampleRequestFromSchema(combinedSchema);
259
- }
260
- return sampleRequestFromSchema(item);
261
- });
262
- }
@@ -5,261 +5,9 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
 
8
- import chalk from "chalk";
9
- import merge from "lodash/merge";
10
-
8
+ import { sampleFromSchema } from "./createSchemaExample";
11
9
  import { SchemaObject } from "./types";
12
- import { mergeAllOf } from "../markdown/createSchema";
13
-
14
- interface OASTypeToTypeMap {
15
- string: string;
16
- number: number;
17
- integer: number;
18
- boolean: boolean;
19
- object: any;
20
- array: any[];
21
- null: string | null;
22
- }
23
-
24
- type Primitives = {
25
- [OASType in keyof OASTypeToTypeMap]: {
26
- [format: string]: (schema: SchemaObject) => OASTypeToTypeMap[OASType];
27
- };
28
- };
29
-
30
- const primitives: Primitives = {
31
- string: {
32
- default: () => "string",
33
- email: () => "user@example.com",
34
- date: () => "2024-07-29",
35
- "date-time": () => "2024-07-29T15:51:28.071Z",
36
- uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
37
- hostname: () => "example.com",
38
- ipv4: () => "198.51.100.42",
39
- ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
40
- },
41
- number: {
42
- default: () => 0,
43
- float: () => 0.0,
44
- },
45
- integer: {
46
- default: () => 0,
47
- },
48
- boolean: {
49
- default: (schema) =>
50
- typeof schema.default === "boolean" ? schema.default : true,
51
- },
52
- object: {},
53
- array: {},
54
- null: {
55
- default: () => "null",
56
- },
57
- };
58
-
59
- function sampleResponseFromProp(name: string, prop: any, obj: any): any {
60
- // Handle resolved circular props
61
- if (typeof prop === "object" && Object.keys(prop).length === 0) {
62
- obj[name] = prop;
63
- return obj;
64
- }
65
-
66
- // TODO: handle discriminators
67
-
68
- if (prop.oneOf) {
69
- obj[name] = sampleResponseFromSchema(prop.oneOf[0]);
70
- } else if (prop.anyOf) {
71
- obj[name] = sampleResponseFromSchema(prop.anyOf[0]);
72
- } else if (prop.allOf) {
73
- const mergedSchemas = mergeAllOf(prop) as SchemaObject;
74
- sampleResponseFromProp(name, mergedSchemas, obj);
75
- } else {
76
- obj[name] = sampleResponseFromSchema(prop);
77
- }
78
- return obj;
79
- }
80
10
 
81
11
  export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
82
- try {
83
- // deep copy schema before processing
84
- let schemaCopy = JSON.parse(JSON.stringify(schema));
85
- let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
86
-
87
- if (example !== undefined) {
88
- return example;
89
- }
90
-
91
- if (allOf) {
92
- const mergedSchemas = mergeAllOf(schemaCopy) as SchemaObject;
93
- if (mergedSchemas.properties) {
94
- for (const [key, value] of Object.entries(mergedSchemas.properties)) {
95
- if (
96
- (value.writeOnly && value.writeOnly === true) ||
97
- value.deprecated
98
- ) {
99
- delete mergedSchemas.properties[key];
100
- }
101
- }
102
- }
103
- if (properties) {
104
- const combinedSchemas = merge(schemaCopy, mergedSchemas);
105
- delete combinedSchemas.allOf;
106
- return sampleResponseFromSchema(combinedSchemas);
107
- }
108
- return sampleResponseFromSchema(mergedSchemas);
109
- }
110
-
111
- if (oneOf) {
112
- if (properties) {
113
- const combinedSchemas = merge(schemaCopy, oneOf[0]);
114
- delete combinedSchemas.oneOf;
115
- return sampleResponseFromSchema(combinedSchemas);
116
- }
117
- // Just go with first schema
118
- return sampleResponseFromSchema(oneOf[0]);
119
- }
120
-
121
- if (anyOf) {
122
- if (properties) {
123
- const combinedSchemas = merge(schemaCopy, anyOf[0]);
124
- delete combinedSchemas.anyOf;
125
- return sampleResponseFromSchema(combinedSchemas);
126
- }
127
- // Just go with first schema
128
- return sampleResponseFromSchema(anyOf[0]);
129
- }
130
-
131
- if (!type) {
132
- if (properties) {
133
- type = "object";
134
- } else if (items) {
135
- type = "array";
136
- } else {
137
- return;
138
- }
139
- }
140
-
141
- if (type === "object") {
142
- let obj: any = {};
143
- for (let [name, prop] of Object.entries(properties ?? {}) as any) {
144
- if (prop.properties) {
145
- for (const [key, value] of Object.entries(prop.properties) as any) {
146
- if (
147
- (value.writeOnly && value.writeOnly === true) ||
148
- value.deprecated
149
- ) {
150
- delete prop.properties[key];
151
- }
152
- }
153
- }
154
-
155
- if (prop.items && prop.items.properties) {
156
- for (const [key, value] of Object.entries(
157
- prop.items.properties
158
- ) as any) {
159
- if (
160
- (value.writeOnly && value.writeOnly === true) ||
161
- value.deprecated
162
- ) {
163
- delete prop.items.properties[key];
164
- }
165
- }
166
- }
167
-
168
- if (prop.writeOnly && prop.writeOnly === true) {
169
- continue;
170
- }
171
-
172
- if (prop.deprecated) {
173
- continue;
174
- }
175
-
176
- // Resolve schema from prop recursively
177
- obj = sampleResponseFromProp(name, prop, obj);
178
- }
179
- return obj;
180
- }
181
-
182
- if (type === "array") {
183
- if (Array.isArray(items?.anyOf)) {
184
- return processArrayItems(items, "anyOf");
185
- }
186
-
187
- if (Array.isArray(items?.oneOf)) {
188
- return processArrayItems(items, "oneOf");
189
- }
190
-
191
- return [sampleResponseFromSchema(items)];
192
- }
193
-
194
- if (schemaCopy.enum) {
195
- if (schemaCopy.default) {
196
- return schemaCopy.default;
197
- }
198
- return normalizeArray(schemaCopy.enum)[0];
199
- }
200
-
201
- if (
202
- (schemaCopy.writeOnly && schemaCopy.writeOnly === true) ||
203
- schemaCopy.deprecated
204
- ) {
205
- return undefined;
206
- }
207
-
208
- return primitive(schemaCopy);
209
- } catch (err) {
210
- console.error(
211
- chalk.yellow("WARNING: failed to create example from schema object:", err)
212
- );
213
- return;
214
- }
12
+ return sampleFromSchema(schema, { type: "response" });
215
13
  };
216
-
217
- function primitive(schema: SchemaObject = {}) {
218
- let { type, format } = schema;
219
-
220
- if (type === undefined) {
221
- return;
222
- }
223
-
224
- let fn = schema.default ? () => schema.default : primitives[type].default;
225
-
226
- if (format !== undefined) {
227
- fn = primitives[type][format] || fn;
228
- }
229
-
230
- if (fn) {
231
- return fn(schema);
232
- }
233
-
234
- return "Unknown Type: " + schema.type;
235
- }
236
-
237
- function normalizeArray(arr: any) {
238
- if (Array.isArray(arr)) {
239
- return arr;
240
- }
241
- return [arr];
242
- }
243
-
244
- function processArrayItems(
245
- items: SchemaObject,
246
- schemaType: "anyOf" | "oneOf"
247
- ): any[] {
248
- const itemsArray = items[schemaType] as SchemaObject[];
249
- return itemsArray.map((item: SchemaObject) => {
250
- // If items has properties, merge them with each item
251
- if (items.properties) {
252
- const combinedSchema = {
253
- ...item,
254
- properties: {
255
- ...items.properties, // Common properties from parent
256
- ...item.properties, // Specific properties from this anyOf/oneOf item
257
- },
258
- };
259
- // Remove anyOf/oneOf to prevent infinite recursion when calling sampleResponseFromSchema
260
- delete combinedSchema[schemaType];
261
- return sampleResponseFromSchema(combinedSchema);
262
- }
263
- return sampleResponseFromSchema(item);
264
- });
265
- }