@camunda8/docusaurus-plugin-openapi-docs 4.5.1

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 (170) hide show
  1. package/README.md +386 -0
  2. package/lib/index.d.ts +9 -0
  3. package/lib/index.js +714 -0
  4. package/lib/markdown/createArrayBracket.d.ts +2 -0
  5. package/lib/markdown/createArrayBracket.js +36 -0
  6. package/lib/markdown/createAuthentication.d.ts +2 -0
  7. package/lib/markdown/createAuthentication.js +173 -0
  8. package/lib/markdown/createAuthorization.d.ts +1 -0
  9. package/lib/markdown/createAuthorization.js +15 -0
  10. package/lib/markdown/createCallbackMethodEndpoint.d.ts +1 -0
  11. package/lib/markdown/createCallbackMethodEndpoint.js +20 -0
  12. package/lib/markdown/createCallbacks.d.ts +6 -0
  13. package/lib/markdown/createCallbacks.js +77 -0
  14. package/lib/markdown/createContactInfo.d.ts +2 -0
  15. package/lib/markdown/createContactInfo.js +39 -0
  16. package/lib/markdown/createDeprecationNotice.d.ts +6 -0
  17. package/lib/markdown/createDeprecationNotice.js +20 -0
  18. package/lib/markdown/createDescription.d.ts +1 -0
  19. package/lib/markdown/createDescription.js +13 -0
  20. package/lib/markdown/createDetails.d.ts +2 -0
  21. package/lib/markdown/createDetails.js +17 -0
  22. package/lib/markdown/createDetailsSummary.d.ts +2 -0
  23. package/lib/markdown/createDetailsSummary.js +17 -0
  24. package/lib/markdown/createDownload.d.ts +1 -0
  25. package/lib/markdown/createDownload.js +16 -0
  26. package/lib/markdown/createHeading.d.ts +1 -0
  27. package/lib/markdown/createHeading.js +20 -0
  28. package/lib/markdown/createLicense.d.ts +2 -0
  29. package/lib/markdown/createLicense.js +32 -0
  30. package/lib/markdown/createLogo.d.ts +2 -0
  31. package/lib/markdown/createLogo.js +18 -0
  32. package/lib/markdown/createMethodEndpoint.d.ts +1 -0
  33. package/lib/markdown/createMethodEndpoint.js +20 -0
  34. package/lib/markdown/createParamsDetails.d.ts +6 -0
  35. package/lib/markdown/createParamsDetails.js +18 -0
  36. package/lib/markdown/createRequestBodyDetails.d.ts +13 -0
  37. package/lib/markdown/createRequestBodyDetails.js +13 -0
  38. package/lib/markdown/createRequestHeader.d.ts +1 -0
  39. package/lib/markdown/createRequestHeader.js +23 -0
  40. package/lib/markdown/createRequestSchema.d.ts +14 -0
  41. package/lib/markdown/createRequestSchema.js +20 -0
  42. package/lib/markdown/createResponseSchema.d.ts +14 -0
  43. package/lib/markdown/createResponseSchema.js +20 -0
  44. package/lib/markdown/createSchema.d.ts +9 -0
  45. package/lib/markdown/createSchema.js +668 -0
  46. package/lib/markdown/createSchema.test.d.ts +1 -0
  47. package/lib/markdown/createSchema.test.js +913 -0
  48. package/lib/markdown/createStatusCodes.d.ts +9 -0
  49. package/lib/markdown/createStatusCodes.js +63 -0
  50. package/lib/markdown/createTermsOfService.d.ts +1 -0
  51. package/lib/markdown/createTermsOfService.js +31 -0
  52. package/lib/markdown/createVendorExtensions.d.ts +1 -0
  53. package/lib/markdown/createVendorExtensions.js +24 -0
  54. package/lib/markdown/createVersionBadge.d.ts +1 -0
  55. package/lib/markdown/createVersionBadge.js +19 -0
  56. package/lib/markdown/index.d.ts +5 -0
  57. package/lib/markdown/index.js +93 -0
  58. package/lib/markdown/schema.d.ts +3 -0
  59. package/lib/markdown/schema.js +157 -0
  60. package/lib/markdown/schema.test.d.ts +1 -0
  61. package/lib/markdown/schema.test.js +181 -0
  62. package/lib/markdown/utils.d.ts +20 -0
  63. package/lib/markdown/utils.js +68 -0
  64. package/lib/openapi/createRequestExample.d.ts +2 -0
  65. package/lib/openapi/createRequestExample.js +14 -0
  66. package/lib/openapi/createResponseExample.d.ts +2 -0
  67. package/lib/openapi/createResponseExample.js +14 -0
  68. package/lib/openapi/createSchemaExample.d.ts +7 -0
  69. package/lib/openapi/createSchemaExample.js +231 -0
  70. package/lib/openapi/index.d.ts +1 -0
  71. package/lib/openapi/index.js +12 -0
  72. package/lib/openapi/openapi.d.ts +12 -0
  73. package/lib/openapi/openapi.js +634 -0
  74. package/lib/openapi/openapi.test.d.ts +1 -0
  75. package/lib/openapi/openapi.test.js +33 -0
  76. package/lib/openapi/sdkExamples.d.ts +39 -0
  77. package/lib/openapi/sdkExamples.js +141 -0
  78. package/lib/openapi/types.d.ts +352 -0
  79. package/lib/openapi/types.js +8 -0
  80. package/lib/openapi/utils/loadAndResolveSpec.d.ts +2 -0
  81. package/lib/openapi/utils/loadAndResolveSpec.js +153 -0
  82. package/lib/openapi/utils/services/OpenAPIParser.d.ts +52 -0
  83. package/lib/openapi/utils/services/OpenAPIParser.js +343 -0
  84. package/lib/openapi/utils/services/RedocNormalizedOptions.d.ts +100 -0
  85. package/lib/openapi/utils/services/RedocNormalizedOptions.js +170 -0
  86. package/lib/openapi/utils/types/index.d.ts +2 -0
  87. package/lib/openapi/utils/types/index.js +23 -0
  88. package/lib/openapi/utils/types/open-api.d.ts +305 -0
  89. package/lib/openapi/utils/types/open-api.js +8 -0
  90. package/lib/openapi/utils/types.d.ts +307 -0
  91. package/lib/openapi/utils/types.js +8 -0
  92. package/lib/openapi/utils/utils/JsonPointer.d.ts +51 -0
  93. package/lib/openapi/utils/utils/JsonPointer.js +95 -0
  94. package/lib/openapi/utils/utils/helpers.d.ts +43 -0
  95. package/lib/openapi/utils/utils/helpers.js +230 -0
  96. package/lib/openapi/utils/utils/index.d.ts +3 -0
  97. package/lib/openapi/utils/utils/index.js +25 -0
  98. package/lib/openapi/utils/utils/openapi.d.ts +40 -0
  99. package/lib/openapi/utils/utils/openapi.js +605 -0
  100. package/lib/openapi/webhooks.test.d.ts +1 -0
  101. package/lib/openapi/webhooks.test.js +23 -0
  102. package/lib/options.d.ts +2 -0
  103. package/lib/options.js +70 -0
  104. package/lib/sidebars/index.d.ts +4 -0
  105. package/lib/sidebars/index.js +226 -0
  106. package/lib/sidebars/utils.d.ts +2 -0
  107. package/lib/sidebars/utils.js +30 -0
  108. package/lib/types.d.ts +138 -0
  109. package/lib/types.js +8 -0
  110. package/package.json +68 -0
  111. package/src/index.ts +955 -0
  112. package/src/markdown/__snapshots__/createSchema.test.ts.snap +1605 -0
  113. package/src/markdown/createArrayBracket.ts +35 -0
  114. package/src/markdown/createAuthentication.ts +209 -0
  115. package/src/markdown/createAuthorization.ts +13 -0
  116. package/src/markdown/createCallbackMethodEndpoint.ts +19 -0
  117. package/src/markdown/createCallbacks.ts +101 -0
  118. package/src/markdown/createContactInfo.ts +41 -0
  119. package/src/markdown/createDeprecationNotice.ts +31 -0
  120. package/src/markdown/createDescription.ts +12 -0
  121. package/src/markdown/createDetails.ts +16 -0
  122. package/src/markdown/createDetailsSummary.ts +16 -0
  123. package/src/markdown/createDownload.ts +15 -0
  124. package/src/markdown/createHeading.ts +23 -0
  125. package/src/markdown/createLicense.ts +34 -0
  126. package/src/markdown/createLogo.ts +21 -0
  127. package/src/markdown/createMethodEndpoint.ts +19 -0
  128. package/src/markdown/createParamsDetails.ts +22 -0
  129. package/src/markdown/createRequestBodyDetails.ts +24 -0
  130. package/src/markdown/createRequestHeader.ts +22 -0
  131. package/src/markdown/createRequestSchema.ts +32 -0
  132. package/src/markdown/createResponseSchema.ts +32 -0
  133. package/src/markdown/createSchema.test.ts +1075 -0
  134. package/src/markdown/createSchema.ts +864 -0
  135. package/src/markdown/createStatusCodes.ts +63 -0
  136. package/src/markdown/createTermsOfService.ts +30 -0
  137. package/src/markdown/createVendorExtensions.ts +22 -0
  138. package/src/markdown/createVersionBadge.ts +22 -0
  139. package/src/markdown/index.ts +145 -0
  140. package/src/markdown/schema.test.ts +208 -0
  141. package/src/markdown/schema.ts +188 -0
  142. package/src/markdown/utils.ts +89 -0
  143. package/src/openapi/__fixtures__/examples/openapi.yaml +49 -0
  144. package/src/openapi/__fixtures__/webhook/openapi.yaml +17 -0
  145. package/src/openapi/createRequestExample.ts +13 -0
  146. package/src/openapi/createResponseExample.ts +13 -0
  147. package/src/openapi/createSchemaExample.ts +292 -0
  148. package/src/openapi/index.ts +8 -0
  149. package/src/openapi/openapi.test.ts +40 -0
  150. package/src/openapi/openapi.ts +772 -0
  151. package/src/openapi/sdkExamples.ts +195 -0
  152. package/src/openapi/types.ts +458 -0
  153. package/src/openapi/utils/loadAndResolveSpec.ts +171 -0
  154. package/src/openapi/utils/services/OpenAPIParser.ts +434 -0
  155. package/src/openapi/utils/services/RedocNormalizedOptions.ts +330 -0
  156. package/src/openapi/utils/types/index.ts +10 -0
  157. package/src/openapi/utils/types/open-api.ts +303 -0
  158. package/src/openapi/utils/types.ts +304 -0
  159. package/src/openapi/utils/utils/JsonPointer.ts +99 -0
  160. package/src/openapi/utils/utils/helpers.ts +239 -0
  161. package/src/openapi/utils/utils/index.ts +11 -0
  162. package/src/openapi/utils/utils/openapi.ts +771 -0
  163. package/src/openapi/webhooks.test.ts +30 -0
  164. package/src/openapi-to-postmanv2.d.ts +10 -0
  165. package/src/options.ts +78 -0
  166. package/src/plugin-openapi.d.ts +88 -0
  167. package/src/sidebars/index.ts +322 -0
  168. package/src/sidebars/utils.ts +29 -0
  169. package/src/types.ts +180 -0
  170. 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,17 @@
1
+ openapi: 3.0.3
2
+ info:
3
+ title: Webhook Example
4
+ version: 1.0.0
5
+ paths: {}
6
+ webhooks:
7
+ order.created:
8
+ post:
9
+ requestBody:
10
+ description: example body
11
+ content:
12
+ application/json:
13
+ schema:
14
+ type: object
15
+ responses:
16
+ "200":
17
+ description: OK
@@ -0,0 +1,13 @@
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 { sampleFromSchema } from "./createSchemaExample";
9
+ import { SchemaObject } from "./types";
10
+
11
+ export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
12
+ return sampleFromSchema(schema, { type: "request" });
13
+ };
@@ -0,0 +1,13 @@
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 { sampleFromSchema } from "./createSchemaExample";
9
+ import { SchemaObject } from "./types";
10
+
11
+ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
12
+ return sampleFromSchema(schema, { type: "response" });
13
+ };
@@ -0,0 +1,292 @@
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
+ 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
+ type ExampleType = "request" | "response";
60
+
61
+ interface ExampleContext {
62
+ type: ExampleType;
63
+ }
64
+
65
+ function shouldExcludeProperty(
66
+ prop: SchemaObject,
67
+ context: ExampleContext
68
+ ): boolean {
69
+ if (prop.deprecated) {
70
+ return true;
71
+ }
72
+
73
+ if (context.type === "request") {
74
+ return prop.readOnly === true;
75
+ } else {
76
+ return prop.writeOnly === true;
77
+ }
78
+ }
79
+
80
+ function sampleFromProp(
81
+ name: string,
82
+ prop: any,
83
+ obj: any,
84
+ context: ExampleContext
85
+ ): any {
86
+ // Handle resolved circular props
87
+ if (typeof prop === "object" && Object.keys(prop).length === 0) {
88
+ obj[name] = prop;
89
+ return obj;
90
+ }
91
+
92
+ // TODO: handle discriminators
93
+
94
+ if (prop.oneOf) {
95
+ obj[name] = sampleFromSchema(prop.oneOf[0], context);
96
+ } else if (prop.anyOf) {
97
+ obj[name] = sampleFromSchema(prop.anyOf[0], context);
98
+ } else if (prop.allOf) {
99
+ const mergedSchemas = mergeAllOf(prop) as SchemaObject;
100
+ sampleFromProp(name, mergedSchemas, obj, context);
101
+ } else {
102
+ obj[name] = sampleFromSchema(prop, context);
103
+ }
104
+ return obj;
105
+ }
106
+
107
+ export const sampleFromSchema = (
108
+ schema: SchemaObject = {},
109
+ context: ExampleContext
110
+ ): any => {
111
+ try {
112
+ // deep copy schema before processing
113
+ let schemaCopy = JSON.parse(JSON.stringify(schema));
114
+ let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
115
+
116
+ if (example !== undefined) {
117
+ return example;
118
+ }
119
+
120
+ if (oneOf) {
121
+ if (properties) {
122
+ const combinedSchemas = merge(schemaCopy, oneOf[0]);
123
+ delete combinedSchemas.oneOf;
124
+ return sampleFromSchema(combinedSchemas, context);
125
+ }
126
+ // Just go with first schema
127
+ return sampleFromSchema(oneOf[0], context);
128
+ }
129
+
130
+ if (anyOf) {
131
+ if (properties) {
132
+ const combinedSchemas = merge(schemaCopy, anyOf[0]);
133
+ delete combinedSchemas.anyOf;
134
+ return sampleFromSchema(combinedSchemas, context);
135
+ }
136
+ // Just go with first schema
137
+ return sampleFromSchema(anyOf[0], context);
138
+ }
139
+
140
+ if (allOf) {
141
+ const mergedSchemas = mergeAllOf(schemaCopy) as SchemaObject;
142
+ if (mergedSchemas.properties) {
143
+ for (const [key, value] of Object.entries(mergedSchemas.properties)) {
144
+ if (shouldExcludeProperty(value, context)) {
145
+ delete mergedSchemas.properties[key];
146
+ }
147
+ }
148
+ }
149
+ if (properties) {
150
+ const combinedSchemas = merge(schemaCopy, mergedSchemas);
151
+ delete combinedSchemas.allOf;
152
+ return sampleFromSchema(combinedSchemas, context);
153
+ }
154
+ return sampleFromSchema(mergedSchemas, context);
155
+ }
156
+
157
+ if (!type) {
158
+ if (properties) {
159
+ type = "object";
160
+ } else if (items) {
161
+ type = "array";
162
+ } else {
163
+ return;
164
+ }
165
+ }
166
+
167
+ if (type === "object") {
168
+ let obj: any = {};
169
+ for (let [name, prop] of Object.entries(properties ?? {}) as any) {
170
+ if (prop.properties) {
171
+ for (const [key, value] of Object.entries(prop.properties) as any) {
172
+ if (shouldExcludeProperty(value, context)) {
173
+ delete prop.properties[key];
174
+ }
175
+ }
176
+ }
177
+
178
+ if (prop.items && prop.items.properties) {
179
+ for (const [key, value] of Object.entries(
180
+ prop.items.properties
181
+ ) as any) {
182
+ if (shouldExcludeProperty(value, context)) {
183
+ delete prop.items.properties[key];
184
+ }
185
+ }
186
+ }
187
+
188
+ if (shouldExcludeProperty(prop, context)) {
189
+ continue;
190
+ }
191
+
192
+ // Resolve schema from prop recursively
193
+ obj = sampleFromProp(name, prop, obj, context);
194
+ }
195
+ return obj;
196
+ }
197
+
198
+ if (type === "array") {
199
+ if (Array.isArray(items?.anyOf)) {
200
+ return processArrayItems(items, "anyOf", context);
201
+ }
202
+
203
+ if (Array.isArray(items?.oneOf)) {
204
+ return processArrayItems(items, "oneOf", context);
205
+ }
206
+
207
+ return normalizeArray(sampleFromSchema(items, context));
208
+ }
209
+
210
+ if (schemaCopy.enum) {
211
+ if (schemaCopy.default) {
212
+ return schemaCopy.default;
213
+ }
214
+ return normalizeArray(schemaCopy.enum)[0];
215
+ }
216
+
217
+ if (shouldExcludeProperty(schemaCopy, context)) {
218
+ return undefined;
219
+ }
220
+
221
+ return primitive(schemaCopy);
222
+ } catch (err) {
223
+ console.error(
224
+ chalk.yellow("WARNING: failed to create example from schema object:", err)
225
+ );
226
+ return;
227
+ }
228
+ };
229
+
230
+ function primitive(schema: SchemaObject = {}) {
231
+ let { type, format } = schema;
232
+
233
+ if (type === undefined) {
234
+ return;
235
+ }
236
+
237
+ // If type is an array, use the first type
238
+ if (Array.isArray(type)) {
239
+ type = type[0];
240
+ if (type === undefined) {
241
+ return;
242
+ }
243
+ }
244
+
245
+ // Use schema default if available, otherwise use type default
246
+ if (schema.default !== undefined) {
247
+ return schema.default;
248
+ }
249
+
250
+ const typeConfig = primitives[type];
251
+ if (typeConfig) {
252
+ if (format !== undefined && typeConfig[format] !== undefined) {
253
+ return typeConfig[format](schema);
254
+ }
255
+ if (typeConfig.default !== undefined) {
256
+ return typeConfig.default(schema);
257
+ }
258
+ }
259
+
260
+ return "Unknown Type: " + schema.type;
261
+ }
262
+
263
+ function normalizeArray(arr: any) {
264
+ if (Array.isArray(arr)) {
265
+ return arr;
266
+ }
267
+ return [arr];
268
+ }
269
+
270
+ function processArrayItems(
271
+ items: SchemaObject,
272
+ schemaType: "anyOf" | "oneOf",
273
+ context: ExampleContext
274
+ ): any[] {
275
+ const itemsArray = items[schemaType] as SchemaObject[];
276
+ return itemsArray.map((item: SchemaObject) => {
277
+ // If items has properties, merge them with each item
278
+ if (items.properties) {
279
+ const combinedSchema = {
280
+ ...item,
281
+ properties: {
282
+ ...items.properties, // Common properties from parent
283
+ ...item.properties, // Specific properties from this anyOf/oneOf item
284
+ },
285
+ };
286
+ // Remove anyOf/oneOf to prevent infinite recursion when calling sampleFromSchema
287
+ delete combinedSchema[schemaType];
288
+ return sampleFromSchema(combinedSchema, context);
289
+ }
290
+ return sampleFromSchema(item, context);
291
+ });
292
+ }
@@ -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
+ });