docusaurus-plugin-openapi-docs 4.4.0 → 4.5.0

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