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.
@@ -9,7 +9,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  return (mod && mod.__esModule) ? mod : { "default": mod };
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.sampleFromSchema = void 0;
12
+ exports.sampleRequestFromSchema = void 0;
13
13
  const chalk_1 = __importDefault(require("chalk"));
14
14
  const createRequestSchema_1 = require("../markdown/createRequestSchema");
15
15
  const primitives = {
@@ -36,12 +36,42 @@ const primitives = {
36
36
  object: {},
37
37
  array: {},
38
38
  };
39
- const sampleFromSchema = (schema = {}) => {
39
+ function sampleRequestFromProp(name, prop, obj) {
40
+ // Handle resolved circular props
41
+ if (typeof prop === "object" && Object.keys(prop).length === 0) {
42
+ obj[name] = prop;
43
+ return obj;
44
+ }
45
+ // TODO: handle discriminators
46
+ if (prop.oneOf) {
47
+ obj[name] = (0, exports.sampleRequestFromSchema)(prop.oneOf[0]);
48
+ }
49
+ else if (prop.anyOf) {
50
+ obj[name] = (0, exports.sampleRequestFromSchema)(prop.anyOf[0]);
51
+ }
52
+ else if (prop.allOf) {
53
+ const { mergedSchemas } = (0, createRequestSchema_1.mergeAllOf)(prop.allOf);
54
+ sampleRequestFromProp(name, mergedSchemas, obj);
55
+ }
56
+ else {
57
+ obj[name] = (0, exports.sampleRequestFromSchema)(prop);
58
+ }
59
+ return obj;
60
+ }
61
+ const sampleRequestFromSchema = (schema = {}) => {
40
62
  try {
41
- let { type, example, allOf, properties, items } = schema;
63
+ let { type, example, allOf, properties, items, oneOf, anyOf } = schema;
42
64
  if (example !== undefined) {
43
65
  return example;
44
66
  }
67
+ if (oneOf) {
68
+ // Just go with first schema
69
+ return (0, exports.sampleRequestFromSchema)(oneOf[0]);
70
+ }
71
+ if (anyOf) {
72
+ // Just go with first schema
73
+ return (0, exports.sampleRequestFromSchema)(anyOf[0]);
74
+ }
45
75
  if (allOf) {
46
76
  const { mergedSchemas } = (0, createRequestSchema_1.mergeAllOf)(allOf);
47
77
  if (mergedSchemas.properties) {
@@ -51,7 +81,7 @@ const sampleFromSchema = (schema = {}) => {
51
81
  }
52
82
  }
53
83
  }
54
- return (0, exports.sampleFromSchema)(mergedSchemas);
84
+ return (0, exports.sampleRequestFromSchema)(mergedSchemas);
55
85
  }
56
86
  if (!type) {
57
87
  if (properties) {
@@ -84,18 +114,19 @@ const sampleFromSchema = (schema = {}) => {
84
114
  if (prop.deprecated) {
85
115
  continue;
86
116
  }
87
- obj[name] = (0, exports.sampleFromSchema)(prop);
117
+ // Resolve schema from prop recursively
118
+ obj = sampleRequestFromProp(name, prop, obj);
88
119
  }
89
120
  return obj;
90
121
  }
91
122
  if (type === "array") {
92
123
  if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
93
- return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleFromSchema)(item));
124
+ return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleRequestFromSchema)(item));
94
125
  }
95
126
  if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
96
- return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleFromSchema)(item));
127
+ return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleRequestFromSchema)(item));
97
128
  }
98
- return [(0, exports.sampleFromSchema)(items)];
129
+ return [(0, exports.sampleRequestFromSchema)(items)];
99
130
  }
100
131
  if (schema.enum) {
101
132
  if (schema.default) {
@@ -113,7 +144,7 @@ const sampleFromSchema = (schema = {}) => {
113
144
  return;
114
145
  }
115
146
  };
116
- exports.sampleFromSchema = sampleFromSchema;
147
+ exports.sampleRequestFromSchema = sampleRequestFromSchema;
117
148
  function primitive(schema = {}) {
118
149
  let { type, format } = schema;
119
150
  if (type === undefined) {
@@ -0,0 +1,2 @@
1
+ import { SchemaObject } from "./types";
2
+ export declare const sampleResponseFromSchema: (schema?: SchemaObject) => any;
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ /* ============================================================================
3
+ * Copyright (c) Palo Alto Networks
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ * ========================================================================== */
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.sampleResponseFromSchema = void 0;
13
+ const chalk_1 = __importDefault(require("chalk"));
14
+ const createRequestSchema_1 = require("../markdown/createRequestSchema");
15
+ const primitives = {
16
+ string: {
17
+ default: () => "string",
18
+ email: () => "user@example.com",
19
+ date: () => new Date().toISOString().substring(0, 10),
20
+ "date-time": () => new Date().toISOString().substring(0, 10),
21
+ uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
22
+ hostname: () => "example.com",
23
+ ipv4: () => "198.51.100.42",
24
+ ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
25
+ },
26
+ number: {
27
+ default: () => 0,
28
+ float: () => 0.0,
29
+ },
30
+ integer: {
31
+ default: () => 0,
32
+ },
33
+ boolean: {
34
+ default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
35
+ },
36
+ object: {},
37
+ array: {},
38
+ };
39
+ function sampleResponseFromProp(name, prop, obj) {
40
+ // Handle resolved circular props
41
+ if (typeof prop === "object" && Object.keys(prop).length === 0) {
42
+ obj[name] = prop;
43
+ return obj;
44
+ }
45
+ // TODO: handle discriminators
46
+ if (prop.oneOf) {
47
+ obj[name] = (0, exports.sampleResponseFromSchema)(prop.oneOf[0]);
48
+ }
49
+ else if (prop.anyOf) {
50
+ obj[name] = (0, exports.sampleResponseFromSchema)(prop.anyOf[0]);
51
+ }
52
+ else if (prop.allOf) {
53
+ const { mergedSchemas } = (0, createRequestSchema_1.mergeAllOf)(prop.allOf);
54
+ sampleResponseFromProp(name, mergedSchemas, obj);
55
+ }
56
+ else {
57
+ obj[name] = (0, exports.sampleResponseFromSchema)(prop);
58
+ }
59
+ return obj;
60
+ }
61
+ const sampleResponseFromSchema = (schema = {}) => {
62
+ try {
63
+ let { type, example, allOf, oneOf, anyOf, properties, items } = schema;
64
+ if (example !== undefined) {
65
+ return example;
66
+ }
67
+ if (allOf) {
68
+ const { mergedSchemas } = (0, createRequestSchema_1.mergeAllOf)(allOf);
69
+ if (mergedSchemas.properties) {
70
+ for (const [key, value] of Object.entries(mergedSchemas.properties)) {
71
+ if (value.readOnly && value.readOnly === true) {
72
+ delete mergedSchemas.properties[key];
73
+ }
74
+ }
75
+ }
76
+ return (0, exports.sampleResponseFromSchema)(mergedSchemas);
77
+ }
78
+ if (oneOf) {
79
+ // Just go with first schema
80
+ return (0, exports.sampleResponseFromSchema)(oneOf[0]);
81
+ }
82
+ if (anyOf) {
83
+ // Just go with first schema
84
+ return (0, exports.sampleResponseFromSchema)(anyOf[0]);
85
+ }
86
+ if (!type) {
87
+ if (properties) {
88
+ type = "object";
89
+ }
90
+ else if (items) {
91
+ type = "array";
92
+ }
93
+ else {
94
+ return;
95
+ }
96
+ }
97
+ if (type === "object") {
98
+ let obj = {};
99
+ for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
100
+ if (prop.properties) {
101
+ for (const [key, value] of Object.entries(prop.properties)) {
102
+ if (value.readOnly && value.readOnly === true) {
103
+ delete prop.properties[key];
104
+ }
105
+ }
106
+ }
107
+ if (prop.items && prop.items.properties) {
108
+ for (const [key, value] of Object.entries(prop.items.properties)) {
109
+ if (value.readOnly && value.readOnly === true) {
110
+ delete prop.items.properties[key];
111
+ }
112
+ }
113
+ }
114
+ if (prop.deprecated) {
115
+ continue;
116
+ }
117
+ // Resolve schema from prop recursively
118
+ obj = sampleResponseFromProp(name, prop, obj);
119
+ }
120
+ return obj;
121
+ }
122
+ if (type === "array") {
123
+ if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
124
+ return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleResponseFromSchema)(item));
125
+ }
126
+ if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
127
+ return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleResponseFromSchema)(item));
128
+ }
129
+ return [(0, exports.sampleResponseFromSchema)(items)];
130
+ }
131
+ if (schema.enum) {
132
+ if (schema.default) {
133
+ return schema.default;
134
+ }
135
+ return normalizeArray(schema.enum)[0];
136
+ }
137
+ if (schema.writeOnly && schema.writeOnly === true) {
138
+ return undefined;
139
+ }
140
+ return primitive(schema);
141
+ }
142
+ catch (err) {
143
+ console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
144
+ return;
145
+ }
146
+ };
147
+ exports.sampleResponseFromSchema = sampleResponseFromSchema;
148
+ function primitive(schema = {}) {
149
+ let { type, format } = schema;
150
+ if (type === undefined) {
151
+ return;
152
+ }
153
+ let fn = schema.default ? () => schema.default : primitives[type].default;
154
+ if (format !== undefined) {
155
+ fn = primitives[type][format] || fn;
156
+ }
157
+ if (fn) {
158
+ return fn(schema);
159
+ }
160
+ return "Unknown Type: " + schema.type;
161
+ }
162
+ function normalizeArray(arr) {
163
+ if (Array.isArray(arr)) {
164
+ return arr;
165
+ }
166
+ return [arr];
167
+ }
@@ -19,7 +19,7 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
19
19
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
20
20
  const kebabCase_1 = __importDefault(require("lodash/kebabCase"));
21
21
  const index_1 = require("../index");
22
- const createExample_1 = require("./createExample");
22
+ const createRequestExample_1 = require("./createRequestExample");
23
23
  const loadAndResolveSpec_1 = require("./utils/loadAndResolveSpec");
24
24
  /**
25
25
  * Convenience function for converting raw JSON to a Postman Collection object.
@@ -134,7 +134,7 @@ function createItems(openapiData, sidebarOptions) {
134
134
  let jsonRequestBodyExample;
135
135
  const body = (_o = (_m = operationObject.requestBody) === null || _m === void 0 ? void 0 : _m.content) === null || _o === void 0 ? void 0 : _o["application/json"];
136
136
  if (body === null || body === void 0 ? void 0 : body.schema) {
137
- jsonRequestBodyExample = (0, createExample_1.sampleFromSchema)(body.schema);
137
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(body.schema);
138
138
  }
139
139
  // Handle vendor JSON media types
140
140
  const bodyContent = (_p = operationObject.requestBody) === null || _p === void 0 ? void 0 : _p.content;
@@ -143,7 +143,7 @@ function createItems(openapiData, sidebarOptions) {
143
143
  if (firstBodyContentKey.endsWith("+json")) {
144
144
  const firstBody = bodyContent[firstBodyContentKey];
145
145
  if (firstBody === null || firstBody === void 0 ? void 0 : firstBody.schema) {
146
- jsonRequestBodyExample = (0, createExample_1.sampleFromSchema)(firstBody.schema);
146
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(firstBody.schema);
147
147
  }
148
148
  }
149
149
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-openapi-docs",
3
3
  "description": "OpenAPI plugin for Docusaurus.",
4
- "version": "0.0.0-429",
4
+ "version": "0.0.0-433",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -59,7 +59,8 @@
59
59
  "slugify": "^1.6.5",
60
60
  "swagger2openapi": "^7.0.8",
61
61
  "url-template": "^3.0.0",
62
- "webpack": "^5.61.0"
62
+ "webpack": "^5.61.0",
63
+ "xml-formatter": "^2.6.1"
63
64
  },
64
65
  "peerDependencies": {
65
66
  "react": "^16.8.4 || ^17.0.0"
@@ -67,5 +68,5 @@
67
68
  "engines": {
68
69
  "node": ">=14"
69
70
  },
70
- "gitHead": "5e8eda4016c28efed34a740aca4dca0f5450835a"
71
+ "gitHead": "e3e99aa7627392218adc006b4918390d3efd7b3d"
71
72
  }
@@ -286,106 +286,106 @@ function createItems(schema: SchemaObject) {
286
286
  /**
287
287
  * For handling discriminators that do not map to a same-level property
288
288
  */
289
- function createDiscriminator(schema: SchemaObject) {
290
- const discriminator = schema.discriminator;
291
- const propertyName = discriminator?.propertyName;
292
- const propertyType = "string"; // should always be string
293
- const mapping: any = discriminator?.mapping;
294
-
295
- // Explicit mapping is required since we can't support implicit
296
- if (mapping === undefined) {
297
- return undefined;
298
- }
299
-
300
- // Attempt to get the property description we want to display
301
- // TODO: how to make it predictable when handling allOf
302
- let propertyDescription;
303
- const firstMappingSchema = mapping[Object.keys(mapping)[0]];
304
- if (firstMappingSchema.properties !== undefined) {
305
- propertyDescription =
306
- firstMappingSchema.properties![propertyName!].description;
307
- }
308
- if (firstMappingSchema.allOf !== undefined) {
309
- const { mergedSchemas }: { mergedSchemas: SchemaObject } = mergeAllOf(
310
- firstMappingSchema.allOf
311
- );
312
- if (mergedSchemas.properties !== undefined) {
313
- propertyDescription =
314
- mergedSchemas.properties[propertyName!]?.description;
315
- }
316
- }
317
-
318
- if (propertyDescription === undefined) {
319
- if (
320
- schema.properties !== undefined &&
321
- schema.properties![propertyName!] !== undefined
322
- ) {
323
- propertyDescription = schema.properties![propertyName!].description;
324
- }
325
- }
326
-
327
- return create("div", {
328
- className: "discriminatorItem",
329
- children: create("div", {
330
- children: [
331
- create("strong", {
332
- style: { paddingLeft: "1rem" },
333
- children: propertyName,
334
- }),
335
- guard(propertyType, (name) =>
336
- create("span", {
337
- style: { opacity: "0.6" },
338
- children: ` ${propertyType}`,
339
- })
340
- ),
341
- guard(getQualifierMessage(schema.discriminator as any), (message) =>
342
- create("div", {
343
- style: {
344
- paddingLeft: "1rem",
345
- },
346
- children: createDescription(message),
347
- })
348
- ),
349
- guard(propertyDescription, (description) =>
350
- create("div", {
351
- style: {
352
- paddingLeft: "1rem",
353
- },
354
- children: createDescription(description),
355
- })
356
- ),
357
- create("DiscriminatorTabs", {
358
- children: Object.keys(mapping!).map((key, index) => {
359
- if (mapping[key].allOf !== undefined) {
360
- const { mergedSchemas }: { mergedSchemas: SchemaObject } =
361
- mergeAllOf(mapping[key].allOf);
362
- // Cleanup duplicate property from mapping schema
363
- delete mergedSchemas.properties![propertyName!];
364
- mapping[key] = mergedSchemas;
365
- }
366
-
367
- if (mapping[key].properties !== undefined) {
368
- // Cleanup duplicate property from mapping schema
369
- delete mapping[key].properties![propertyName!];
370
- }
371
-
372
- const label = key;
373
- return create("TabItem", {
374
- label: label,
375
- value: `${index}-item-discriminator`,
376
- children: [
377
- create("div", {
378
- style: { marginLeft: "-4px" },
379
- children: createNodes(mapping[key]),
380
- }),
381
- ],
382
- });
383
- }),
384
- }),
385
- ],
386
- }),
387
- });
388
- }
289
+ // function createDiscriminator(schema: SchemaObject) {
290
+ // const discriminator = schema.discriminator;
291
+ // const propertyName = discriminator?.propertyName;
292
+ // const propertyType = "string"; // should always be string
293
+ // const mapping: any = discriminator?.mapping;
294
+
295
+ // // Explicit mapping is required since we can't support implicit
296
+ // if (mapping === undefined) {
297
+ // return undefined;
298
+ // }
299
+
300
+ // // Attempt to get the property description we want to display
301
+ // // TODO: how to make it predictable when handling allOf
302
+ // let propertyDescription;
303
+ // const firstMappingSchema = mapping[Object.keys(mapping)[0]];
304
+ // if (firstMappingSchema.properties !== undefined) {
305
+ // propertyDescription =
306
+ // firstMappingSchema.properties![propertyName!].description;
307
+ // }
308
+ // if (firstMappingSchema.allOf !== undefined) {
309
+ // const { mergedSchemas }: { mergedSchemas: SchemaObject } = mergeAllOf(
310
+ // firstMappingSchema.allOf
311
+ // );
312
+ // if (mergedSchemas.properties !== undefined) {
313
+ // propertyDescription =
314
+ // mergedSchemas.properties[propertyName!]?.description;
315
+ // }
316
+ // }
317
+
318
+ // if (propertyDescription === undefined) {
319
+ // if (
320
+ // schema.properties !== undefined &&
321
+ // schema.properties![propertyName!] !== undefined
322
+ // ) {
323
+ // propertyDescription = schema.properties![propertyName!].description;
324
+ // }
325
+ // }
326
+
327
+ // return create("div", {
328
+ // className: "discriminatorItem",
329
+ // children: create("div", {
330
+ // children: [
331
+ // create("strong", {
332
+ // style: { paddingLeft: "1rem" },
333
+ // children: propertyName,
334
+ // }),
335
+ // guard(propertyType, (name) =>
336
+ // create("span", {
337
+ // style: { opacity: "0.6" },
338
+ // children: ` ${propertyType}`,
339
+ // })
340
+ // ),
341
+ // guard(getQualifierMessage(schema.discriminator as any), (message) =>
342
+ // create("div", {
343
+ // style: {
344
+ // paddingLeft: "1rem",
345
+ // },
346
+ // children: createDescription(message),
347
+ // })
348
+ // ),
349
+ // guard(propertyDescription, (description) =>
350
+ // create("div", {
351
+ // style: {
352
+ // paddingLeft: "1rem",
353
+ // },
354
+ // children: createDescription(description),
355
+ // })
356
+ // ),
357
+ // create("DiscriminatorTabs", {
358
+ // children: Object.keys(mapping!).map((key, index) => {
359
+ // if (mapping[key].allOf !== undefined) {
360
+ // const { mergedSchemas }: { mergedSchemas: SchemaObject } =
361
+ // mergeAllOf(mapping[key].allOf);
362
+ // // Cleanup duplicate property from mapping schema
363
+ // delete mergedSchemas.properties![propertyName!];
364
+ // mapping[key] = mergedSchemas;
365
+ // }
366
+
367
+ // if (mapping[key].properties !== undefined) {
368
+ // // Cleanup duplicate property from mapping schema
369
+ // delete mapping[key].properties![propertyName!];
370
+ // }
371
+
372
+ // const label = key;
373
+ // return create("TabItem", {
374
+ // label: label,
375
+ // value: `${index}-item-discriminator`,
376
+ // children: [
377
+ // create("div", {
378
+ // style: { marginLeft: "-4px" },
379
+ // children: createNodes(mapping[key]),
380
+ // }),
381
+ // ],
382
+ // });
383
+ // }),
384
+ // }),
385
+ // ],
386
+ // }),
387
+ // });
388
+ // }
389
389
 
390
390
  function createDetailsNode(
391
391
  name: string,
@@ -624,9 +624,9 @@ function createEdges({
624
624
  * Creates a hierarchical level of a schema tree. Nodes produce edges that can branch into sub-nodes with edges, recursively.
625
625
  */
626
626
  function createNodes(schema: SchemaObject): any {
627
- if (schema.discriminator !== undefined) {
628
- return createDiscriminator(schema);
629
- }
627
+ // if (schema.discriminator !== undefined) {
628
+ // return createDiscriminator(schema);
629
+ // }
630
630
 
631
631
  if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
632
632
  return createAnyOneOf(schema);