@voxgig/apidef 8.15.2 → 8.17.0
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/dist/apidef.js +15 -0
- package/dist/apidef.js.map +1 -1
- package/dist/builder/entity/entity.js +8 -5
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/builder/entity/info.js +1 -1
- package/dist/builder/entity/info.js.map +1 -1
- package/dist/builder/flow.js +3 -3
- package/dist/builder/flow.js.map +1 -1
- package/dist/cli.js +8 -10
- package/dist/cli.js.map +1 -1
- package/dist/desc.d.ts +2 -0
- package/dist/guide/guide.d.ts +14 -1
- package/dist/guide/guide.js +106 -62
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +9 -1
- package/dist/guide/heuristic01.js +290 -71
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/parse.d.ts +4 -1
- package/dist/parse.js +183 -133
- package/dist/parse.js.map +1 -1
- package/dist/refcount.d.ts +6 -0
- package/dist/refcount.js +215 -0
- package/dist/refcount.js.map +1 -0
- package/dist/transform/entity.d.ts +1 -1
- package/dist/transform/entity.js +12 -3
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.js +1 -1
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/operation.js +1 -1
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/top.js +1 -1
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +3 -1
- package/dist/utility.js +85 -6
- package/dist/utility.js.map +1 -1
- package/model/{guide.aon → guide.aontu} +1 -1
- package/package.json +3 -3
- package/src/apidef.ts +20 -1
- package/src/builder/entity/entity.ts +8 -5
- package/src/builder/entity/info.ts +1 -1
- package/src/builder/flow.ts +3 -3
- package/src/cli.ts +8 -10
- package/src/desc.ts +3 -0
- package/src/guide/guide.ts +136 -71
- package/src/guide/heuristic01.ts +358 -90
- package/src/parse.ts +221 -126
- package/src/refcount.ts +244 -0
- package/src/transform/entity.ts +16 -5
- package/src/transform/field.ts +1 -1
- package/src/transform/operation.ts +1 -1
- package/src/transform/top.ts +1 -1
- package/src/types.ts +2 -2
- package/src/utility.ts +102 -6
- /package/model/{apidef.aon → apidef.aontu} +0 -0
package/src/guide/heuristic01.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { each } from 'jostraca'
|
|
|
8
8
|
import { size, merge, getelem, isempty, items, keysof } from '@voxgig/struct'
|
|
9
9
|
|
|
10
10
|
import {
|
|
11
|
-
isEntityWrapperProp, envelopeProp, closedBodyTransform,
|
|
11
|
+
isEntityWrapperProp, envelopeProp, envelopeItemRef, closedBodyTransform,
|
|
12
12
|
authExchangeOp, specSecuredByDefault,
|
|
13
13
|
} from '../utility'
|
|
14
14
|
|
|
@@ -66,6 +66,8 @@ import { snakify } from 'jostraca'
|
|
|
66
66
|
|
|
67
67
|
import { mergeCollectionPaths } from '../transform/entity'
|
|
68
68
|
|
|
69
|
+
import { byCodePoint, countRefs, satAdd } from '../refcount'
|
|
70
|
+
|
|
69
71
|
import type {
|
|
70
72
|
PathMatch
|
|
71
73
|
} from '../utility'
|
|
@@ -77,8 +79,8 @@ const KONSOLE_LOG = console['log']
|
|
|
77
79
|
// Log non - fatal wierdness.
|
|
78
80
|
const dlog = getdlog('apidef', __filename)
|
|
79
81
|
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
+
// A schema whose per-use occurrences, over the method count or over the path
|
|
83
|
+
// count, fall below these rates names an entity rather than a shared shape.
|
|
82
84
|
const IS_ENTCMP_METHOD_RATE = 0.21
|
|
83
85
|
const IS_ENTCMP_PATH_RATE = 0.41
|
|
84
86
|
|
|
@@ -98,6 +100,16 @@ const METHOD_IDOP: Record<string, string> = {
|
|
|
98
100
|
OPTIONS: 'OPTIONS',
|
|
99
101
|
}
|
|
100
102
|
|
|
103
|
+
// Tried in order: the first shape a path matches decides how its entity is named.
|
|
104
|
+
const ENTITY_PATH_SHAPES = ['t/p/t/', 't/p/', 'p/t/', 't/', 't/p/p']
|
|
105
|
+
|
|
106
|
+
// The matched part that gives each shape its name.
|
|
107
|
+
const PATH_NAME_INDEX: Record<string, number> = {
|
|
108
|
+
't/p/t/': 2, 't/p/': 0, 'p/t/': 1, 't/': 0, 't/p/p': 0,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const READ_METHODS = ['GET', 'QUERY', 'HEAD', 'OPTIONS']
|
|
112
|
+
|
|
101
113
|
const METHOD_CONSIDER_ORDER: Record<string, number> = {
|
|
102
114
|
'GET': 100,
|
|
103
115
|
'QUERY': 150,
|
|
@@ -123,6 +135,10 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
123
135
|
]
|
|
124
136
|
},
|
|
125
137
|
{ select: selectCmpXrefs, apply: MeasureRef },
|
|
138
|
+
{ select: selectAllMethods, apply: MeasureEnvelope },
|
|
139
|
+
MeasureEnvelopeItems,
|
|
140
|
+
{ select: selectAllMethods, apply: MeasureSharing },
|
|
141
|
+
MeasureShared,
|
|
126
142
|
{
|
|
127
143
|
select: selectAllMethods, apply: [
|
|
128
144
|
ResolveEntityComponent,
|
|
@@ -145,7 +161,7 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
145
161
|
|
|
146
162
|
const guide = result.data.guide
|
|
147
163
|
|
|
148
|
-
mergeCollectionPaths(guide, ctx.log)
|
|
164
|
+
guide.metrics.count.entity -= mergeCollectionPaths(guide, ctx.log).length
|
|
149
165
|
|
|
150
166
|
const metrics = guide.metrics
|
|
151
167
|
|
|
@@ -206,6 +222,8 @@ function Prepare(spec: TaskSpec) {
|
|
|
206
222
|
work: {
|
|
207
223
|
pathmap: {},
|
|
208
224
|
entmap: {},
|
|
225
|
+
envelope: {},
|
|
226
|
+
sharing: { routes: [], records: {}, yields: {} },
|
|
209
227
|
entity: {
|
|
210
228
|
count: {
|
|
211
229
|
seen: 0,
|
|
@@ -279,10 +297,11 @@ function PreparePath(spec: TaskSpec) {
|
|
|
279
297
|
|
|
280
298
|
|
|
281
299
|
function selectCmpXrefs(_source: any, spec: TaskSpec) {
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
300
|
+
const counts = countRefs(spec.ctx.def)
|
|
301
|
+
return Object.keys(counts)
|
|
302
|
+
.sort(byCodePoint)
|
|
303
|
+
.filter(val => val.match(/\/(components\/schemas|definitions)\//))
|
|
304
|
+
.map(val => ({ val, count: counts[val] }))
|
|
286
305
|
}
|
|
287
306
|
|
|
288
307
|
|
|
@@ -290,14 +309,16 @@ function MeasureRef(spec: TaskSpec) {
|
|
|
290
309
|
const guide = spec.data.guide
|
|
291
310
|
const metrics = guide.metrics
|
|
292
311
|
|
|
293
|
-
|
|
312
|
+
const xref = spec.node.val
|
|
313
|
+
let m = xref.val.match(/\/(components\/schemas|definitions)\/(.+)$/)
|
|
294
314
|
if (m) {
|
|
295
315
|
const name = canonizeCmpName(m[2])
|
|
296
316
|
if (null == metrics.count.origcmprefs[name]) {
|
|
297
317
|
metrics.count.cmp++
|
|
298
318
|
metrics.count.origcmprefs[name] = 0
|
|
299
319
|
}
|
|
300
|
-
metrics.count.origcmprefs[name]
|
|
320
|
+
metrics.count.origcmprefs[name] =
|
|
321
|
+
satAdd(metrics.count.origcmprefs[name], xref.count)
|
|
301
322
|
|
|
302
323
|
if (null == metrics.found.cmp[name]) {
|
|
303
324
|
metrics.found.cmp[name] = { orig: m[2] }
|
|
@@ -306,6 +327,74 @@ function MeasureRef(spec: TaskSpec) {
|
|
|
306
327
|
}
|
|
307
328
|
|
|
308
329
|
|
|
330
|
+
// Being an envelope belongs to the component, not to one operation: it names
|
|
331
|
+
// through the record it carries only when every operation answering with it
|
|
332
|
+
// unwraps it, so the operations on one resource are never split between the
|
|
333
|
+
// record's name and the envelope's. An operation unwraps only the response
|
|
334
|
+
// ResolveTransform reads. The entry is the item's reference, or '' for none.
|
|
335
|
+
function MeasureEnvelope(spec: TaskSpec) {
|
|
336
|
+
const work = spec.data.work
|
|
337
|
+
const mdesc = spec.node.val
|
|
338
|
+
const opname = methodOpname(mdesc, matchEntityPath(work.pathmap[mdesc.path].parts), [])
|
|
339
|
+
const unwrapref = getResponseSchema(successResponse(mdesc.responses))?.['x-ref']
|
|
340
|
+
|
|
341
|
+
for (const schema of successSchemas(mdesc.responses)) {
|
|
342
|
+
const xref = schema['x-ref']
|
|
343
|
+
if (null != xref) {
|
|
344
|
+
const itemref = null == opname || xref !== unwrapref ? null :
|
|
345
|
+
envelopeItemRef(schema, opname)
|
|
346
|
+
work.envelope[xref] = '' === work.envelope[xref] || null == itemref ? '' : itemref
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
// An item carried by more than one envelope is named by none of them: the
|
|
353
|
+
// envelopes' own names are then what tell the resources apart.
|
|
354
|
+
function MeasureEnvelopeItems(spec: TaskSpec) {
|
|
355
|
+
const envelope: Record<string, string> = spec.data.work.envelope
|
|
356
|
+
const carriers: Record<string, number> = {}
|
|
357
|
+
for (const itemref of Object.values(envelope)) {
|
|
358
|
+
carriers[itemref] = (carriers[itemref] ?? 0) + 1
|
|
359
|
+
}
|
|
360
|
+
for (const xref of Object.keys(envelope)) {
|
|
361
|
+
if (1 < carriers[envelope[xref]]) {
|
|
362
|
+
envelope[xref] = ''
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
// Records every route whose operation answers with a response component, and
|
|
369
|
+
// whether that component declares an `id`, before any method is named.
|
|
370
|
+
function MeasureSharing(spec: TaskSpec) {
|
|
371
|
+
const work = spec.data.work
|
|
372
|
+
const mdesc = spec.node.val
|
|
373
|
+
const parts: string[] = work.pathmap[mdesc.path].parts
|
|
374
|
+
const op = methodOpname(mdesc, matchEntityPath(parts), []) ?? ''
|
|
375
|
+
const sharing = work.sharing
|
|
376
|
+
|
|
377
|
+
const xrefs = findPotentialSchemaRefs(mdesc.path, mdesc.method, mdesc.responses, work.envelope, [])
|
|
378
|
+
for (const xref of xrefs) {
|
|
379
|
+
const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/)
|
|
380
|
+
if (null != m) {
|
|
381
|
+
const cmp = canonizeCmpName(m[2])
|
|
382
|
+
sharing.routes.push({ cmp, method: mdesc.method, path: mdesc.path, op })
|
|
383
|
+
sharing.records[cmp] = true === sharing.records[cmp] || declaresId(refSchema(spec.data.def, xref))
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
function MeasureShared(spec: TaskSpec) {
|
|
390
|
+
const sharing = spec.data.work.sharing
|
|
391
|
+
const records = Object.keys(sharing.records).filter((cmp) => sharing.records[cmp])
|
|
392
|
+
for (const key of sharedRoutes(sharing.routes, records)) {
|
|
393
|
+
sharing.yields[key] = true
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
|
|
309
398
|
function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
|
|
310
399
|
const ctx = spec.ctx
|
|
311
400
|
|
|
@@ -316,6 +405,12 @@ function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
|
|
|
316
405
|
for (const [m, mdef] of sortedEntries(pdef)) {
|
|
317
406
|
const method = m.toUpperCase()
|
|
318
407
|
|
|
408
|
+
// A path item also holds parameters, servers, summary and the like.
|
|
409
|
+
if (null == METHOD_CONSIDER_ORDER[method] ||
|
|
410
|
+
null == mdef || 'object' !== typeof mdef || Array.isArray(mdef)) {
|
|
411
|
+
continue
|
|
412
|
+
}
|
|
413
|
+
|
|
319
414
|
caught.methods.push({
|
|
320
415
|
path,
|
|
321
416
|
method,
|
|
@@ -370,9 +465,10 @@ function ResolveEntityComponent(spec: TaskSpec) {
|
|
|
370
465
|
|
|
371
466
|
let responses = methodDef.responses
|
|
372
467
|
|
|
373
|
-
let origxrefs: any[] = findPotentialSchemaRefs(
|
|
374
|
-
val
|
|
375
|
-
|
|
468
|
+
let origxrefs: any[] = findPotentialSchemaRefs(
|
|
469
|
+
pathStr, methodName, responses, work.envelope, why_cmp).map(val => ({
|
|
470
|
+
val
|
|
471
|
+
}))
|
|
376
472
|
|
|
377
473
|
let cmpxrefs = origxrefs
|
|
378
474
|
.filter(xref => xref.val.includes('schema') || xref.val.includes('definitions'))
|
|
@@ -487,6 +583,8 @@ function ResolveEntityComponent(spec: TaskSpec) {
|
|
|
487
583
|
(pathStr.includes('/' + ftag + '/') || pathStr.includes('/' + tagdesc.canon + '/'))
|
|
488
584
|
&& out.cmp !== tagdesc.canon
|
|
489
585
|
) {
|
|
586
|
+
const rescmp = out.cmp
|
|
587
|
+
const rescmpoccur = metrics.count.origcmprefs[out.origcmpref ?? ''] ?? 0
|
|
490
588
|
out = makeMethodEntityDesc({
|
|
491
589
|
ref: 'tag',
|
|
492
590
|
cmp: tagdesc.canon,
|
|
@@ -494,6 +592,8 @@ function ResolveEntityComponent(spec: TaskSpec) {
|
|
|
494
592
|
why_cmp,
|
|
495
593
|
entname: tagdesc.canon,
|
|
496
594
|
})
|
|
595
|
+
out.rescmp = rescmp
|
|
596
|
+
out.rescmpoccur = rescmpoccur
|
|
497
597
|
why_cmp.push('tag/path=' + out.cmp)
|
|
498
598
|
}
|
|
499
599
|
}
|
|
@@ -544,25 +644,25 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
544
644
|
|
|
545
645
|
let entname
|
|
546
646
|
|
|
547
|
-
|
|
647
|
+
const pm = matchEntityPath(parts)
|
|
548
648
|
|
|
549
|
-
if (
|
|
649
|
+
if ('t/p/t/' === pm?.expr) {
|
|
550
650
|
entname = entityPathMatch_tpte(data, pm, mdesc, why_path)
|
|
551
651
|
}
|
|
552
652
|
|
|
553
|
-
else if (
|
|
653
|
+
else if ('t/p/' === pm?.expr) {
|
|
554
654
|
entname = entityPathMatch_tpe(data, pm, mdesc, why_path)
|
|
555
655
|
}
|
|
556
656
|
|
|
557
|
-
else if (
|
|
657
|
+
else if ('p/t/' === pm?.expr) {
|
|
558
658
|
entname = entityPathMatch_pte(data, pm, mdesc, why_path)
|
|
559
659
|
}
|
|
560
660
|
|
|
561
|
-
else if (
|
|
661
|
+
else if ('t/' === pm?.expr) {
|
|
562
662
|
entname = entityPathMatch_te(data, pm, mdesc, why_path)
|
|
563
663
|
}
|
|
564
664
|
|
|
565
|
-
else if (
|
|
665
|
+
else if ('t/p/p' === pm?.expr) {
|
|
566
666
|
entname = entityPathMatch_tpp(data, pm, mdesc, why_path)
|
|
567
667
|
}
|
|
568
668
|
|
|
@@ -624,8 +724,6 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
624
724
|
function RenameParams(spec: TaskSpec) {
|
|
625
725
|
const ctx = spec.ctx
|
|
626
726
|
const data = spec.data
|
|
627
|
-
const guide = data.guide
|
|
628
|
-
const metrics = guide.metrics
|
|
629
727
|
|
|
630
728
|
const mdesc = spec.node.val
|
|
631
729
|
const ment = mdesc.MethodEntity
|
|
@@ -678,12 +776,6 @@ function RenameParams(spec: TaskSpec) {
|
|
|
678
776
|
return
|
|
679
777
|
}
|
|
680
778
|
|
|
681
|
-
const cmpname = mdesc.cmp
|
|
682
|
-
const considerCmp =
|
|
683
|
-
null != cmpname &&
|
|
684
|
-
0 < metrics.count.uniqschema &&
|
|
685
|
-
mdesc.method_rate < IS_ENTCMP_METHOD_RATE
|
|
686
|
-
|
|
687
779
|
const origParams = []
|
|
688
780
|
|
|
689
781
|
for (let partI = 0; partI < parts.length; partI++) {
|
|
@@ -734,14 +826,8 @@ function RenameParams(spec: TaskSpec) {
|
|
|
734
826
|
// actually an action
|
|
735
827
|
if (
|
|
736
828
|
secondLastPart
|
|
737
|
-
&&
|
|
738
|
-
|
|
739
|
-
parentName !== entdesc.name
|
|
740
|
-
&& entdesc.name.startsWith(parentName + '_')
|
|
741
|
-
)
|
|
742
|
-
// || parentName === cmp.name
|
|
743
|
-
|| parentName === cmpname
|
|
744
|
-
)
|
|
829
|
+
&& parentName !== entdesc.name
|
|
830
|
+
&& entdesc.name.startsWith(parentName + '_')
|
|
745
831
|
) {
|
|
746
832
|
updateParamRename(
|
|
747
833
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
@@ -752,13 +838,6 @@ function RenameParams(spec: TaskSpec) {
|
|
|
752
838
|
parts[partI + 1], entdesc, pathDesc, 'action-not-parent')
|
|
753
839
|
}
|
|
754
840
|
|
|
755
|
-
else if (hasParent && parentName === cmpname) {
|
|
756
|
-
updateParamRename(
|
|
757
|
-
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
758
|
-
'id', 'id-parent-cmp')
|
|
759
|
-
why.push('id-parent-cmp')
|
|
760
|
-
}
|
|
761
|
-
|
|
762
841
|
else if (hasParent && parentName === entdesc.name) {
|
|
763
842
|
updateParamRename(
|
|
764
843
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
@@ -789,12 +868,10 @@ function RenameParams(spec: TaskSpec) {
|
|
|
789
868
|
|| entdesc.name.endsWith('_' + parentName)
|
|
790
869
|
)
|
|
791
870
|
)
|
|
792
|
-
&& (!considerCmp || cmpname === entdesc.name)
|
|
793
871
|
) {
|
|
794
872
|
updateParamRename(
|
|
795
873
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
796
|
-
'id', 'end-id;' + methodName + ';parent=' + hasParent + '/' + parentName
|
|
797
|
-
';cmp=' + considerCmp + (null == cmpname ? '' : '/' + cmpname))
|
|
874
|
+
'id', 'end-id;' + methodName + ';parent=' + hasParent + '/' + parentName)
|
|
798
875
|
why.push('end-id')
|
|
799
876
|
}
|
|
800
877
|
|
|
@@ -831,18 +908,6 @@ function RenameParams(spec: TaskSpec) {
|
|
|
831
908
|
}
|
|
832
909
|
}
|
|
833
910
|
|
|
834
|
-
// Primary ent id not at end!
|
|
835
|
-
else if (
|
|
836
|
-
hasParent
|
|
837
|
-
&& parentName === cmpname
|
|
838
|
-
) {
|
|
839
|
-
updateParamRename(
|
|
840
|
-
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
841
|
-
'id', 'id-not-last')
|
|
842
|
-
|
|
843
|
-
why.push('id-not-last')
|
|
844
|
-
}
|
|
845
|
-
|
|
846
911
|
// Not primary ent.
|
|
847
912
|
else {
|
|
848
913
|
why.push('default')
|
|
@@ -880,9 +945,6 @@ function RenameParams(spec: TaskSpec) {
|
|
|
880
945
|
parentName,
|
|
881
946
|
not_exact_id,
|
|
882
947
|
probably_an_id,
|
|
883
|
-
considerCmp,
|
|
884
|
-
cmp: mdesc.cmp,
|
|
885
|
-
cmpname,
|
|
886
948
|
paramRenameCapture,
|
|
887
949
|
entdesc
|
|
888
950
|
}
|
|
@@ -1004,7 +1066,7 @@ function ResolveOperation(spec: TaskSpec) {
|
|
|
1004
1066
|
|
|
1005
1067
|
|
|
1006
1068
|
if ('load' === standard_opname) {
|
|
1007
|
-
const islist = isListResponse(mdesc, pathStr, why_op)
|
|
1069
|
+
const islist = isListResponse(mdesc, ment.pm, pathStr, why_op)
|
|
1008
1070
|
opname = islist ? 'list' : opname
|
|
1009
1071
|
}
|
|
1010
1072
|
|
|
@@ -1074,8 +1136,7 @@ function ResolveTransform(spec: TaskSpec) {
|
|
|
1074
1136
|
res: undefined,
|
|
1075
1137
|
}
|
|
1076
1138
|
|
|
1077
|
-
const
|
|
1078
|
-
const resprops = getResponseSchema(resokdef)?.properties
|
|
1139
|
+
const resprops = getResponseSchema(successResponse(mdesc.responses))?.properties
|
|
1079
1140
|
debugpath(pathStr, methodName, 'TRANSFORM-RES', keysof(resprops))
|
|
1080
1141
|
|
|
1081
1142
|
if (resprops) {
|
|
@@ -1190,7 +1251,7 @@ function entityPathMatch_tpte(
|
|
|
1190
1251
|
) {
|
|
1191
1252
|
const ment = mdesc.MethodEntity
|
|
1192
1253
|
|
|
1193
|
-
const pathNameIndex =
|
|
1254
|
+
const pathNameIndex = PATH_NAME_INDEX['t/p/t/']
|
|
1194
1255
|
|
|
1195
1256
|
why.push('path=t/p/t/')
|
|
1196
1257
|
const origPathName = pm[pathNameIndex]
|
|
@@ -1252,12 +1313,12 @@ function verbOnParent(
|
|
|
1252
1313
|
mdesc: any,
|
|
1253
1314
|
): null | string {
|
|
1254
1315
|
const method = mdesc.method
|
|
1255
|
-
if (
|
|
1316
|
+
if (READ_METHODS.includes(method)) {
|
|
1256
1317
|
return null
|
|
1257
1318
|
}
|
|
1258
1319
|
|
|
1259
1320
|
const ment = mdesc.MethodEntity
|
|
1260
|
-
if (1 < (ment.cmpoccur ?? 0)) {
|
|
1321
|
+
if (1 < (ment.rescmpoccur ?? ment.cmpoccur ?? 0)) {
|
|
1261
1322
|
return null
|
|
1262
1323
|
}
|
|
1263
1324
|
|
|
@@ -1267,7 +1328,7 @@ function verbOnParent(
|
|
|
1267
1328
|
}
|
|
1268
1329
|
|
|
1269
1330
|
const verb = canonize(getelem(pm, -1))
|
|
1270
|
-
const cmp = String(ment.cmp ?? '')
|
|
1331
|
+
const cmp = String(ment.rescmp ?? ment.cmp ?? '')
|
|
1271
1332
|
if ('' === verb || cmp === verb || cmp.endsWith('_' + verb)) {
|
|
1272
1333
|
return null
|
|
1273
1334
|
}
|
|
@@ -1326,7 +1387,7 @@ function entityPathMatch_tpe(
|
|
|
1326
1387
|
pm: PathMatch, mdesc: any, why: string[]
|
|
1327
1388
|
) {
|
|
1328
1389
|
const ment = mdesc.MethodEntity
|
|
1329
|
-
const pathNameIndex =
|
|
1390
|
+
const pathNameIndex = PATH_NAME_INDEX['t/p/']
|
|
1330
1391
|
|
|
1331
1392
|
why.push('path=t/p/')
|
|
1332
1393
|
const origPathName = pm[pathNameIndex]
|
|
@@ -1349,7 +1410,7 @@ function entityPathMatch_pte(
|
|
|
1349
1410
|
pm: PathMatch, mdesc: any, why: string[]
|
|
1350
1411
|
) {
|
|
1351
1412
|
const ment = mdesc.MethodEntity
|
|
1352
|
-
const pathNameIndex =
|
|
1413
|
+
const pathNameIndex = PATH_NAME_INDEX['p/t/']
|
|
1353
1414
|
|
|
1354
1415
|
why.push('path=p/t/')
|
|
1355
1416
|
const origPathName = pm[pathNameIndex]
|
|
@@ -1372,7 +1433,7 @@ function entityPathMatch_te(
|
|
|
1372
1433
|
pm: PathMatch, mdesc: any, why: string[]
|
|
1373
1434
|
) {
|
|
1374
1435
|
const ment = mdesc.MethodEntity
|
|
1375
|
-
const pathNameIndex =
|
|
1436
|
+
const pathNameIndex = PATH_NAME_INDEX['t/']
|
|
1376
1437
|
|
|
1377
1438
|
why.push('path=t/')
|
|
1378
1439
|
const origPathName = pm[pathNameIndex]
|
|
@@ -1395,7 +1456,7 @@ function entityPathMatch_tpp(
|
|
|
1395
1456
|
pm: PathMatch, mdesc: any, why: string[]
|
|
1396
1457
|
) {
|
|
1397
1458
|
const ment = mdesc.MethodEntity
|
|
1398
|
-
const pathNameIndex =
|
|
1459
|
+
const pathNameIndex = PATH_NAME_INDEX['t/p/p']
|
|
1399
1460
|
|
|
1400
1461
|
why.push('path=t/p/p')
|
|
1401
1462
|
const origPathName = pm[pathNameIndex]
|
|
@@ -1418,6 +1479,21 @@ function getRequestBodySchema(requestBody: any) {
|
|
|
1418
1479
|
requestBody?.schema
|
|
1419
1480
|
}
|
|
1420
1481
|
|
|
1482
|
+
// The response an operation's result is read from.
|
|
1483
|
+
function successResponse(responses: any): any {
|
|
1484
|
+
return responses?.[200] ?? responses?.[201]
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
|
|
1488
|
+
// The response schemas an operation answers with when it succeeds, in the
|
|
1489
|
+
// order they are tried.
|
|
1490
|
+
function successSchemas(responses: any): any[] {
|
|
1491
|
+
return ['200', '201']
|
|
1492
|
+
.map((rescode) => getResponseSchema(responses?.[rescode]))
|
|
1493
|
+
.filter((schema) => null != schema)
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
|
|
1421
1497
|
function getResponseSchema(response: any) {
|
|
1422
1498
|
return response?.content?.['application/json']?.schema ??
|
|
1423
1499
|
response?.schema
|
|
@@ -1558,12 +1634,19 @@ function entityCmpMatch(
|
|
|
1558
1634
|
|| ment.path_rate < IS_ENTCMP_PATH_RATE
|
|
1559
1635
|
)
|
|
1560
1636
|
|
|
1637
|
+
const cmpShared =
|
|
1638
|
+
true === data.work.sharing.yields[ment.origcmpref + ' ' + mdesc.method + ' ' + mdesc.path]
|
|
1639
|
+
|
|
1561
1640
|
if (
|
|
1562
1641
|
null != ment.cmp
|
|
1563
1642
|
&& entname != ment.cmp
|
|
1564
1643
|
&& !ment.cmp.startsWith(entname)
|
|
1565
1644
|
) {
|
|
1566
|
-
if (cmpInfrequent) {
|
|
1645
|
+
if (cmpInfrequent && cmpShared) {
|
|
1646
|
+
why.push('cmp-shared')
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
if (cmpInfrequent && !cmpShared) {
|
|
1567
1650
|
why.push('cmp-primary')
|
|
1568
1651
|
out.name = ment.cmp
|
|
1569
1652
|
out.orig = ment.origcmp
|
|
@@ -1634,14 +1717,184 @@ function cmpOccursInPath(data: { def: any, work: any }, cmpname: string): boolea
|
|
|
1634
1717
|
|
|
1635
1718
|
|
|
1636
1719
|
|
|
1720
|
+
function matchEntityPath(parts: string[]): PathMatch | null {
|
|
1721
|
+
for (const shape of ENTITY_PATH_SHAPES) {
|
|
1722
|
+
const pm = pathMatch(parts, shape)
|
|
1723
|
+
if (null != pm) {
|
|
1724
|
+
return pm
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
return null
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
// The resource a method's path names by itself, as its shape names it. A
|
|
1732
|
+
// write to a singular trailing literal is a verb on another resource, and
|
|
1733
|
+
// names none.
|
|
1734
|
+
function pathResource(parts: string[], method: string): string | null {
|
|
1735
|
+
const pm = matchEntityPath(parts)
|
|
1736
|
+
if (null == pm) {
|
|
1737
|
+
return null
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
const last = parts[parts.length - 1]
|
|
1741
|
+
if (!READ_METHODS.includes(method) && !isParam(last)) {
|
|
1742
|
+
const lit = snakify(last)
|
|
1743
|
+
if ('' !== lit && depluralize(lit) === lit) {
|
|
1744
|
+
return null
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
return canonize(pm[PATH_NAME_INDEX[pm.expr]])
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
|
|
1752
|
+
type SharingRoute = { cmp: string, method: string, path: string, op: string }
|
|
1753
|
+
|
|
1754
|
+
type SharingEntry = SharingRoute & { parts: string[], resource: string }
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
// The routes that keep their own path's name for the component they answer
|
|
1758
|
+
// with, each keyed `cmp METHOD path`: those of a component that several
|
|
1759
|
+
// resources share, where a record (one that declares an `id`) is never shared.
|
|
1760
|
+
function sharedRoutes(routes: SharingRoute[], records: string[]): string[] {
|
|
1761
|
+
const bycmp: Record<string, SharingEntry[]> = {}
|
|
1762
|
+
for (const route of routes) {
|
|
1763
|
+
const parts = route.path.split('/').filter((p) => '' !== p)
|
|
1764
|
+
const resource = pathResource(parts, route.method)
|
|
1765
|
+
if (null != resource && !records.includes(route.cmp)) {
|
|
1766
|
+
(bycmp[route.cmp] = bycmp[route.cmp] ?? []).push({ ...route, parts, resource })
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
const taking: SharingEntry[] = []
|
|
1771
|
+
for (const cmp of Object.keys(bycmp).sort()) {
|
|
1772
|
+
const names = new Set(bycmp[cmp].map((entry) => entry.resource))
|
|
1773
|
+
const counted = bycmp[cmp].filter((entry) => !isSharingView(entry, names))
|
|
1774
|
+
const group = aliasGroups(counted)
|
|
1775
|
+
if (new Set(counted.map((entry) => group[entry.resource])).size < 2) {
|
|
1776
|
+
continue
|
|
1777
|
+
}
|
|
1778
|
+
const members: Record<string, number> = {}
|
|
1779
|
+
for (const name of new Set(counted.map((entry) => entry.resource))) {
|
|
1780
|
+
members[group[name]] = (members[group[name]] ?? 0) + 1
|
|
1781
|
+
}
|
|
1782
|
+
taking.push(...counted.filter((entry) => 1 === members[group[entry.resource]]))
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
const cmps: Record<string, Set<string>> = {}
|
|
1786
|
+
const routesOf: Record<string, Set<string>> = {}
|
|
1787
|
+
for (const entry of taking) {
|
|
1788
|
+
(cmps[entry.resource] = cmps[entry.resource] ?? new Set()).add(entry.cmp)
|
|
1789
|
+
const key = entry.resource + ' ' + entry.op + ' ' + paramNames(entry.parts)
|
|
1790
|
+
;(routesOf[key] = routesOf[key] ?? new Set()).add(entry.path)
|
|
1791
|
+
}
|
|
1792
|
+
const blocked = new Set(Object.keys(cmps).filter((name) => 1 < cmps[name].size))
|
|
1793
|
+
for (const key of Object.keys(routesOf)) {
|
|
1794
|
+
if (1 < routesOf[key].size) {
|
|
1795
|
+
blocked.add(key.split(' ')[0])
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
return taking
|
|
1800
|
+
.filter((entry) => !blocked.has(entry.resource))
|
|
1801
|
+
.map((entry) => entry.cmp + ' ' + entry.method + ' ' + entry.path)
|
|
1802
|
+
.sort()
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
|
|
1806
|
+
// A route beneath a segment that names another of the component's resources
|
|
1807
|
+
// is a view of that resource, such as `/builds/latest` of `/builds`.
|
|
1808
|
+
function isSharingView(entry: SharingEntry, names: Set<string>): boolean {
|
|
1809
|
+
for (let i = 1; i < entry.parts.length; i++) {
|
|
1810
|
+
const name = pathResource(entry.parts.slice(0, i), 'GET')
|
|
1811
|
+
if (null != name && name !== entry.resource && names.has(name)) {
|
|
1812
|
+
return true
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
return false
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
// Item routes under the same parent keyed by the same parameters address the
|
|
1820
|
+
// same records, so the resources they name are one: each maps to its group.
|
|
1821
|
+
function aliasGroups(entries: SharingEntry[]): Record<string, string> {
|
|
1822
|
+
const group: Record<string, string> = {}
|
|
1823
|
+
const find = (name: string): string => group[name] === name ? name : find(group[name])
|
|
1824
|
+
for (const entry of entries) {
|
|
1825
|
+
group[entry.resource] = entry.resource
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
const first: Record<string, string> = {}
|
|
1829
|
+
for (const entry of entries) {
|
|
1830
|
+
if (!isParam(entry.parts[entry.parts.length - 1])) {
|
|
1831
|
+
continue
|
|
1832
|
+
}
|
|
1833
|
+
const pm = matchEntityPath(entry.parts) as PathMatch
|
|
1834
|
+
const parent = entry.parts.slice(0, pm.index + PATH_NAME_INDEX[pm.expr])
|
|
1835
|
+
.map((p) => isParam(p) ? '{}' : p).join('/')
|
|
1836
|
+
const key = parent + ' ' + paramNames(entry.parts)
|
|
1837
|
+
const other = first[key] = first[key] ?? entry.resource
|
|
1838
|
+
const [a, b] = [find(other), find(entry.resource)].sort()
|
|
1839
|
+
group[b] = a
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
for (const name of Object.keys(group)) {
|
|
1843
|
+
group[name] = find(name)
|
|
1844
|
+
}
|
|
1845
|
+
return group
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
|
|
1849
|
+
function paramNames(parts: string[]): string {
|
|
1850
|
+
return parts.filter(isParam).sort().join(',')
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
|
|
1854
|
+
// The schema a local `$ref` pointer names in the definition.
|
|
1855
|
+
function refSchema(def: any, xref: string): any {
|
|
1856
|
+
if (!xref.startsWith('#/')) {
|
|
1857
|
+
return undefined
|
|
1858
|
+
}
|
|
1859
|
+
let node = def
|
|
1860
|
+
for (const seg of xref.slice(2).split('/')) {
|
|
1861
|
+
const key = seg.replace(/~1/g, '/').replace(/~0/g, '~')
|
|
1862
|
+
node = null != node && 'object' === typeof node && Object.prototype.hasOwnProperty.call(node, key)
|
|
1863
|
+
? node[key] : undefined
|
|
1864
|
+
}
|
|
1865
|
+
return node
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
|
|
1869
|
+
// Read in place: resolveSchemaProperties merges, and merging rewrites the
|
|
1870
|
+
// nodes the def's schemas share.
|
|
1871
|
+
function declaresId(schema: any): boolean {
|
|
1872
|
+
if (null == schema || 'object' !== typeof schema) {
|
|
1873
|
+
return false
|
|
1874
|
+
}
|
|
1875
|
+
const parts = [schema, ...(Array.isArray(schema.allOf) ? schema.allOf : [])]
|
|
1876
|
+
return parts.some((part: any) => null != part?.properties?.id)
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
|
|
1880
|
+
// The operation ResolveOperation will assign, needed before the entity is
|
|
1881
|
+
// named: whether a response unwraps as an envelope depends on it.
|
|
1882
|
+
function methodOpname(
|
|
1883
|
+
mdesc: Record<string, any>,
|
|
1884
|
+
pm: PathMatch | null,
|
|
1885
|
+
why: string[]
|
|
1886
|
+
): string | undefined {
|
|
1887
|
+
const opname = METHOD_IDOP[mdesc.method]
|
|
1888
|
+
return 'load' === opname && isListResponse(mdesc, pm, mdesc.path, why) ? 'list' : opname
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
|
|
1637
1892
|
function isListResponse(
|
|
1638
1893
|
mdesc: Record<string, any>,
|
|
1894
|
+
pm: PathMatch | null | undefined,
|
|
1639
1895
|
pathStr: string,
|
|
1640
1896
|
why: string[]
|
|
1641
1897
|
): boolean {
|
|
1642
|
-
const ment = mdesc.MethodEntity
|
|
1643
|
-
const pm = ment.pm
|
|
1644
|
-
|
|
1645
1898
|
let islist = false
|
|
1646
1899
|
let schema
|
|
1647
1900
|
|
|
@@ -1652,8 +1905,7 @@ function isListResponse(
|
|
|
1652
1905
|
why.push('end-param')
|
|
1653
1906
|
}
|
|
1654
1907
|
else {
|
|
1655
|
-
|
|
1656
|
-
schema = getResponseSchema(response)
|
|
1908
|
+
schema = getResponseSchema(successResponse(mdesc.responses))
|
|
1657
1909
|
|
|
1658
1910
|
if (null == schema) {
|
|
1659
1911
|
why.push('no-schema')
|
|
@@ -1875,22 +2127,30 @@ function makeMethodEntityDesc(desc: Record<string, any>): MethodEntityDesc {
|
|
|
1875
2127
|
}
|
|
1876
2128
|
|
|
1877
2129
|
|
|
1878
|
-
function findPotentialSchemaRefs(
|
|
2130
|
+
function findPotentialSchemaRefs(
|
|
2131
|
+
pathStr: string,
|
|
2132
|
+
methodName: string,
|
|
2133
|
+
responses: any,
|
|
2134
|
+
envelope: Record<string, string>,
|
|
2135
|
+
why: string[],
|
|
2136
|
+
) {
|
|
1879
2137
|
const xrefs: string[] = []
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
xrefs.push(schema['x-ref'])
|
|
2138
|
+
for (const schema of successSchemas(responses)) {
|
|
2139
|
+
if (null != schema['x-ref']) {
|
|
2140
|
+
// An envelope component names its wrapping, not the entity: the
|
|
2141
|
+
// component it carries takes its place.
|
|
2142
|
+
const itemref = envelope[schema['x-ref']]
|
|
2143
|
+
if ('' !== itemref) {
|
|
2144
|
+
why.push('envelope=' + cmpRefName(schema['x-ref']))
|
|
2145
|
+
xrefs.push(itemref)
|
|
1889
2146
|
}
|
|
1890
|
-
else
|
|
1891
|
-
xrefs.push(schema
|
|
2147
|
+
else {
|
|
2148
|
+
xrefs.push(schema['x-ref'])
|
|
1892
2149
|
}
|
|
1893
2150
|
}
|
|
2151
|
+
else if ('array' === schema.type && null != schema.items?.['x-ref']) {
|
|
2152
|
+
xrefs.push(schema.items?.['x-ref'])
|
|
2153
|
+
}
|
|
1894
2154
|
}
|
|
1895
2155
|
|
|
1896
2156
|
debugpath(pathStr, methodName, 'POTENTIAL-SCHEMA-REFS', xrefs)
|
|
@@ -1898,6 +2158,12 @@ function findPotentialSchemaRefs(pathStr: string, methodName: string, responses:
|
|
|
1898
2158
|
}
|
|
1899
2159
|
|
|
1900
2160
|
|
|
2161
|
+
function cmpRefName(xref: string): string {
|
|
2162
|
+
const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/)
|
|
2163
|
+
return null == m ? xref : canonizeCmpName(m[2])
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
|
|
1901
2167
|
function hasMethod(def: any, pathStr: string, methodName: string) {
|
|
1902
2168
|
const pathDef = def?.paths?.[pathStr]
|
|
1903
2169
|
const found = (
|
|
@@ -1913,5 +2179,7 @@ function hasMethod(def: any, pathStr: string, methodName: string) {
|
|
|
1913
2179
|
|
|
1914
2180
|
|
|
1915
2181
|
export {
|
|
1916
|
-
heuristic01
|
|
2182
|
+
heuristic01,
|
|
2183
|
+
pathResource,
|
|
2184
|
+
sharedRoutes,
|
|
1917
2185
|
}
|