docusaurus-plugin-openapi-docs 0.0.0-1000

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.
Files changed (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +361 -0
  3. package/lib/index.d.ts +9 -0
  4. package/lib/index.js +709 -0
  5. package/lib/markdown/createArrayBracket.d.ts +2 -0
  6. package/lib/markdown/createArrayBracket.js +36 -0
  7. package/lib/markdown/createAuthentication.d.ts +2 -0
  8. package/lib/markdown/createAuthentication.js +171 -0
  9. package/lib/markdown/createAuthorization.d.ts +1 -0
  10. package/lib/markdown/createAuthorization.js +15 -0
  11. package/lib/markdown/createCallbackMethodEndpoint.d.ts +1 -0
  12. package/lib/markdown/createCallbackMethodEndpoint.js +20 -0
  13. package/lib/markdown/createCallbacks.d.ts +6 -0
  14. package/lib/markdown/createCallbacks.js +77 -0
  15. package/lib/markdown/createContactInfo.d.ts +2 -0
  16. package/lib/markdown/createContactInfo.js +39 -0
  17. package/lib/markdown/createDeprecationNotice.d.ts +6 -0
  18. package/lib/markdown/createDeprecationNotice.js +20 -0
  19. package/lib/markdown/createDescription.d.ts +1 -0
  20. package/lib/markdown/createDescription.js +13 -0
  21. package/lib/markdown/createDetails.d.ts +2 -0
  22. package/lib/markdown/createDetails.js +17 -0
  23. package/lib/markdown/createDetailsSummary.d.ts +2 -0
  24. package/lib/markdown/createDetailsSummary.js +17 -0
  25. package/lib/markdown/createDownload.d.ts +1 -0
  26. package/lib/markdown/createDownload.js +16 -0
  27. package/lib/markdown/createHeading.d.ts +1 -0
  28. package/lib/markdown/createHeading.js +20 -0
  29. package/lib/markdown/createLicense.d.ts +2 -0
  30. package/lib/markdown/createLicense.js +32 -0
  31. package/lib/markdown/createLogo.d.ts +2 -0
  32. package/lib/markdown/createLogo.js +18 -0
  33. package/lib/markdown/createMethodEndpoint.d.ts +1 -0
  34. package/lib/markdown/createMethodEndpoint.js +20 -0
  35. package/lib/markdown/createParamsDetails.d.ts +6 -0
  36. package/lib/markdown/createParamsDetails.js +18 -0
  37. package/lib/markdown/createRequestBodyDetails.d.ts +13 -0
  38. package/lib/markdown/createRequestBodyDetails.js +13 -0
  39. package/lib/markdown/createRequestHeader.d.ts +1 -0
  40. package/lib/markdown/createRequestHeader.js +21 -0
  41. package/lib/markdown/createRequestSchema.d.ts +14 -0
  42. package/lib/markdown/createRequestSchema.js +20 -0
  43. package/lib/markdown/createResponseSchema.d.ts +14 -0
  44. package/lib/markdown/createResponseSchema.js +20 -0
  45. package/lib/markdown/createSchema.d.ts +9 -0
  46. package/lib/markdown/createSchema.js +668 -0
  47. package/lib/markdown/createSchema.test.d.ts +1 -0
  48. package/lib/markdown/createSchema.test.js +913 -0
  49. package/lib/markdown/createStatusCodes.d.ts +9 -0
  50. package/lib/markdown/createStatusCodes.js +63 -0
  51. package/lib/markdown/createTermsOfService.d.ts +1 -0
  52. package/lib/markdown/createTermsOfService.js +31 -0
  53. package/lib/markdown/createVendorExtensions.d.ts +1 -0
  54. package/lib/markdown/createVendorExtensions.js +24 -0
  55. package/lib/markdown/createVersionBadge.d.ts +1 -0
  56. package/lib/markdown/createVersionBadge.js +19 -0
  57. package/lib/markdown/index.d.ts +5 -0
  58. package/lib/markdown/index.js +92 -0
  59. package/lib/markdown/schema.d.ts +3 -0
  60. package/lib/markdown/schema.js +154 -0
  61. package/lib/markdown/schema.test.d.ts +1 -0
  62. package/lib/markdown/schema.test.js +181 -0
  63. package/lib/markdown/utils.d.ts +20 -0
  64. package/lib/markdown/utils.js +68 -0
  65. package/lib/openapi/createRequestExample.d.ts +2 -0
  66. package/lib/openapi/createRequestExample.js +191 -0
  67. package/lib/openapi/createResponseExample.d.ts +2 -0
  68. package/lib/openapi/createResponseExample.js +192 -0
  69. package/lib/openapi/index.d.ts +1 -0
  70. package/lib/openapi/index.js +12 -0
  71. package/lib/openapi/openapi.d.ts +12 -0
  72. package/lib/openapi/openapi.js +544 -0
  73. package/lib/openapi/openapi.test.d.ts +1 -0
  74. package/lib/openapi/openapi.test.js +33 -0
  75. package/lib/openapi/types.d.ts +354 -0
  76. package/lib/openapi/types.js +8 -0
  77. package/lib/openapi/utils/loadAndResolveSpec.d.ts +2 -0
  78. package/lib/openapi/utils/loadAndResolveSpec.js +153 -0
  79. package/lib/openapi/utils/services/OpenAPIParser.d.ts +52 -0
  80. package/lib/openapi/utils/services/OpenAPIParser.js +343 -0
  81. package/lib/openapi/utils/services/RedocNormalizedOptions.d.ts +100 -0
  82. package/lib/openapi/utils/services/RedocNormalizedOptions.js +170 -0
  83. package/lib/openapi/utils/types/index.d.ts +2 -0
  84. package/lib/openapi/utils/types/index.js +23 -0
  85. package/lib/openapi/utils/types/open-api.d.ts +305 -0
  86. package/lib/openapi/utils/types/open-api.js +8 -0
  87. package/lib/openapi/utils/types.d.ts +306 -0
  88. package/lib/openapi/utils/types.js +8 -0
  89. package/lib/openapi/utils/utils/JsonPointer.d.ts +51 -0
  90. package/lib/openapi/utils/utils/JsonPointer.js +95 -0
  91. package/lib/openapi/utils/utils/helpers.d.ts +43 -0
  92. package/lib/openapi/utils/utils/helpers.js +230 -0
  93. package/lib/openapi/utils/utils/index.d.ts +3 -0
  94. package/lib/openapi/utils/utils/index.js +25 -0
  95. package/lib/openapi/utils/utils/openapi.d.ts +40 -0
  96. package/lib/openapi/utils/utils/openapi.js +605 -0
  97. package/lib/options.d.ts +2 -0
  98. package/lib/options.js +69 -0
  99. package/lib/sidebars/index.d.ts +4 -0
  100. package/lib/sidebars/index.js +226 -0
  101. package/lib/sidebars/utils.d.ts +2 -0
  102. package/lib/sidebars/utils.js +30 -0
  103. package/lib/types.d.ts +135 -0
  104. package/lib/types.js +8 -0
  105. package/package.json +69 -0
  106. package/src/index.ts +945 -0
  107. package/src/markdown/__snapshots__/createSchema.test.ts.snap +1605 -0
  108. package/src/markdown/createArrayBracket.ts +35 -0
  109. package/src/markdown/createAuthentication.ts +201 -0
  110. package/src/markdown/createAuthorization.ts +13 -0
  111. package/src/markdown/createCallbackMethodEndpoint.ts +19 -0
  112. package/src/markdown/createCallbacks.ts +101 -0
  113. package/src/markdown/createContactInfo.ts +41 -0
  114. package/src/markdown/createDeprecationNotice.ts +31 -0
  115. package/src/markdown/createDescription.ts +12 -0
  116. package/src/markdown/createDetails.ts +16 -0
  117. package/src/markdown/createDetailsSummary.ts +16 -0
  118. package/src/markdown/createDownload.ts +15 -0
  119. package/src/markdown/createHeading.ts +23 -0
  120. package/src/markdown/createLicense.ts +34 -0
  121. package/src/markdown/createLogo.ts +21 -0
  122. package/src/markdown/createMethodEndpoint.ts +19 -0
  123. package/src/markdown/createParamsDetails.ts +22 -0
  124. package/src/markdown/createRequestBodyDetails.ts +24 -0
  125. package/src/markdown/createRequestHeader.ts +24 -0
  126. package/src/markdown/createRequestSchema.ts +32 -0
  127. package/src/markdown/createResponseSchema.ts +32 -0
  128. package/src/markdown/createSchema.test.ts +1075 -0
  129. package/src/markdown/createSchema.ts +862 -0
  130. package/src/markdown/createStatusCodes.ts +63 -0
  131. package/src/markdown/createTermsOfService.ts +30 -0
  132. package/src/markdown/createVendorExtensions.ts +22 -0
  133. package/src/markdown/createVersionBadge.ts +22 -0
  134. package/src/markdown/index.ts +144 -0
  135. package/src/markdown/schema.test.ts +208 -0
  136. package/src/markdown/schema.ts +185 -0
  137. package/src/markdown/utils.ts +89 -0
  138. package/src/openapi/__fixtures__/examples/openapi.yaml +49 -0
  139. package/src/openapi/createRequestExample.ts +235 -0
  140. package/src/openapi/createResponseExample.ts +238 -0
  141. package/src/openapi/index.ts +8 -0
  142. package/src/openapi/openapi.test.ts +40 -0
  143. package/src/openapi/openapi.ts +697 -0
  144. package/src/openapi/types.ts +455 -0
  145. package/src/openapi/utils/loadAndResolveSpec.ts +171 -0
  146. package/src/openapi/utils/services/OpenAPIParser.ts +434 -0
  147. package/src/openapi/utils/services/RedocNormalizedOptions.ts +330 -0
  148. package/src/openapi/utils/types/index.ts +10 -0
  149. package/src/openapi/utils/types/open-api.ts +303 -0
  150. package/src/openapi/utils/types.ts +303 -0
  151. package/src/openapi/utils/utils/JsonPointer.ts +99 -0
  152. package/src/openapi/utils/utils/helpers.ts +239 -0
  153. package/src/openapi/utils/utils/index.ts +11 -0
  154. package/src/openapi/utils/utils/openapi.ts +771 -0
  155. package/src/openapi-to-postmanv2.d.ts +10 -0
  156. package/src/options.ts +77 -0
  157. package/src/plugin-content-docs-types.d.ts +42 -0
  158. package/src/plugin-openapi.d.ts +87 -0
  159. package/src/postman-collection.d.ts +10 -0
  160. package/src/sidebars/index.ts +322 -0
  161. package/src/sidebars/utils.ts +29 -0
  162. package/src/types.ts +176 -0
  163. package/tsconfig.json +7 -0
@@ -0,0 +1,89 @@
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
+ /**
9
+ * Children in the plugin does not accept DOM elements, when compared with Children in the theme.
10
+ * It is designed for rendering HTML a strings.
11
+ */
12
+ export type Children = string | undefined | (string | string[] | undefined)[];
13
+
14
+ export type Props = Record<string, any> & { children?: Children };
15
+
16
+ export type Options = { inline?: boolean };
17
+
18
+ export function create(
19
+ tag: string,
20
+ props: Props,
21
+ options: Options = {}
22
+ ): string {
23
+ const { children, ...rest } = props;
24
+
25
+ let propString = "";
26
+ for (const [key, value] of Object.entries(rest)) {
27
+ propString += `\n ${key}={${JSON.stringify(value)}}`;
28
+ }
29
+ let indentedChildren = render(children).replace(/^/gm, " ");
30
+
31
+ if (options.inline) {
32
+ propString += `\n children={${JSON.stringify(children)}}`;
33
+ indentedChildren = "";
34
+ }
35
+
36
+ propString += propString ? "\n" : "";
37
+ indentedChildren += indentedChildren ? "\n" : "";
38
+ return `<${tag}${propString}>\n${indentedChildren}</${tag}>`;
39
+ }
40
+
41
+ export function guard<T>(
42
+ value: T | undefined,
43
+ cb: (value: T) => Children
44
+ ): string {
45
+ if (!!value || value === 0) {
46
+ const children = cb(value);
47
+ return render(children);
48
+ }
49
+ return "";
50
+ }
51
+
52
+ export function render(children: Children): string {
53
+ if (Array.isArray(children)) {
54
+ const filteredChildren = children.filter((c) => c !== undefined);
55
+ return filteredChildren
56
+ .map((i: any) => (Array.isArray(i) ? i.join("") : i))
57
+ .join("");
58
+ }
59
+ return children ?? "";
60
+ }
61
+
62
+ // Regex to selectively URL-encode '>' and '<' chars
63
+ export const lessThan =
64
+ /<=?(?!(=|button|\s?\/button|code|\s?\/code|details|\s?\/details|summary|\s?\/summary|hr|\s?\/hr|br|\s?\/br|span|\s?\/span|strong|\s?\/strong|small|\s?\/small|table|\s?\/table|thead|\s?\/thead|tbody|\s?\/tbody|td|\s?\/td|tr|\s?\/tr|th|\s?\/th|h1|\s?\/h1|h2|\s?\/h2|h3|\s?\/h3|h4|\s?\/h4|h5|\s?\/h5|h6|\s?\/h6|title|\s?\/title|p|\s?\/p|em|\s?\/em|b|\s?\/b|i|\s?\/i|u|\s?\/u|strike|\s?\/strike|bold|\s?\/bold|a|\s?\/a|table|\s?\/table|li|\s?\/li|ol|\s?\/ol|ul|\s?\/ul|img|\s?\/img|svg|\s?\/svg|div|\s?\/div|center|\s?\/center))/gu;
65
+ export const greaterThan =
66
+ /(?<!(button|code|details|summary|hr|br|span|strong|small|table|thead|tbody|td|tr|th|h1|h2|h3|h4|h5|h6|title|p|em|b|i|u|strike|bold|a|li|ol|ul|img|svg|div|center|\/|\s|"|'))>/gu;
67
+ export const codeFence = /`{1,3}[\s\S]*?`{1,3}/g;
68
+ export const curlyBrackets = /([{}])/g;
69
+ export const codeBlock = /(^```.*[\s\S]*?```$|`[^`].+?`)/gm;
70
+
71
+ export function clean(value: string | undefined): string {
72
+ if (!value) {
73
+ return "";
74
+ }
75
+
76
+ let sections = value.split(codeBlock);
77
+ for (let sectionIndex in sections) {
78
+ if (!sections[sectionIndex].startsWith("`")) {
79
+ sections[sectionIndex] = sections[sectionIndex]
80
+ .replace(lessThan, "&lt;")
81
+ .replace(greaterThan, "&gt;")
82
+ .replace(codeFence, function (match) {
83
+ return match.replace(/\\>/g, ">");
84
+ })
85
+ .replace(curlyBrackets, "\\$1");
86
+ }
87
+ }
88
+ return sections.join("");
89
+ }
@@ -0,0 +1,49 @@
1
+ openapi: 3.0.3
2
+ info:
3
+ title: YAML Example
4
+ version: 1.0.0
5
+ description: Sample description.
6
+ paths:
7
+ /hello:
8
+ get:
9
+ summary: Hello World
10
+ description: Example OpenApi definition with YAML.
11
+ responses:
12
+ 200:
13
+ description: OK
14
+
15
+ tags:
16
+ - name: tag1
17
+ description: Everything about your Pets
18
+ x-displayName: Tag 1
19
+ - name: tag2
20
+ description: Tag 2 description
21
+ x-displayName: Tag 2
22
+ - name: tag3
23
+ description: Tag 3 description
24
+ x-displayName: Tag 3
25
+ - name: tag4
26
+ description: Tag 4 description
27
+ x-displayName: Tag 4
28
+
29
+ x-tagGroups:
30
+ - name: Tag 1 & 2
31
+ tags:
32
+ - tag1
33
+ - tag2
34
+ - name: Trinity
35
+ tags:
36
+ - tag1
37
+ - tag2
38
+ - tag3
39
+ - name: Last Two
40
+ tags:
41
+ - tag3
42
+ - tag4
43
+
44
+ components:
45
+ schemas:
46
+ HelloString:
47
+ x-tags:
48
+ - tag1
49
+ type: string
@@ -0,0 +1,235 @@
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
+ import merge from "lodash/merge";
10
+
11
+ 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
+
77
+ 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
+ }
208
+ };
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
+ }
@@ -0,0 +1,238 @@
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
+ import merge from "lodash/merge";
10
+
11
+ 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
+
77
+ 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
+ }
211
+ };
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
+ }
@@ -0,0 +1,8 @@
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
+ export { readOpenapiFiles, processOpenapiFiles } from "./openapi";
@@ -0,0 +1,40 @@
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 path from "path";
9
+
10
+ // eslint-disable-next-line import/no-extraneous-dependencies
11
+ import { posixPath } from "@docusaurus/utils";
12
+
13
+ import { readOpenapiFiles } from ".";
14
+
15
+ // npx jest packages/docusaurus-plugin-openapi/src/openapi/openapi.test.ts --watch
16
+
17
+ describe("openapi", () => {
18
+ describe("readOpenapiFiles", () => {
19
+ it("readOpenapiFiles", async () => {
20
+ const results = await readOpenapiFiles(
21
+ posixPath(path.join(__dirname, "__fixtures__/examples"))
22
+ );
23
+ const categoryMeta = results.find((x) =>
24
+ x.source.endsWith("_category_.json")
25
+ );
26
+ expect(categoryMeta).toBeFalsy();
27
+ // console.log(results);
28
+ const yaml = results.find((x) => x.source.endsWith("openapi.yaml"));
29
+ expect(yaml).toBeTruthy();
30
+ expect(yaml?.sourceDirName).toBe(".");
31
+
32
+ expect(yaml?.data.tags).toBeDefined();
33
+ expect(yaml?.data["x-tagGroups"]).toBeDefined();
34
+
35
+ expect(
36
+ yaml?.data.components?.schemas?.HelloString["x-tags"]
37
+ ).toBeDefined();
38
+ });
39
+ });
40
+ });