docusaurus-plugin-openapi-docs 0.0.0-429 → 0.0.0-433

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.
@@ -0,0 +1,205 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import chalk from "chalk";
9
+
10
+ import { mergeAllOf } from "../markdown/createRequestSchema";
11
+ import { SchemaObject } from "./types";
12
+
13
+ interface OASTypeToTypeMap {
14
+ string: string;
15
+ number: number;
16
+ integer: number;
17
+ boolean: boolean;
18
+ object: any;
19
+ array: any[];
20
+ }
21
+
22
+ type Primitives = {
23
+ [OASType in keyof OASTypeToTypeMap]: {
24
+ [format: string]: (schema: SchemaObject) => OASTypeToTypeMap[OASType];
25
+ };
26
+ };
27
+
28
+ const primitives: Primitives = {
29
+ string: {
30
+ default: () => "string",
31
+ email: () => "user@example.com",
32
+ date: () => new Date().toISOString().substring(0, 10),
33
+ "date-time": () => new Date().toISOString().substring(0, 10),
34
+ uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
35
+ hostname: () => "example.com",
36
+ ipv4: () => "198.51.100.42",
37
+ ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
38
+ },
39
+ number: {
40
+ default: () => 0,
41
+ float: () => 0.0,
42
+ },
43
+ integer: {
44
+ default: () => 0,
45
+ },
46
+ boolean: {
47
+ default: (schema) =>
48
+ typeof schema.default === "boolean" ? schema.default : true,
49
+ },
50
+ object: {},
51
+ array: {},
52
+ };
53
+
54
+ function sampleResponseFromProp(name: string, prop: any, obj: any): any {
55
+ // Handle resolved circular props
56
+ if (typeof prop === "object" && Object.keys(prop).length === 0) {
57
+ obj[name] = prop;
58
+ return obj;
59
+ }
60
+
61
+ // TODO: handle discriminators
62
+
63
+ if (prop.oneOf) {
64
+ obj[name] = sampleResponseFromSchema(prop.oneOf[0]);
65
+ } else if (prop.anyOf) {
66
+ obj[name] = sampleResponseFromSchema(prop.anyOf[0]);
67
+ } else if (prop.allOf) {
68
+ const { mergedSchemas }: { mergedSchemas: SchemaObject } = mergeAllOf(
69
+ prop.allOf
70
+ );
71
+ sampleResponseFromProp(name, mergedSchemas, obj);
72
+ } else {
73
+ obj[name] = sampleResponseFromSchema(prop);
74
+ }
75
+ return obj;
76
+ }
77
+
78
+ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
79
+ try {
80
+ let { type, example, allOf, oneOf, anyOf, properties, items } = schema;
81
+
82
+ if (example !== undefined) {
83
+ return example;
84
+ }
85
+
86
+ if (allOf) {
87
+ const { mergedSchemas }: { mergedSchemas: SchemaObject } =
88
+ mergeAllOf(allOf);
89
+ if (mergedSchemas.properties) {
90
+ for (const [key, value] of Object.entries(mergedSchemas.properties)) {
91
+ if (value.readOnly && value.readOnly === true) {
92
+ delete mergedSchemas.properties[key];
93
+ }
94
+ }
95
+ }
96
+ return sampleResponseFromSchema(mergedSchemas);
97
+ }
98
+
99
+ if (oneOf) {
100
+ // Just go with first schema
101
+ return sampleResponseFromSchema(oneOf[0]);
102
+ }
103
+
104
+ if (anyOf) {
105
+ // Just go with first schema
106
+ return sampleResponseFromSchema(anyOf[0]);
107
+ }
108
+
109
+ if (!type) {
110
+ if (properties) {
111
+ type = "object";
112
+ } else if (items) {
113
+ type = "array";
114
+ } else {
115
+ return;
116
+ }
117
+ }
118
+
119
+ if (type === "object") {
120
+ let obj: any = {};
121
+ for (let [name, prop] of Object.entries(properties ?? {})) {
122
+ if (prop.properties) {
123
+ for (const [key, value] of Object.entries(prop.properties)) {
124
+ if (value.readOnly && value.readOnly === true) {
125
+ delete prop.properties[key];
126
+ }
127
+ }
128
+ }
129
+
130
+ if (prop.items && prop.items.properties) {
131
+ for (const [key, value] of Object.entries(prop.items.properties)) {
132
+ if (value.readOnly && value.readOnly === true) {
133
+ delete prop.items.properties[key];
134
+ }
135
+ }
136
+ }
137
+
138
+ if (prop.deprecated) {
139
+ continue;
140
+ }
141
+
142
+ // Resolve schema from prop recursively
143
+ obj = sampleResponseFromProp(name, prop, obj);
144
+ }
145
+ return obj;
146
+ }
147
+
148
+ if (type === "array") {
149
+ if (Array.isArray(items?.anyOf)) {
150
+ return items?.anyOf.map((item) => sampleResponseFromSchema(item));
151
+ }
152
+
153
+ if (Array.isArray(items?.oneOf)) {
154
+ return items?.oneOf.map((item) => sampleResponseFromSchema(item));
155
+ }
156
+
157
+ return [sampleResponseFromSchema(items)];
158
+ }
159
+
160
+ if (schema.enum) {
161
+ if (schema.default) {
162
+ return schema.default;
163
+ }
164
+ return normalizeArray(schema.enum)[0];
165
+ }
166
+
167
+ if (schema.writeOnly && schema.writeOnly === true) {
168
+ return undefined;
169
+ }
170
+
171
+ return primitive(schema);
172
+ } catch (err) {
173
+ console.error(
174
+ chalk.yellow("WARNING: failed to create example from schema object:", err)
175
+ );
176
+ return;
177
+ }
178
+ };
179
+
180
+ function primitive(schema: SchemaObject = {}) {
181
+ let { type, format } = schema;
182
+
183
+ if (type === undefined) {
184
+ return;
185
+ }
186
+
187
+ let fn = schema.default ? () => schema.default : primitives[type].default;
188
+
189
+ if (format !== undefined) {
190
+ fn = primitives[type][format] || fn;
191
+ }
192
+
193
+ if (fn) {
194
+ return fn(schema);
195
+ }
196
+
197
+ return "Unknown Type: " + schema.type;
198
+ }
199
+
200
+ function normalizeArray(arr: any) {
201
+ if (Array.isArray(arr)) {
202
+ return arr;
203
+ }
204
+ return [arr];
205
+ }
@@ -25,7 +25,7 @@ import {
25
25
  SidebarOptions,
26
26
  TagPageMetadata,
27
27
  } from "../types";
28
- import { sampleFromSchema } from "./createExample";
28
+ import { sampleRequestFromSchema } from "./createRequestExample";
29
29
  import { OpenApiObject, TagObject } from "./types";
30
30
  import { loadAndResolveSpec } from "./utils/loadAndResolveSpec";
31
31
 
@@ -170,7 +170,7 @@ function createItems(
170
170
  let jsonRequestBodyExample;
171
171
  const body = operationObject.requestBody?.content?.["application/json"];
172
172
  if (body?.schema) {
173
- jsonRequestBodyExample = sampleFromSchema(body.schema);
173
+ jsonRequestBodyExample = sampleRequestFromSchema(body.schema);
174
174
  }
175
175
 
176
176
  // Handle vendor JSON media types
@@ -180,7 +180,7 @@ function createItems(
180
180
  if (firstBodyContentKey.endsWith("+json")) {
181
181
  const firstBody = bodyContent[firstBodyContentKey];
182
182
  if (firstBody?.schema) {
183
- jsonRequestBodyExample = sampleFromSchema(firstBody.schema);
183
+ jsonRequestBodyExample = sampleRequestFromSchema(firstBody.schema);
184
184
  }
185
185
  }
186
186
  }
@@ -1,2 +0,0 @@
1
- import { SchemaObject } from "./types";
2
- export declare const sampleFromSchema: (schema?: SchemaObject) => any;