@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.
- package/README.md +386 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +714 -0
- package/lib/markdown/createArrayBracket.d.ts +2 -0
- package/lib/markdown/createArrayBracket.js +36 -0
- package/lib/markdown/createAuthentication.d.ts +2 -0
- package/lib/markdown/createAuthentication.js +173 -0
- package/lib/markdown/createAuthorization.d.ts +1 -0
- package/lib/markdown/createAuthorization.js +15 -0
- package/lib/markdown/createCallbackMethodEndpoint.d.ts +1 -0
- package/lib/markdown/createCallbackMethodEndpoint.js +20 -0
- package/lib/markdown/createCallbacks.d.ts +6 -0
- package/lib/markdown/createCallbacks.js +77 -0
- package/lib/markdown/createContactInfo.d.ts +2 -0
- package/lib/markdown/createContactInfo.js +39 -0
- package/lib/markdown/createDeprecationNotice.d.ts +6 -0
- package/lib/markdown/createDeprecationNotice.js +20 -0
- package/lib/markdown/createDescription.d.ts +1 -0
- package/lib/markdown/createDescription.js +13 -0
- package/lib/markdown/createDetails.d.ts +2 -0
- package/lib/markdown/createDetails.js +17 -0
- package/lib/markdown/createDetailsSummary.d.ts +2 -0
- package/lib/markdown/createDetailsSummary.js +17 -0
- package/lib/markdown/createDownload.d.ts +1 -0
- package/lib/markdown/createDownload.js +16 -0
- package/lib/markdown/createHeading.d.ts +1 -0
- package/lib/markdown/createHeading.js +20 -0
- package/lib/markdown/createLicense.d.ts +2 -0
- package/lib/markdown/createLicense.js +32 -0
- package/lib/markdown/createLogo.d.ts +2 -0
- package/lib/markdown/createLogo.js +18 -0
- package/lib/markdown/createMethodEndpoint.d.ts +1 -0
- package/lib/markdown/createMethodEndpoint.js +20 -0
- package/lib/markdown/createParamsDetails.d.ts +6 -0
- package/lib/markdown/createParamsDetails.js +18 -0
- package/lib/markdown/createRequestBodyDetails.d.ts +13 -0
- package/lib/markdown/createRequestBodyDetails.js +13 -0
- package/lib/markdown/createRequestHeader.d.ts +1 -0
- package/lib/markdown/createRequestHeader.js +23 -0
- package/lib/markdown/createRequestSchema.d.ts +14 -0
- package/lib/markdown/createRequestSchema.js +20 -0
- package/lib/markdown/createResponseSchema.d.ts +14 -0
- package/lib/markdown/createResponseSchema.js +20 -0
- package/lib/markdown/createSchema.d.ts +9 -0
- package/lib/markdown/createSchema.js +668 -0
- package/lib/markdown/createSchema.test.d.ts +1 -0
- package/lib/markdown/createSchema.test.js +913 -0
- package/lib/markdown/createStatusCodes.d.ts +9 -0
- package/lib/markdown/createStatusCodes.js +63 -0
- package/lib/markdown/createTermsOfService.d.ts +1 -0
- package/lib/markdown/createTermsOfService.js +31 -0
- package/lib/markdown/createVendorExtensions.d.ts +1 -0
- package/lib/markdown/createVendorExtensions.js +24 -0
- package/lib/markdown/createVersionBadge.d.ts +1 -0
- package/lib/markdown/createVersionBadge.js +19 -0
- package/lib/markdown/index.d.ts +5 -0
- package/lib/markdown/index.js +93 -0
- package/lib/markdown/schema.d.ts +3 -0
- package/lib/markdown/schema.js +157 -0
- package/lib/markdown/schema.test.d.ts +1 -0
- package/lib/markdown/schema.test.js +181 -0
- package/lib/markdown/utils.d.ts +20 -0
- package/lib/markdown/utils.js +68 -0
- package/lib/openapi/createRequestExample.d.ts +2 -0
- package/lib/openapi/createRequestExample.js +14 -0
- package/lib/openapi/createResponseExample.d.ts +2 -0
- package/lib/openapi/createResponseExample.js +14 -0
- package/lib/openapi/createSchemaExample.d.ts +7 -0
- package/lib/openapi/createSchemaExample.js +231 -0
- package/lib/openapi/index.d.ts +1 -0
- package/lib/openapi/index.js +12 -0
- package/lib/openapi/openapi.d.ts +12 -0
- package/lib/openapi/openapi.js +634 -0
- package/lib/openapi/openapi.test.d.ts +1 -0
- package/lib/openapi/openapi.test.js +33 -0
- package/lib/openapi/sdkExamples.d.ts +39 -0
- package/lib/openapi/sdkExamples.js +141 -0
- package/lib/openapi/types.d.ts +352 -0
- package/lib/openapi/types.js +8 -0
- package/lib/openapi/utils/loadAndResolveSpec.d.ts +2 -0
- package/lib/openapi/utils/loadAndResolveSpec.js +153 -0
- package/lib/openapi/utils/services/OpenAPIParser.d.ts +52 -0
- package/lib/openapi/utils/services/OpenAPIParser.js +343 -0
- package/lib/openapi/utils/services/RedocNormalizedOptions.d.ts +100 -0
- package/lib/openapi/utils/services/RedocNormalizedOptions.js +170 -0
- package/lib/openapi/utils/types/index.d.ts +2 -0
- package/lib/openapi/utils/types/index.js +23 -0
- package/lib/openapi/utils/types/open-api.d.ts +305 -0
- package/lib/openapi/utils/types/open-api.js +8 -0
- package/lib/openapi/utils/types.d.ts +307 -0
- package/lib/openapi/utils/types.js +8 -0
- package/lib/openapi/utils/utils/JsonPointer.d.ts +51 -0
- package/lib/openapi/utils/utils/JsonPointer.js +95 -0
- package/lib/openapi/utils/utils/helpers.d.ts +43 -0
- package/lib/openapi/utils/utils/helpers.js +230 -0
- package/lib/openapi/utils/utils/index.d.ts +3 -0
- package/lib/openapi/utils/utils/index.js +25 -0
- package/lib/openapi/utils/utils/openapi.d.ts +40 -0
- package/lib/openapi/utils/utils/openapi.js +605 -0
- package/lib/openapi/webhooks.test.d.ts +1 -0
- package/lib/openapi/webhooks.test.js +23 -0
- package/lib/options.d.ts +2 -0
- package/lib/options.js +70 -0
- package/lib/sidebars/index.d.ts +4 -0
- package/lib/sidebars/index.js +226 -0
- package/lib/sidebars/utils.d.ts +2 -0
- package/lib/sidebars/utils.js +30 -0
- package/lib/types.d.ts +138 -0
- package/lib/types.js +8 -0
- package/package.json +68 -0
- package/src/index.ts +955 -0
- package/src/markdown/__snapshots__/createSchema.test.ts.snap +1605 -0
- package/src/markdown/createArrayBracket.ts +35 -0
- package/src/markdown/createAuthentication.ts +209 -0
- package/src/markdown/createAuthorization.ts +13 -0
- package/src/markdown/createCallbackMethodEndpoint.ts +19 -0
- package/src/markdown/createCallbacks.ts +101 -0
- package/src/markdown/createContactInfo.ts +41 -0
- package/src/markdown/createDeprecationNotice.ts +31 -0
- package/src/markdown/createDescription.ts +12 -0
- package/src/markdown/createDetails.ts +16 -0
- package/src/markdown/createDetailsSummary.ts +16 -0
- package/src/markdown/createDownload.ts +15 -0
- package/src/markdown/createHeading.ts +23 -0
- package/src/markdown/createLicense.ts +34 -0
- package/src/markdown/createLogo.ts +21 -0
- package/src/markdown/createMethodEndpoint.ts +19 -0
- package/src/markdown/createParamsDetails.ts +22 -0
- package/src/markdown/createRequestBodyDetails.ts +24 -0
- package/src/markdown/createRequestHeader.ts +22 -0
- package/src/markdown/createRequestSchema.ts +32 -0
- package/src/markdown/createResponseSchema.ts +32 -0
- package/src/markdown/createSchema.test.ts +1075 -0
- package/src/markdown/createSchema.ts +864 -0
- package/src/markdown/createStatusCodes.ts +63 -0
- package/src/markdown/createTermsOfService.ts +30 -0
- package/src/markdown/createVendorExtensions.ts +22 -0
- package/src/markdown/createVersionBadge.ts +22 -0
- package/src/markdown/index.ts +145 -0
- package/src/markdown/schema.test.ts +208 -0
- package/src/markdown/schema.ts +188 -0
- package/src/markdown/utils.ts +89 -0
- package/src/openapi/__fixtures__/examples/openapi.yaml +49 -0
- package/src/openapi/__fixtures__/webhook/openapi.yaml +17 -0
- package/src/openapi/createRequestExample.ts +13 -0
- package/src/openapi/createResponseExample.ts +13 -0
- package/src/openapi/createSchemaExample.ts +292 -0
- package/src/openapi/index.ts +8 -0
- package/src/openapi/openapi.test.ts +40 -0
- package/src/openapi/openapi.ts +772 -0
- package/src/openapi/sdkExamples.ts +195 -0
- package/src/openapi/types.ts +458 -0
- package/src/openapi/utils/loadAndResolveSpec.ts +171 -0
- package/src/openapi/utils/services/OpenAPIParser.ts +434 -0
- package/src/openapi/utils/services/RedocNormalizedOptions.ts +330 -0
- package/src/openapi/utils/types/index.ts +10 -0
- package/src/openapi/utils/types/open-api.ts +303 -0
- package/src/openapi/utils/types.ts +304 -0
- package/src/openapi/utils/utils/JsonPointer.ts +99 -0
- package/src/openapi/utils/utils/helpers.ts +239 -0
- package/src/openapi/utils/utils/index.ts +11 -0
- package/src/openapi/utils/utils/openapi.ts +771 -0
- package/src/openapi/webhooks.test.ts +30 -0
- package/src/openapi-to-postmanv2.d.ts +10 -0
- package/src/options.ts +78 -0
- package/src/plugin-openapi.d.ts +88 -0
- package/src/sidebars/index.ts +322 -0
- package/src/sidebars/utils.ts +29 -0
- package/src/types.ts +180 -0
- 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, "<")
|
|
81
|
+
.replace(greaterThan, ">")
|
|
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
|
+
});
|