vitepress-openapi 0.2.7 → 0.2.8

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 (174) hide show
  1. package/dist/{angular-html-DAq3gDGA.cjs → angular-html-IrYiNUL5.cjs} +1 -1
  2. package/dist/{angular-html-C0iYY931.js → angular-html-uv0a0QNn.js} +2 -2
  3. package/dist/{angular-ts-Ct5d_gHN.js → angular-ts-CVTzQ4yM.js} +2 -2
  4. package/dist/{angular-ts-B2PPB0Y5.cjs → angular-ts-CmalYPic.cjs} +1 -1
  5. package/dist/{apl-CoWANpfc.cjs → apl-Cp2eHb5H.cjs} +1 -1
  6. package/dist/{apl-CiiA3JDJ.js → apl-G7yswX6Q.js} +5 -5
  7. package/dist/{astro-BX6R0KnA.cjs → astro-CRpTyG5K.cjs} +1 -1
  8. package/dist/{astro-Bzlt-IuJ.js → astro-DUi5zMa4.js} +5 -5
  9. package/dist/{blade-BBZCfhID.cjs → blade-DMsLIvGX.cjs} +1 -1
  10. package/dist/{blade-BGVF6ubL.js → blade-uJ4Xa5wc.js} +7 -7
  11. package/dist/{c-BpiaxH9s.js → c-CtMd0n5W.js} +1 -1
  12. package/dist/{c-CHRY6FQk.cjs → c-DJDNxT35.cjs} +1 -1
  13. package/dist/{chapel-CCUE2hqL.cjs → chapel-CCgxhb9n.cjs} +1 -1
  14. package/dist/{chapel-fECU7c1r.js → chapel-DGgSBAIm.js} +1 -1
  15. package/dist/{cobol-lxVOio4o.js → cobol-CDcij9i2.js} +2 -2
  16. package/dist/{cobol-Ete-w65U.cjs → cobol-DP8yLe0J.cjs} +1 -1
  17. package/dist/{coffee-D_GP6Mp_.js → coffee-Bcg2ycks.js} +1 -1
  18. package/dist/{coffee-DcVdEhV6.cjs → coffee-D3hXAm0d.cjs} +1 -1
  19. package/dist/{cpp-BQiBH6_n.cjs → cpp-CEMyuz0Z.cjs} +1 -1
  20. package/dist/{cpp-C2_nndOX.js → cpp-CfsSSf1b.js} +3 -3
  21. package/dist/{crystal-D68fQQf4.js → crystal-B76s5RL_.js} +6 -6
  22. package/dist/{crystal-bWCs3mcQ.cjs → crystal-BPjfGQfv.cjs} +1 -1
  23. package/dist/{css-LUs62piT.cjs → css-B_5YxPHw.cjs} +1 -1
  24. package/dist/{css-CwZ52NVY.js → css-Dnve9eIF.js} +1 -1
  25. package/dist/{edge-CRrD84FE.cjs → edge-DEHYswlE.cjs} +1 -1
  26. package/dist/{edge-CxIBp4BJ.js → edge-Dlu9JAOU.js} +3 -3
  27. package/dist/{elixir-txrLIPgt.cjs → elixir-CKhKSpjp.cjs} +1 -1
  28. package/dist/{elixir-DzlkeFTp.js → elixir-CSK8G02U.js} +1 -1
  29. package/dist/{elm-BSAvBsXU.js → elm-CsVe-drn.js} +1 -1
  30. package/dist/{elm-D3rgeOAI.cjs → elm-oUMNo4dE.cjs} +1 -1
  31. package/dist/{erb-CDFFPhk2.js → erb-3PcpbjWs.js} +2 -2
  32. package/dist/{erb-CEerDw7_.cjs → erb-B8WJz_Ls.cjs} +1 -1
  33. package/dist/{git-rebase-BI3TNfL1.js → git-rebase-BFPUa7RP.js} +1 -1
  34. package/dist/{git-rebase-BRc34H8A.cjs → git-rebase-CBj8NRO-.cjs} +1 -1
  35. package/dist/{glimmer-js-BjCbjAxk.cjs → glimmer-js-BkM2LzJ_.cjs} +1 -1
  36. package/dist/{glimmer-js-BCOl9pkK.js → glimmer-js-C06d46qf.js} +4 -4
  37. package/dist/{glimmer-ts-BaygTMht.cjs → glimmer-ts-B9TsQ9YM.cjs} +1 -1
  38. package/dist/{glimmer-ts-DtU3AR2J.js → glimmer-ts-BfZJAJhH.js} +4 -4
  39. package/dist/{glsl-C3jot75m.js → glsl-Bl7M-IPe.js} +2 -2
  40. package/dist/{glsl-C4QVQ8IM.cjs → glsl-uL1y-Bk6.cjs} +1 -1
  41. package/dist/{graphql-CWVurcx-.cjs → graphql-UjpB-MIz.cjs} +1 -1
  42. package/dist/{graphql-C57m_foI.js → graphql-j5wYAqtD.js} +5 -5
  43. package/dist/{hack-LG-O4iX2.js → hack-BN-XE31l.js} +2 -2
  44. package/dist/{hack-CO29PdAW.cjs → hack-DC3gQB4E.cjs} +1 -1
  45. package/dist/{haml-D5udMhEZ.cjs → haml-Bhy5P-KC.cjs} +1 -1
  46. package/dist/{haml-Qqt96dA3.js → haml-CDfF0h4F.js} +3 -3
  47. package/dist/{handlebars-BBft0uRg.js → handlebars-BRDXZqV4.js} +4 -4
  48. package/dist/{handlebars-dotklBKW.cjs → handlebars-CNE9oFMe.cjs} +1 -1
  49. package/dist/{html-Cm4eMgz9.js → html-CPA7KoKg.js} +3 -3
  50. package/dist/{html-hrBEmwSv.cjs → html-DF6ZRDg_.cjs} +1 -1
  51. package/dist/{html-derivative-DMWaumzq.js → html-derivative-61htNTyH.js} +1 -1
  52. package/dist/{html-derivative-Cu6O2gHN.cjs → html-derivative-D7b_E1PX.cjs} +1 -1
  53. package/dist/{http-D168KNW_.cjs → http-BjRzGfMq.cjs} +1 -1
  54. package/dist/{http-Cz6VY5Wo.js → http-CR6vxe-f.js} +4 -4
  55. package/dist/{hurl-D36azgIj.js → hurl-DR3Ao4uN.js} +2 -2
  56. package/dist/{hurl-DSwY6Z5G.cjs → hurl-Dz53E_EO.cjs} +1 -1
  57. package/dist/{java-BKXCRk8D.js → java-BCOO6DGx.js} +1 -1
  58. package/dist/{java-CFiC7SeI.cjs → java-rdA6e0wp.cjs} +1 -1
  59. package/dist/{javascript-Bx9N9g1f.cjs → javascript-4PA_CVZC.cjs} +1 -1
  60. package/dist/{javascript-DljrdC8t.js → javascript-B0H0Bm6m.js} +1 -1
  61. package/dist/{jinja-f05HaP8v.cjs → jinja-BcxYV4gu.cjs} +1 -1
  62. package/dist/{jinja-NZUdcntO.js → jinja-DdO6Tf4t.js} +1 -1
  63. package/dist/{jison-Dlb0RIZ3.cjs → jison-BJENBrIF.cjs} +1 -1
  64. package/dist/{jison-BQnZ1V4P.js → jison-CmQksr2y.js} +1 -1
  65. package/dist/{json-CzaEM6zR.cjs → json-BJ_bvg_A.cjs} +1 -1
  66. package/dist/{json-DyprfpNy.js → json-Dd3OP7m9.js} +1 -1
  67. package/dist/{jsx-DoPFZPXc.js → jsx-BhoiPRal.js} +1 -1
  68. package/dist/{jsx-X5IYTTmR.cjs → jsx-CaK8syd3.cjs} +1 -1
  69. package/dist/{julia-DrSudrKE.js → julia-CDeyMHwa.js} +4 -4
  70. package/dist/{julia-BUcVnjBf.cjs → julia-DdPKjdf7.cjs} +1 -1
  71. package/dist/{just-DydEbCQZ.cjs → just-B3mF2_pd.cjs} +1 -1
  72. package/dist/{just-tTSTWYrq.js → just-Db1lRieD.js} +5 -5
  73. package/dist/{latex-BUsA-DrV.js → latex-D73GL9tM.js} +1 -1
  74. package/dist/{latex-BQA9tWbc.cjs → latex-DyUb8gG5.cjs} +1 -1
  75. package/dist/{liquid-DkgygF2N.cjs → liquid-CztsYCRo.cjs} +1 -1
  76. package/dist/{liquid-cWkPgL2I.js → liquid-DF-tIke9.js} +4 -4
  77. package/dist/{lua-DIfY63me.cjs → lua-Cg38zrfV.cjs} +1 -1
  78. package/dist/{lua-C6iAvpwo.js → lua-xXZSPcKB.js} +2 -2
  79. package/dist/{marko-Du_QgmTX.cjs → marko-BuXulXH5.cjs} +1 -1
  80. package/dist/{marko-CNIbHKC5.js → marko-D-oNtuqC.js} +3 -3
  81. package/dist/{mdc-C_TsMRBC.cjs → mdc-CXK0bw8f.cjs} +1 -1
  82. package/dist/{mdc-DJo1F4gz.js → mdc-FTCNLElJ.js} +2 -2
  83. package/dist/{nginx-CwCNXVm0.js → nginx-C7lNZZWT.js} +1 -1
  84. package/dist/{nginx-B4IJHacq.cjs → nginx-Cgnh7QO-.cjs} +1 -1
  85. package/dist/{nim-rz8U_2RE.cjs → nim-D3q9r1nf.cjs} +1 -1
  86. package/dist/{nim-vPNRZtmW.js → nim-DIkmkdRK.js} +6 -6
  87. package/dist/{org-BJUQuLxI.cjs → org-Be03OJi3.cjs} +1 -1
  88. package/dist/{org-BXPZpMn-.js → org-CsORUXsX.js} +26 -26
  89. package/dist/{perl-C4EazcOb.js → perl-DzKqYwtD.js} +5 -5
  90. package/dist/{perl-CmADOgcQ.cjs → perl-jEpH5QnN.cjs} +1 -1
  91. package/dist/{php-BDdD_YcH.cjs → php-Cd0xNNLC.cjs} +1 -1
  92. package/dist/{php-BsJ_ZqPP.js → php-MrcCGNBb.js} +6 -6
  93. package/dist/{pug-BYXwQyBY.js → pug-7jPx_35W.js} +3 -3
  94. package/dist/{pug-BqCAm08K.cjs → pug-tintEWep.cjs} +1 -1
  95. package/dist/{qml-CnP9LpPo.js → qml-6QUtoeWA.js} +1 -1
  96. package/dist/{qml-BkLbkVNV.cjs → qml-BYzhsmnS.cjs} +1 -1
  97. package/dist/{r-Crbt2ian.cjs → r-Bwq_agHo.cjs} +1 -1
  98. package/dist/{r-BskJ0B3b.js → r-DSFCAodN.js} +1 -1
  99. package/dist/{razor-CcOw1mzY.cjs → razor-GHEO8ZrJ.cjs} +1 -1
  100. package/dist/{razor-CgobKwDB.js → razor-kZ9VMhh7.js} +1 -1
  101. package/dist/{regexp-CiRKg-B7.cjs → regexp-BTPSBkuS.cjs} +1 -1
  102. package/dist/{regexp-35oNQcei.js → regexp-C4cLyNsj.js} +1 -1
  103. package/dist/{rst-CcG1VyHf.js → rst-Celv7pzQ.js} +6 -6
  104. package/dist/{rst-0CCQrjRR.cjs → rst-DC596VAb.cjs} +1 -1
  105. package/dist/{ruby-D84T3HVs.js → ruby-CMUkZc4V.js} +12 -12
  106. package/dist/{ruby-Bz7pH067.cjs → ruby-CkA4Oavu.cjs} +1 -1
  107. package/dist/{sas-CfNVA6xj.cjs → sas-CWtZaM4y.cjs} +1 -1
  108. package/dist/{sas-leffZV-t.js → sas-DM3UyDFg.js} +1 -1
  109. package/dist/{scss-ConKgv_x.js → scss-Ce0za1qK.js} +2 -2
  110. package/dist/{scss-tjluFEfq.cjs → scss-LkHhJo18.cjs} +1 -1
  111. package/dist/{shellscript-CZORtZ0m.cjs → shellscript-CiDBNSSV.cjs} +1 -1
  112. package/dist/{shellscript-Cqrf74-d.js → shellscript-DfQbZbZv.js} +1 -1
  113. package/dist/{shellsession-DGN6o4H4.js → shellsession-B-u2-M3U.js} +1 -1
  114. package/dist/{shellsession-C7TZqfBX.cjs → shellsession-VJTxH3Qp.cjs} +1 -1
  115. package/dist/{soy-CYwevEyV.cjs → soy-CFhgAJvR.cjs} +1 -1
  116. package/dist/{soy-C3C1N4BB.js → soy-K4I5GWmG.js} +1 -1
  117. package/dist/{sql-CKAdltev.cjs → sql-CySKVD8Q.cjs} +1 -1
  118. package/dist/{sql-D28Ep8kC.js → sql-jusQgRuy.js} +1 -1
  119. package/dist/{stata-gwURYib4.cjs → stata-9iclJTGl.cjs} +1 -1
  120. package/dist/{stata-BdKZ3Tfa.js → stata-B0U1mDy6.js} +1 -1
  121. package/dist/{surrealql-BE3WUki2.js → surrealql-6aGCR7SU.js} +1 -1
  122. package/dist/{surrealql-CBAekr00.cjs → surrealql-DysQv_VH.cjs} +1 -1
  123. package/dist/{svelte-BIf-Q33W.js → svelte-B9UOpTL8.js} +3 -3
  124. package/dist/{svelte-ClXPqtyX.cjs → svelte-ZhIemg1n.cjs} +1 -1
  125. package/dist/{templ-CJJCQFkp.cjs → templ-BaKkruXs.cjs} +1 -1
  126. package/dist/{templ-KcJQzTCJ.js → templ-CwElXEjo.js} +2 -2
  127. package/dist/{tex-V7tXlPyb.cjs → tex-CwQDQCX_.cjs} +1 -1
  128. package/dist/{tex-DtiwEWZU.js → tex-kQHmfFkX.js} +2 -2
  129. package/dist/{ts-tags-gxidxB8R.js → ts-tags-BkcgKPdU.js} +7 -7
  130. package/dist/{ts-tags-BaSrqEU0.cjs → ts-tags-Dqqv0wS7.cjs} +1 -1
  131. package/dist/{tsx-yCIscrGd.cjs → tsx-TxRbY_MS.cjs} +1 -1
  132. package/dist/{tsx-CLVkAc-4.js → tsx-tC9_eQM0.js} +1 -1
  133. package/dist/{twig-DucNk_L2.cjs → twig-Dsriugp3.cjs} +1 -1
  134. package/dist/{twig-BozPiA4G.js → twig-zezbIase.js} +5 -5
  135. package/dist/types/src/components/Common/OAJSONEditor.vue.d.ts +2 -2
  136. package/dist/types/src/lib/parser/generateRequestBodyUi.d.ts +2 -1
  137. package/dist/types/src/lib/parser/generateResponseUi.d.ts +2 -1
  138. package/dist/types/src/lib/parser/getSchemaUi.d.ts +5 -1
  139. package/dist/types/src/lib/parser/mergeAllOf.d.ts +6 -0
  140. package/dist/types/src/lib/parser/resolveCircularRef.d.ts +1 -1
  141. package/dist/{typescript-CpL8J23X.cjs → typescript-BoS6yqrz.cjs} +1 -1
  142. package/dist/{typescript-v3MDZy3z.js → typescript-KKVXItRs.js} +1 -1
  143. package/dist/{typst-KAYwLgnz.cjs → typst-B_783qIn.cjs} +1 -1
  144. package/dist/{typst-BqVfAgGz.js → typst-DsM2L8MG.js} +29 -29
  145. package/dist/{useOpenapi-_Y6U0hd9.cjs → useOpenapi-DOw8b0Cm.cjs} +77 -77
  146. package/dist/{useOpenapi-B8TP2xUt.js → useOpenapi-tKaw3COz.js} +1482 -1442
  147. package/dist/vitepress-openapi.client.cjs.js +4 -4
  148. package/dist/vitepress-openapi.client.es.js +9044 -9040
  149. package/dist/vitepress-openapi.node.cjs.js +1 -1
  150. package/dist/vitepress-openapi.node.es.js +1 -1
  151. package/dist/{vue-CCBnDBrj.js → vue-B58kMEAQ.js} +6 -6
  152. package/dist/{vue-DDP4ToP4.cjs → vue-C-ymR46K.cjs} +1 -1
  153. package/dist/{vue-html-ChckxjrF.js → vue-html-B8vwCoQU.js} +1 -1
  154. package/dist/{vue-html-pUsOdCRA.cjs → vue-html-qs-Ree4R.cjs} +1 -1
  155. package/dist/{vue-vine-CW6_oew-.js → vue-vine-B95XCKNF.js} +3 -3
  156. package/dist/{vue-vine-C-R1ePqA.cjs → vue-vine-Nsb-fhNk.cjs} +1 -1
  157. package/dist/{xml-BjuxZasO.js → xml-B119d3-g.js} +2 -2
  158. package/dist/{xml-5oO_YDXi.cjs → xml-DzTGxXsy.cjs} +1 -1
  159. package/dist/{xsl-A-O9TTaY.js → xsl-CEz1476V.js} +1 -1
  160. package/dist/{xsl-BfGzl8_E.cjs → xsl-xWljPYeJ.cjs} +1 -1
  161. package/dist/{yaml-DgRJBItn.js → yaml-B9YlAzUe.js} +1 -1
  162. package/dist/{yaml-CFG4vyir.cjs → yaml-Cd_AUGXn.cjs} +1 -1
  163. package/package.json +1 -1
  164. package/src/components/Common/OAJSONEditor.vue +2 -1
  165. package/src/composables/useOpenapi.ts +2 -2
  166. package/src/composables/usePlayground.ts +4 -2
  167. package/src/lib/parser/dereferenceWithAnnotations.ts +22 -5
  168. package/src/lib/parser/generateRequestBodyUi.ts +3 -3
  169. package/src/lib/parser/generateResponseUi.ts +3 -3
  170. package/src/lib/parser/getSchemaUi.ts +50 -22
  171. package/src/lib/parser/mergeAllOf.ts +45 -0
  172. package/src/lib/parser/parseOpenapi.ts +12 -7
  173. package/src/lib/parser/resolveCircularRef.ts +25 -45
  174. package/src/lib/spec/createOpenApiSpec.ts +27 -51
@@ -0,0 +1,45 @@
1
+ import { merge } from 'allof-merge'
2
+
3
+ /**
4
+ * Merges `allOf` entries under `paths` and `components` without crawling the whole document.
5
+ * Merge a sparse copy holding just those entries, at their original paths so the cycle
6
+ * $refs allof-merge emits stay valid.
7
+ */
8
+ export function mergeAllOf(spec: any): any {
9
+ const groups = [['paths'], ...Object.keys(spec.components ?? {}).map(section => ['components', section])]
10
+ const entries = groups.flatMap(group => Object.entries(getIn(spec, group) ?? {})
11
+ .filter(([, entry]) => hasAllOf(entry))
12
+ .map(([name]) => [...group, name]))
13
+
14
+ if (!entries.length) {
15
+ return spec
16
+ }
17
+
18
+ const sparse = { openapi: spec.openapi }
19
+ entries.forEach(path => setIn(sparse, path, getIn(spec, path)))
20
+ const merged = merge(sparse, { source: spec })
21
+
22
+ const result = { ...spec }
23
+ entries.forEach(path => setIn(result, path, getIn(merged, path)))
24
+ return result
25
+ }
26
+
27
+ function hasAllOf(value: unknown): boolean {
28
+ if (!value || typeof value !== 'object') {
29
+ return false
30
+ }
31
+ return 'allOf' in value || Object.values(value).some(hasAllOf)
32
+ }
33
+
34
+ function getIn(value: any, path: string[]): any {
35
+ return path.reduce((node, key) => node?.[key], value)
36
+ }
37
+
38
+ // Shallow-copies containers along the path, so the input spec is never mutated.
39
+ function setIn(target: any, path: string[], value: unknown): void {
40
+ let node = target
41
+ for (const key of path.slice(0, -1)) {
42
+ node = node[key] = { ...node[key] }
43
+ }
44
+ node[path[path.length - 1]] = value
45
+ }
@@ -2,7 +2,6 @@ import type { JSONSchema } from '@trojs/openapi-dereference'
2
2
  import type { OpenAPIDocument, ParsedOpenAPI } from '../../types'
3
3
 
4
4
  import { $trycatch } from '@tszen/trycatch'
5
- import { merge } from 'allof-merge'
6
5
  import { parseSpec } from '../utils/parseSpec'
7
6
  import { dereferenceWithAnnotationsSync } from './dereferenceWithAnnotations'
8
7
  import { generateCodeSamples } from './generateCodeSamples'
@@ -12,6 +11,8 @@ import { generateMissingTags } from './generateMissingTags'
12
11
  import { generateRequestBodyUi } from './generateRequestBodyUi'
13
12
  import { generateResponseUi } from './generateResponseUi'
14
13
  import { generateSecurityUi } from './generateSecurityUi'
14
+ import { SchemaUiCache } from './getSchemaUi'
15
+ import { mergeAllOf } from './mergeAllOf'
15
16
 
16
17
  export function parseOpenapi() {
17
18
  function transformSync({
@@ -78,13 +79,14 @@ export function parseOpenapi() {
78
79
 
79
80
  let parsedSpec = { ...specContent } as ParsedOpenAPI
80
81
 
81
- const [mergedSpec, errMerge] = $trycatch(() => merge(
82
- transformSync({
82
+ const [mergedSpec, errMerge] = $trycatch(() => {
83
+ const transformed = transformSync({
83
84
  spec,
84
85
  defaultTag,
85
86
  defaultTagDescription,
86
- }),
87
- ) as ParsedOpenAPI)
87
+ })
88
+ return mergeAllOf(transformed) as ParsedOpenAPI
89
+ })
88
90
  parsedSpec = errMerge ? parsedSpec : mergedSpec
89
91
 
90
92
  const [dereferencedSpec, errDereference] = $trycatch(() => dereferenceWithAnnotationsSync(parsedSpec as JSONSchema) as ParsedOpenAPI)
@@ -93,10 +95,13 @@ export function parseOpenapi() {
93
95
  const [securitySpec, errSecurity] = $trycatch(() => generateSecurityUi(parsedSpec))
94
96
  parsedSpec = errSecurity ? parsedSpec : securitySpec
95
97
 
96
- const [requestBodySpec, errRequestBody] = $trycatch(() => generateRequestBodyUi(parsedSpec))
98
+ // Requests and responses share schemas: convert each once per parse.
99
+ const schemaUiCache = new SchemaUiCache()
100
+
101
+ const [requestBodySpec, errRequestBody] = $trycatch(() => generateRequestBodyUi(parsedSpec, schemaUiCache))
97
102
  parsedSpec = errRequestBody ? parsedSpec : requestBodySpec
98
103
 
99
- const [responseSpec, errResponse] = $trycatch(() => generateResponseUi(parsedSpec))
104
+ const [responseSpec, errResponse] = $trycatch(() => generateResponseUi(parsedSpec, schemaUiCache))
100
105
  parsedSpec = errResponse ? parsedSpec : responseSpec
101
106
 
102
107
  parsedSpec.externalDocs = parsedSpec.externalDocs || parsedSpec.externalDocs || {}
@@ -7,19 +7,22 @@ interface SchemaNode {
7
7
  parent: SchemaNode | null
8
8
  }
9
9
 
10
- export function resolveCircularRef(schema: OpenAPI.SchemaObject): OpenAPI.SchemaObject {
11
- const rootNode: SchemaNode = {
12
- key: 'root',
13
- value: schema,
14
- parent: null,
15
- }
16
-
17
- traverseNode(rootNode)
10
+ // `done` holds nodes already walked. Pass the same set across calls within one parse
11
+ // (operations reference the same dereferenced schemas) to walk each node only once.
12
+ export function resolveCircularRef(
13
+ schema: OpenAPI.SchemaObject,
14
+ done = new WeakSet<object>(),
15
+ ): OpenAPI.SchemaObject {
16
+ traverseNode({ key: 'root', value: schema, parent: null }, new Map(), done)
18
17
 
19
18
  return schema
20
19
  }
21
20
 
22
- function traverseNode(node: SchemaNode): void {
21
+ // Depth-first walk that cuts back edges (a node whose origin is already on the
22
+ // ancestor stack). Nodes already fully walked are skipped: removing every back
23
+ // edge found by a DFS leaves an acyclic graph, so shared schemas don't need to be
24
+ // re-expanded per path (which is exponential on specs like Stripe's).
25
+ function traverseNode(node: SchemaNode, ancestors: Map<unknown, SchemaNode>, done: WeakSet<object>): void {
23
26
  const { value } = node
24
27
 
25
28
  if (typeof value !== 'object' || value === null) {
@@ -27,51 +30,28 @@ function traverseNode(node: SchemaNode): void {
27
30
  return
28
31
  }
29
32
 
30
- const circularReference = detectCircularReference(node.parent, value)
33
+ const origin = (value as any)[originSymbol] ?? value
34
+ const ancestor = ancestors.get(origin)
31
35
 
32
- if (circularReference) {
36
+ if (ancestor) {
33
37
  // Replace the circular reference with a descriptor object.
34
38
  node.parent!.value[node.key] = {
35
39
  type: 'object',
36
- circularReference,
37
- }
38
- } else {
39
- // Recursively traverse child nodes.
40
- for (const [key, childValue] of Object.entries(value)) {
41
- const childNode: SchemaNode = {
42
- key,
43
- value: childValue,
44
- parent: node,
45
- }
46
-
47
- traverseNode(childNode)
40
+ circularReference: buildReferencePath(ancestor),
48
41
  }
42
+ return
49
43
  }
50
- }
51
44
 
52
- function detectCircularReference(
53
- ancestor: SchemaNode | null,
54
- value: any,
55
- ): string | null {
56
- const target
57
- = value && (value as any)[originSymbol]
58
- ? (value as any)[originSymbol]
59
- : value
60
-
61
- while (ancestor) {
62
- const ancestorValue = ancestor.value
63
- const ancestorTarget
64
- = ancestorValue && (ancestorValue as any)[originSymbol]
65
- ? (ancestorValue as any)[originSymbol]
66
- : ancestorValue
67
-
68
- if (ancestorTarget === target) {
69
- return buildReferencePath(ancestor)
70
- }
71
- ancestor = ancestor.parent
45
+ if (done.has(value)) {
46
+ return
72
47
  }
73
48
 
74
- return null
49
+ ancestors.set(origin, node)
50
+ for (const [key, childValue] of Object.entries(value)) {
51
+ traverseNode({ key, value: childValue, parent: node }, ancestors, done)
52
+ }
53
+ ancestors.delete(origin)
54
+ done.add(value)
75
55
  }
76
56
 
77
57
  function buildReferencePath(node: SchemaNode | null): string {
@@ -32,9 +32,13 @@ export function createOpenApiSpec(options: {
32
32
  } = {}): OpenApiSpecInstance {
33
33
  let innerSpec: OpenAPIDocument | null = null
34
34
  let innerOriginalSpec: OpenAPIDocument | null = options.originalSpec ?? null
35
+ // operationId -> location, built on first lookup so repeated lookups (one per
36
+ // rendered operation, per operation link) don't rescan every path and verb.
37
+ let operationIndex: Map<string, { path: string, method: string, operation: any }> | null = null
35
38
 
36
39
  function setSpec(spec: OpenAPIDocument) {
37
40
  innerSpec = spec
41
+ operationIndex = null
38
42
  }
39
43
 
40
44
  function getSpec(): OpenAPIDocument {
@@ -55,53 +59,32 @@ export function createOpenApiSpec(options: {
55
59
  innerOriginalSpec = spec
56
60
  }
57
61
 
58
- function findOperation(paths: OpenAPIV3.PathsObject, operationId: string) {
59
- for (const path of Object.values(paths)) {
60
- for (const verb of httpVerbs) {
61
- if (path && path[verb]?.operationId === operationId) {
62
- return path[verb]
62
+ function findOperation(operationId: string) {
63
+ if (!operationIndex) {
64
+ operationIndex = new Map()
65
+ for (const [path, methods] of Object.entries(getPaths())) {
66
+ for (const verb of httpVerbs) {
67
+ const operation = methods?.[verb]
68
+ // First match wins, like the linear scan this replaces.
69
+ if (operation?.operationId !== undefined && !operationIndex.has(operation.operationId)) {
70
+ operationIndex.set(operation.operationId, { path, method: verb, operation })
71
+ }
63
72
  }
64
73
  }
65
74
  }
66
- return null
75
+ return operationIndex.get(operationId) ?? null
67
76
  }
68
77
 
69
78
  function getOperation(operationId: string) {
70
- const paths = getSpec().paths as OpenAPIV3.PathsObject
71
- if (!paths) {
72
- return null
73
- }
74
- return findOperation(paths, operationId)
79
+ return findOperation(operationId)?.operation ?? null
75
80
  }
76
81
 
77
82
  function getOperationPath(operationId: string) {
78
- const paths = getSpec().paths as OpenAPIV3.PathsObject
79
- if (!paths) {
80
- return null
81
- }
82
- for (const [path, methods] of Object.entries(paths)) {
83
- for (const verb of httpVerbs) {
84
- if (methods && methods[verb]?.operationId === operationId) {
85
- return path
86
- }
87
- }
88
- }
89
- return null
83
+ return findOperation(operationId)?.path ?? null
90
84
  }
91
85
 
92
86
  function getOperationMethod(operationId: string) {
93
- const paths = getSpec().paths as OpenAPIV3.PathsObject
94
- if (!paths) {
95
- return null
96
- }
97
- for (const path of Object.values(paths)) {
98
- for (const verb of httpVerbs) {
99
- if (path && path[verb]?.operationId === operationId) {
100
- return verb
101
- }
102
- }
103
- }
104
- return null
87
+ return findOperation(operationId)?.method ?? null
105
88
  }
106
89
 
107
90
  function getOperationParameters(operationId: string) {
@@ -152,13 +135,12 @@ export function createOpenApiSpec(options: {
152
135
  }
153
136
 
154
137
  function getOperationServers(operationId: string): OpenAPIV3.ServerObject[] {
155
- const operation = findOperation(getPaths(), operationId)
156
- if (!operation) {
138
+ const entry = findOperation(operationId)
139
+ if (!entry) {
157
140
  return []
158
141
  }
159
- const operationPath = getOperationPath(operationId)
160
- const paths = getSpec().paths as OpenAPIV3.PathsObject
161
- const pathServers = paths[(operationPath ?? '')]?.servers
142
+ const operation = entry.operation
143
+ const pathServers = getPaths()[entry.path]?.servers
162
144
 
163
145
  if (operation?.servers !== undefined) {
164
146
  return operation.servers as OpenAPIV3.ServerObject[]
@@ -173,19 +155,13 @@ export function createOpenApiSpec(options: {
173
155
  if (!getSpec().paths) {
174
156
  return []
175
157
  }
176
- const paths = getSpec().paths as OpenAPIV3.PathsObject
177
- return Object.values(paths).reduce((tags: string[], path) => {
158
+ const tags = new Set<string>()
159
+ for (const path of Object.values(getPaths())) {
178
160
  for (const verb of httpVerbs) {
179
- if (path && path[verb]?.tags) {
180
- path[verb].tags.forEach((tag: string) => {
181
- if (!tags.includes(tag)) {
182
- tags.push(tag)
183
- }
184
- })
185
- }
161
+ path?.[verb]?.tags?.forEach((tag: string) => tags.add(tag))
186
162
  }
187
- return tags
188
- }, [])
163
+ }
164
+ return [...tags]
189
165
  }
190
166
 
191
167
  function filterPaths(predicate: (operation: any) => boolean) {