docusaurus-plugin-openapi-docs 2.2.0 → 2.2.2

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.
@@ -11,47 +11,305 @@ import { createNodes } from "./createSchema";
11
11
  import { SchemaObject } from "../openapi/types";
12
12
 
13
13
  describe("createNodes", () => {
14
- it("should create readable MODs for oneOf primitive properties", () => {
15
- const schema: SchemaObject = {
16
- "x-tags": ["clown"],
17
- type: "object",
18
- properties: {
19
- oneOfProperty: {
20
- oneOf: [
21
- {
22
- type: "object",
23
- properties: {
24
- noseLength: {
25
- type: "number",
14
+ describe("oneOf", () => {
15
+ it("should create readable MODs for oneOf primitive properties", async () => {
16
+ const schema: SchemaObject = {
17
+ "x-tags": ["clown"],
18
+ type: "object",
19
+ properties: {
20
+ oneOfProperty: {
21
+ oneOf: [
22
+ {
23
+ type: "object",
24
+ properties: {
25
+ noseLength: {
26
+ type: "number",
27
+ },
28
+ },
29
+ required: ["noseLength"],
30
+ description: "Clown's nose length",
31
+ },
32
+ {
33
+ type: "array",
34
+ items: {
35
+ type: "string",
36
+ },
37
+ description: "Array of strings",
38
+ },
39
+ {
40
+ type: "boolean",
41
+ },
42
+ {
43
+ type: "number",
44
+ },
45
+ {
46
+ type: "string",
47
+ },
48
+ ],
49
+ },
50
+ },
51
+ };
52
+ expect(
53
+ await Promise.all(
54
+ createNodes(schema, "request").map(
55
+ async (md: any) => await prettier.format(md, { parser: "babel" })
56
+ )
57
+ )
58
+ ).toMatchSnapshot();
59
+ });
60
+ });
61
+
62
+ describe("allOf", () => {
63
+ it("should render same-level properties with allOf", async () => {
64
+ const schema: SchemaObject = {
65
+ allOf: [
66
+ {
67
+ type: "object",
68
+ properties: {
69
+ allOfProp1: {
70
+ type: "string",
71
+ },
72
+ allOfProp2: {
73
+ type: "string",
74
+ },
75
+ },
76
+ },
77
+ ],
78
+ properties: {
79
+ parentProp1: {
80
+ type: "string",
81
+ },
82
+ parentProp2: {
83
+ type: "string",
84
+ },
85
+ },
86
+ };
87
+
88
+ expect(
89
+ await Promise.all(
90
+ createNodes(schema, "response").map(
91
+ async (md: any) => await prettier.format(md, { parser: "babel" })
92
+ )
93
+ )
94
+ ).toMatchSnapshot();
95
+ });
96
+
97
+ it("should correctly merge nested properties from multiple allOf schemas", async () => {
98
+ const schema: SchemaObject = {
99
+ allOf: [
100
+ {
101
+ type: "object",
102
+ properties: {
103
+ outerProp1: {
104
+ type: "object",
105
+ properties: {
106
+ innerProp1: {
107
+ type: "string",
108
+ },
26
109
  },
27
110
  },
28
- required: ["noseLength"],
29
- description: "Clown's nose length",
30
111
  },
31
- {
32
- type: "array",
33
- items: {
112
+ },
113
+ {
114
+ type: "object",
115
+ properties: {
116
+ outerProp2: {
117
+ type: "object",
118
+ properties: {
119
+ innerProp2: {
120
+ type: "number",
121
+ },
122
+ },
123
+ },
124
+ },
125
+ },
126
+ ],
127
+ };
128
+
129
+ expect(
130
+ await Promise.all(
131
+ createNodes(schema, "response").map(
132
+ async (md: any) => await prettier.format(md, { parser: "babel" })
133
+ )
134
+ )
135
+ ).toMatchSnapshot();
136
+ });
137
+
138
+ it("should correctly handle shared required properties across allOf schemas", async () => {
139
+ const schema: SchemaObject = {
140
+ allOf: [
141
+ {
142
+ type: "object",
143
+ properties: {
144
+ sharedProp: {
34
145
  type: "string",
35
146
  },
36
- description: "Array of strings",
37
147
  },
38
- {
39
- type: "boolean",
148
+ required: ["sharedProp"],
149
+ },
150
+ {
151
+ type: "object",
152
+ properties: {
153
+ anotherProp: {
154
+ type: "number",
155
+ },
40
156
  },
41
- {
42
- type: "number",
157
+ required: ["anotherProp"],
158
+ },
159
+ ],
160
+ };
161
+
162
+ expect(
163
+ await Promise.all(
164
+ createNodes(schema, "response").map(
165
+ async (md: any) => await prettier.format(md, { parser: "babel" })
166
+ )
167
+ )
168
+ ).toMatchSnapshot();
169
+ });
170
+
171
+ // Could not resolve values for path:"properties.conflictingProp.type". They are probably incompatible. Values:
172
+ // "string"
173
+ // "number"
174
+ // eslint-disable-next-line jest/no-commented-out-tests
175
+ // it("should handle conflicting properties in allOf schemas", async () => {
176
+ // const schema: SchemaObject = {
177
+ // allOf: [
178
+ // {
179
+ // type: "object",
180
+ // properties: {
181
+ // conflictingProp: {
182
+ // type: "string",
183
+ // },
184
+ // },
185
+ // },
186
+ // {
187
+ // type: "object",
188
+ // properties: {
189
+ // conflictingProp: {
190
+ // type: "number",
191
+ // },
192
+ // },
193
+ // },
194
+ // ],
195
+ // };
196
+
197
+ // expect(
198
+ // await Promise.all(
199
+ // createNodes(schema, "response").map(
200
+ // async (md: any) => await prettier.format(md, { parser: "babel" })
201
+ // )
202
+ // )
203
+ // ).toMatchSnapshot();
204
+ // });
205
+
206
+ // Could not resolve values for path:"type". They are probably incompatible. Values:
207
+ // "object"
208
+ // "array"
209
+ // eslint-disable-next-line jest/no-commented-out-tests
210
+ // it("should handle mixed data types in allOf schemas", async () => {
211
+ // const schema: SchemaObject = {
212
+ // allOf: [
213
+ // {
214
+ // type: "object",
215
+ // properties: {
216
+ // mixedTypeProp1: {
217
+ // type: "string",
218
+ // },
219
+ // },
220
+ // },
221
+ // {
222
+ // type: "array",
223
+ // items: {
224
+ // type: "number",
225
+ // },
226
+ // },
227
+ // ],
228
+ // };
229
+
230
+ // expect(
231
+ // await Promise.all(
232
+ // createNodes(schema, "response").map(
233
+ // async (md: any) => await prettier.format(md, { parser: "babel" })
234
+ // )
235
+ // )
236
+ // ).toMatchSnapshot();
237
+ // });
238
+
239
+ it("should correctly deep merge properties in allOf schemas", async () => {
240
+ const schema: SchemaObject = {
241
+ allOf: [
242
+ {
243
+ type: "object",
244
+ properties: {
245
+ deepProp: {
246
+ type: "object",
247
+ properties: {
248
+ innerProp1: {
249
+ type: "string",
250
+ },
251
+ },
252
+ },
43
253
  },
44
- {
45
- type: "string",
254
+ },
255
+ {
256
+ type: "object",
257
+ properties: {
258
+ deepProp: {
259
+ type: "object",
260
+ properties: {
261
+ innerProp2: {
262
+ type: "number",
263
+ },
264
+ },
265
+ },
46
266
  },
47
- ],
48
- },
49
- },
50
- };
51
- expect(
52
- createNodes(schema, "request").map((md: any) =>
53
- prettier.format(md, { parser: "babel" })
54
- )
55
- ).toMatchSnapshot();
267
+ },
268
+ ],
269
+ };
270
+
271
+ expect(
272
+ await Promise.all(
273
+ createNodes(schema, "response").map(
274
+ async (md: any) => await prettier.format(md, { parser: "babel" })
275
+ )
276
+ )
277
+ ).toMatchSnapshot();
278
+ });
279
+
280
+ // eslint-disable-next-line jest/no-commented-out-tests
281
+ // it("should handle discriminator with allOf schemas", async () => {
282
+ // const schema: SchemaObject = {
283
+ // allOf: [
284
+ // {
285
+ // type: "object",
286
+ // discriminator: {
287
+ // propertyName: "type",
288
+ // },
289
+ // properties: {
290
+ // type: {
291
+ // type: "string",
292
+ // },
293
+ // },
294
+ // },
295
+ // {
296
+ // type: "object",
297
+ // properties: {
298
+ // specificProp: {
299
+ // type: "string",
300
+ // },
301
+ // },
302
+ // },
303
+ // ],
304
+ // };
305
+
306
+ // expect(
307
+ // await Promise.all(
308
+ // createNodes(schema, "response").map(
309
+ // async (md: any) => await prettier.format(md, { parser: "babel" })
310
+ // )
311
+ // )
312
+ // ).toMatchSnapshot();
313
+ // });
56
314
  });
57
315
  });
@@ -73,6 +73,15 @@ function createAnyOneOf(schema: SchemaObject): any {
73
73
  : `MOD${index + 1}`;
74
74
  const anyOneChildren = [];
75
75
 
76
+ if (
77
+ anyOneSchema.type === "object" &&
78
+ !anyOneSchema.properties &&
79
+ !anyOneSchema.allOf &&
80
+ !anyOneSchema.items
81
+ ) {
82
+ anyOneChildren.push(createNodes(anyOneSchema, SCHEMA_TYPE));
83
+ }
84
+
76
85
  if (anyOneSchema.properties !== undefined) {
77
86
  anyOneChildren.push(createProperties(anyOneSchema));
78
87
  delete anyOneSchema.properties;
@@ -617,6 +626,7 @@ function createEdges({
617
626
  }
618
627
 
619
628
  const schemaName = getSchemaName(schema);
629
+
620
630
  if (discriminator !== undefined && discriminator.propertyName === name) {
621
631
  return createPropertyDiscriminator(
622
632
  name,
@@ -637,6 +647,47 @@ function createEdges({
637
647
  );
638
648
  }
639
649
 
650
+ if (schema.properties !== undefined) {
651
+ return createDetailsNode(
652
+ name,
653
+ schemaName,
654
+ schema,
655
+ required,
656
+ schema.nullable
657
+ );
658
+ }
659
+
660
+ if (schema.additionalProperties !== undefined) {
661
+ return createDetailsNode(
662
+ name,
663
+ schemaName,
664
+ schema,
665
+ required,
666
+ schema.nullable
667
+ );
668
+ }
669
+
670
+ // array of objects
671
+ if (schema.items?.properties !== undefined) {
672
+ return createDetailsNode(
673
+ name,
674
+ schemaName,
675
+ schema,
676
+ required,
677
+ schema.nullable
678
+ );
679
+ }
680
+
681
+ if (schema.items?.anyOf !== undefined || schema.items?.oneOf !== undefined) {
682
+ return createDetailsNode(
683
+ name,
684
+ schemaName,
685
+ schema,
686
+ required,
687
+ schema.nullable
688
+ );
689
+ }
690
+
640
691
  if (schema.allOf !== undefined) {
641
692
  const { mergedSchemas }: { mergedSchemas: SchemaObject } = mergeAllOf(
642
693
  schema.allOf
@@ -709,47 +760,6 @@ function createEdges({
709
760
  });
710
761
  }
711
762
 
712
- if (schema.properties !== undefined) {
713
- return createDetailsNode(
714
- name,
715
- schemaName,
716
- schema,
717
- required,
718
- schema.nullable
719
- );
720
- }
721
-
722
- if (schema.additionalProperties !== undefined) {
723
- return createDetailsNode(
724
- name,
725
- schemaName,
726
- schema,
727
- required,
728
- schema.nullable
729
- );
730
- }
731
-
732
- // array of objects
733
- if (schema.items?.properties !== undefined) {
734
- return createDetailsNode(
735
- name,
736
- schemaName,
737
- schema,
738
- required,
739
- schema.nullable
740
- );
741
- }
742
-
743
- if (schema.items?.anyOf !== undefined || schema.items?.oneOf !== undefined) {
744
- return createDetailsNode(
745
- name,
746
- schemaName,
747
- schema,
748
- required,
749
- schema.nullable
750
- );
751
- }
752
-
753
763
  // primitives and array of non-objects
754
764
  return create("SchemaItem", {
755
765
  collapsible: false,
@@ -789,15 +799,6 @@ export function createNodes(
789
799
  nodes.push(createAnyOneOf(schema));
790
800
  }
791
801
 
792
- if (schema.allOf !== undefined) {
793
- const { mergedSchemas } = mergeAllOf(schema.allOf);
794
-
795
- // allOf seems to always result in properties
796
- if (mergedSchemas.properties !== undefined) {
797
- nodes.push(createProperties(mergedSchemas));
798
- }
799
- }
800
-
801
802
  if (schema.properties !== undefined) {
802
803
  nodes.push(createProperties(schema));
803
804
  }
@@ -811,6 +812,15 @@ export function createNodes(
811
812
  nodes.push(createItems(schema));
812
813
  }
813
814
 
815
+ if (schema.allOf !== undefined) {
816
+ const { mergedSchemas } = mergeAllOf(schema.allOf);
817
+
818
+ // allOf seems to always result in properties
819
+ if (mergedSchemas.properties !== undefined) {
820
+ nodes.push(createProperties(mergedSchemas));
821
+ }
822
+ }
823
+
814
824
  if (nodes.length && nodes.length > 0) {
815
825
  return nodes.filter(Boolean).flat();
816
826
  }
@@ -127,8 +127,9 @@ export function createResponseExamples(
127
127
  value: `${exampleName}`,
128
128
  children: [
129
129
  guard(exampleValue.summary, (summary) => [
130
- create("Markdown", {
131
- children: ` ${summary}`,
130
+ create("div", {
131
+ children: `${summary}`,
132
+ className: "openapi-example__summary",
132
133
  }),
133
134
  ]),
134
135
  create("ResponseSamples", {
@@ -143,8 +144,9 @@ export function createResponseExamples(
143
144
  value: `${exampleName}`,
144
145
  children: [
145
146
  guard(exampleValue.summary, (summary) => [
146
- create("Markdown", {
147
- children: ` ${summary}`,
147
+ create("div", {
148
+ children: `${summary}`,
149
+ className: "openapi-example__summary",
148
150
  }),
149
151
  ]),
150
152
  create("ResponseSamples", {
@@ -171,8 +173,9 @@ export function createResponseExample(responseExample: any, mimeType: string) {
171
173
  value: `Example`,
172
174
  children: [
173
175
  guard(responseExample.summary, (summary) => [
174
- create("Markdown", {
175
- children: ` ${summary}`,
176
+ create("div", {
177
+ children: `${summary}`,
178
+ className: "openapi-example__summary",
176
179
  }),
177
180
  ]),
178
181
  create("ResponseSamples", {
@@ -187,8 +190,9 @@ export function createResponseExample(responseExample: any, mimeType: string) {
187
190
  value: `Example`,
188
191
  children: [
189
192
  guard(responseExample.summary, (summary) => [
190
- create("Markdown", {
191
- children: ` ${summary}`,
193
+ create("div", {
194
+ children: `${summary}`,
195
+ className: "openapi-example__summary",
192
196
  }),
193
197
  ]),
194
198
  create("ResponseSamples", {
@@ -76,7 +76,7 @@ export function createApiPageMD({
76
76
  `import ResponseSamples from "@theme/ResponseSamples";\n`,
77
77
  `import SchemaItem from "@theme/SchemaItem";\n`,
78
78
  `import SchemaTabs from "@theme/SchemaTabs";\n`,
79
- `import Markdown from "@theme/Markdown";\n`,
79
+ `import Heading from "@theme/Heading";\n`,
80
80
  `import OperationTabs from "@theme/OperationTabs";\n`,
81
81
  `import TabItem from "@theme/TabItem";\n\n`,
82
82
  createHeading(title.replace(lessThan, "<").replace(greaterThan, ">")),
@@ -30,8 +30,8 @@ const primitives: Primitives = {
30
30
  string: {
31
31
  default: () => "string",
32
32
  email: () => "user@example.com",
33
- date: () => new Date().toISOString().substring(0, 10),
34
- "date-time": () => new Date().toISOString(),
33
+ date: () => "2024-07-29",
34
+ "date-time": () => "2024-07-29T15:51:28.071Z",
35
35
  uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
36
36
  hostname: () => "example.com",
37
37
  ipv4: () => "198.51.100.42",
@@ -30,8 +30,8 @@ const primitives: Primitives = {
30
30
  string: {
31
31
  default: () => "string",
32
32
  email: () => "user@example.com",
33
- date: () => new Date().toISOString().substring(0, 10),
34
- "date-time": () => new Date().toISOString(),
33
+ date: () => "2024-07-29",
34
+ "date-time": () => "2024-07-29T15:51:28.071Z",
35
35
  uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
36
36
  hostname: () => "example.com",
37
37
  ipv4: () => "198.51.100.42",