@voxgig/apidef 8.22.1 → 8.24.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.d.ts +1 -1
- package/dist/apidef.js +2 -0
- package/dist/apidef.js.map +1 -1
- package/dist/builder/entity/entity.d.ts +2 -1
- package/dist/builder/entity/entity.js +14 -11
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/guide/guide.js +5 -2
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +11 -1
- package/dist/guide/heuristic01.js +457 -100
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/model.d.ts +18 -1
- package/dist/transform/args.d.ts +4 -1
- package/dist/transform/args.js +67 -20
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/body.d.ts +23 -0
- package/dist/transform/body.js +474 -0
- package/dist/transform/body.js.map +1 -0
- package/dist/transform/entity.d.ts +1 -1
- package/dist/transform/entity.js +13 -4
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.d.ts +3 -1
- package/dist/transform/field.js +159 -87
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/operation.js +77 -24
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/select.js +44 -11
- package/dist/transform/select.js.map +1 -1
- package/dist/transform/top.d.ts +3 -2
- package/dist/transform/top.js +127 -11
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.js.map +1 -1
- package/dist/utility.d.ts +11 -1
- package/dist/utility.js +156 -13
- package/dist/utility.js.map +1 -1
- package/model/apidef.aontu +29 -6
- package/model/guide.aontu +8 -0
- package/package.json +1 -1
- package/src/apidef.ts +5 -0
- package/src/builder/entity/entity.ts +17 -15
- package/src/guide/guide.ts +5 -3
- package/src/guide/heuristic01.ts +587 -104
- package/src/model.ts +27 -0
- package/src/transform/args.ts +83 -22
- package/src/transform/body.ts +638 -0
- package/src/transform/entity.ts +20 -4
- package/src/transform/field.ts +190 -88
- package/src/transform/operation.ts +97 -26
- package/src/transform/select.ts +49 -11
- package/src/transform/top.ts +154 -15
- package/src/types.ts +6 -0
- package/src/utility.ts +190 -13
package/src/guide/heuristic01.ts
CHANGED
|
@@ -5,13 +5,16 @@ import type { TaskSpec } from 'ordu'
|
|
|
5
5
|
|
|
6
6
|
import { each } from 'jostraca'
|
|
7
7
|
|
|
8
|
-
import { size, merge, getelem, isempty, items, keysof } from '@voxgig/struct'
|
|
8
|
+
import { size, merge, clone, getelem, isempty, items, keysof } from '@voxgig/struct'
|
|
9
9
|
|
|
10
10
|
import {
|
|
11
|
-
isEntityWrapperProp, envelopeProp, envelopeItemRef, composedEnvelopeProp,
|
|
12
|
-
|
|
11
|
+
isEntityWrapperProp, envelopeProp, envelopeItemRef, composedEnvelopeProp, propIsList,
|
|
12
|
+
bodyRequestTransform, authExchangeOp, specSecuredByDefault, itemEnvelopeKey,
|
|
13
|
+
itemEnvelopeTransform,
|
|
13
14
|
} from '../utility'
|
|
14
15
|
|
|
16
|
+
import { jsonRequestSchema } from '../transform/body'
|
|
17
|
+
|
|
15
18
|
|
|
16
19
|
import {
|
|
17
20
|
ApiDefContext,
|
|
@@ -44,6 +47,7 @@ import type {
|
|
|
44
47
|
import {
|
|
45
48
|
canonize,
|
|
46
49
|
canonizeCmpName,
|
|
50
|
+
canonizeParam,
|
|
47
51
|
capture,
|
|
48
52
|
cleanComponentName,
|
|
49
53
|
debugpath,
|
|
@@ -57,6 +61,8 @@ import {
|
|
|
57
61
|
mergedProperties,
|
|
58
62
|
normalizeFieldName,
|
|
59
63
|
pathMatch,
|
|
64
|
+
pluralize,
|
|
65
|
+
prefixLeadingDigit,
|
|
60
66
|
resplitFromCmp,
|
|
61
67
|
sortedEntries,
|
|
62
68
|
sortedKeys,
|
|
@@ -138,11 +144,13 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
138
144
|
{ select: selectCmpXrefs, apply: MeasureRef },
|
|
139
145
|
{ select: selectAllMethods, apply: MeasureEnvelope },
|
|
140
146
|
MeasureEnvelopeItems,
|
|
147
|
+
MeasureAnswered,
|
|
141
148
|
{ select: selectAllMethods, apply: MeasureSharing },
|
|
142
149
|
MeasureShared,
|
|
150
|
+
{ select: selectAllMethods, apply: [ResolveEntityComponent, MeasureTagItems] },
|
|
151
|
+
{ select: selectAllMethods, apply: MeasureClaims },
|
|
143
152
|
{
|
|
144
153
|
select: selectAllMethods, apply: [
|
|
145
|
-
ResolveEntityComponent,
|
|
146
154
|
ResolveEntityName,
|
|
147
155
|
RenameParams,
|
|
148
156
|
FindActions,
|
|
@@ -164,7 +172,14 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
164
172
|
|
|
165
173
|
guide.metrics.count.entity -= mergeCollectionPaths(guide, ctx.log,
|
|
166
174
|
(pathStr: string, methods: string[], collection: boolean) =>
|
|
167
|
-
pathRecordRef(ctx.def, pathStr, methods, collection)
|
|
175
|
+
pathRecordRef(ctx.def, pathStr, methods, collection),
|
|
176
|
+
(sharePath: string, shareMethods: string[], itemPath: string, itemMethods: string[]) =>
|
|
177
|
+
distinctShare(ctx.def, sharePath, shareMethods, itemPath, itemMethods)).length
|
|
178
|
+
|
|
179
|
+
// After the merge, which can move a path onto another entity.
|
|
180
|
+
for (const entity of Object.values(guide.entity) as GuideEntity[]) {
|
|
181
|
+
nameEntityParams(entity)
|
|
182
|
+
}
|
|
168
183
|
|
|
169
184
|
const metrics = guide.metrics
|
|
170
185
|
|
|
@@ -229,6 +244,13 @@ function Prepare(spec: TaskSpec) {
|
|
|
229
244
|
listEnvelope: {},
|
|
230
245
|
envelopePaths: {},
|
|
231
246
|
sharing: { routes: [], records: {}, yields: {} },
|
|
247
|
+
recordResources: {},
|
|
248
|
+
tagItems: {},
|
|
249
|
+
claims: {},
|
|
250
|
+
claimowner: {},
|
|
251
|
+
claimrecord: {},
|
|
252
|
+
pathowner: {},
|
|
253
|
+
recordowner: {},
|
|
232
254
|
entity: {
|
|
233
255
|
count: {
|
|
234
256
|
seen: 0,
|
|
@@ -378,6 +400,13 @@ function MeasureEnvelopeItems(spec: TaskSpec) {
|
|
|
378
400
|
}
|
|
379
401
|
|
|
380
402
|
|
|
403
|
+
// The records some operation answers with in a 200 or 201, read through the
|
|
404
|
+
// envelopes MeasureEnvelopeItems settles.
|
|
405
|
+
function MeasureAnswered(spec: TaskSpec) {
|
|
406
|
+
spec.data.work.answered = answeredRefs(spec.data.def, spec.data.work.envelope)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
381
410
|
function isPageAndItsItem(work: any, xrefs: string[]): boolean {
|
|
382
411
|
const pages = xrefs.filter((xref) => work.listEnvelope[xref])
|
|
383
412
|
const items = xrefs.filter((xref) => !work.listEnvelope[xref])
|
|
@@ -400,13 +429,20 @@ function MeasureSharing(spec: TaskSpec) {
|
|
|
400
429
|
const op = methodOpname(mdesc, matchEntityPath(parts), []) ?? ''
|
|
401
430
|
const sharing = work.sharing
|
|
402
431
|
|
|
403
|
-
const
|
|
432
|
+
const resource = pathResource(parts, mdesc.method)
|
|
433
|
+
|
|
434
|
+
const xrefs = findPotentialSchemaRefs(
|
|
435
|
+
mdesc.path, mdesc.method, mdesc.responses, work.envelope, work.answered, [])
|
|
404
436
|
for (const xref of xrefs) {
|
|
405
437
|
const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/)
|
|
406
438
|
if (null != m) {
|
|
407
439
|
const cmp = canonizeCmpName(m[2])
|
|
440
|
+
const record = declaresId(refSchema(spec.data.def, xref))
|
|
408
441
|
sharing.routes.push({ cmp, method: mdesc.method, path: mdesc.path, op })
|
|
409
|
-
sharing.records[cmp] = true === sharing.records[cmp] ||
|
|
442
|
+
sharing.records[cmp] = true === sharing.records[cmp] || record
|
|
443
|
+
if (record && null != resource) {
|
|
444
|
+
work.recordResources[resource] = true
|
|
445
|
+
}
|
|
410
446
|
}
|
|
411
447
|
}
|
|
412
448
|
}
|
|
@@ -421,8 +457,13 @@ function MeasureShared(spec: TaskSpec) {
|
|
|
421
457
|
}
|
|
422
458
|
|
|
423
459
|
|
|
460
|
+
// One list for every pass, so a method keeps the component resolved for it.
|
|
424
461
|
function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
|
|
425
462
|
const ctx = spec.ctx
|
|
463
|
+
const work = spec.data.work
|
|
464
|
+
if (null != work.methods) {
|
|
465
|
+
return work.methods
|
|
466
|
+
}
|
|
426
467
|
|
|
427
468
|
|
|
428
469
|
let caught: any = { methods: [] }
|
|
@@ -471,7 +512,55 @@ function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
|
|
|
471
512
|
}
|
|
472
513
|
})
|
|
473
514
|
|
|
474
|
-
|
|
515
|
+
work.methods = caught.methods
|
|
516
|
+
return work.methods
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
// The collections each tag gathers item routes from, before any is named, with
|
|
521
|
+
// the parameters of each collection's item routes.
|
|
522
|
+
function MeasureTagItems(spec: TaskSpec) {
|
|
523
|
+
const work = spec.data.work
|
|
524
|
+
const mdesc = spec.node.val
|
|
525
|
+
const parts: string[] = work.pathmap[mdesc.path].parts
|
|
526
|
+
const collection = tagItemCollection(spec.data, mdesc, parts)
|
|
527
|
+
if (null != collection) {
|
|
528
|
+
const cmp = mdesc.MethodEntity.cmp
|
|
529
|
+
const items = work.tagItems[cmp] = work.tagItems[cmp] ?? {}
|
|
530
|
+
items[collection] = items[collection] ?? {}
|
|
531
|
+
items[collection][parts.filter(isParam)
|
|
532
|
+
.map((part) => canonizeParam(part.slice(1, -1))).sort().join(',')] = true
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
// The name each route takes by its path and component, and its stored form,
|
|
538
|
+
// found before any route is named so an item route sees one sorting after it.
|
|
539
|
+
// The rules run on copies. A verb takes its parent's claim; an item route its
|
|
540
|
+
// collection names makes none, so a verb on one claims the route's segment.
|
|
541
|
+
function MeasureClaims(spec: TaskSpec) {
|
|
542
|
+
const data = spec.data
|
|
543
|
+
const work = data.work
|
|
544
|
+
const mdesc = spec.node.val
|
|
545
|
+
const parts: string[] = work.pathmap[mdesc.path].parts
|
|
546
|
+
if (null != collectionNaming(data, mdesc, parts, work.claimrecord)) {
|
|
547
|
+
return
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const ment = { ...(mdesc.MethodEntity ?? makeMethodEntityDesc({})) }
|
|
551
|
+
const name = pathEntityName(data, { ...mdesc, MethodEntity: ment }, parts,
|
|
552
|
+
matchEntityPath(parts), work.claimowner, [])
|
|
553
|
+
if (null != name) {
|
|
554
|
+
const claim = resplitFromCmp(name, ment.cmp as string, [])
|
|
555
|
+
const stored = ensureMinEntityName(claim, {})
|
|
556
|
+
for (const key of [claim, stored]) {
|
|
557
|
+
const claims = work.claims[key] = work.claims[key] ?? {}
|
|
558
|
+
claims[mdesc.path] = true
|
|
559
|
+
}
|
|
560
|
+
const owners = work.claimowner[mdesc.path] = work.claimowner[mdesc.path] ?? {}
|
|
561
|
+
owners[mdesc.method] = claim
|
|
562
|
+
markRecordOwner(work.claimrecord, mdesc.path, parts, ment, stored)
|
|
563
|
+
}
|
|
475
564
|
}
|
|
476
565
|
|
|
477
566
|
|
|
@@ -492,7 +581,7 @@ function ResolveEntityComponent(spec: TaskSpec) {
|
|
|
492
581
|
let responses = methodDef.responses
|
|
493
582
|
|
|
494
583
|
let origxrefs: any[] = findPotentialSchemaRefs(
|
|
495
|
-
pathStr, methodName, responses, work.envelope, why_cmp).map(val => ({
|
|
584
|
+
pathStr, methodName, responses, work.envelope, work.answered, why_cmp).map(val => ({
|
|
496
585
|
val
|
|
497
586
|
}))
|
|
498
587
|
|
|
@@ -668,40 +757,22 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
668
757
|
|
|
669
758
|
why_path.push(...(ment.why_cmp ?? []))
|
|
670
759
|
|
|
671
|
-
let entname
|
|
672
|
-
|
|
673
760
|
const pm = matchEntityPath(parts)
|
|
674
761
|
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
else if ('t/p/' === pm?.expr) {
|
|
680
|
-
entname = entityPathMatch_tpe(data, pm, mdesc, why_path)
|
|
762
|
+
let entname = pathEntityName(data, mdesc, parts, pm, work.pathowner, why_path)
|
|
763
|
+
if (null == entname) {
|
|
764
|
+
work.entity.count.unresolved++
|
|
765
|
+
entname = 'entity' + work.entity.count.unresolved
|
|
681
766
|
}
|
|
682
767
|
|
|
683
|
-
|
|
684
|
-
entname = entityPathMatch_pte(data, pm, mdesc, why_path)
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
else if ('t/' === pm?.expr) {
|
|
688
|
-
entname = entityPathMatch_te(data, pm, mdesc, why_path)
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
else if ('t/p/p' === pm?.expr) {
|
|
692
|
-
entname = entityPathMatch_tpp(data, pm, mdesc, why_path)
|
|
693
|
-
}
|
|
768
|
+
entname = resplitFromCmp(entname, ment.cmp as string, why_path)
|
|
694
769
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
entname = 'entity' + work.entity.count.unresolved
|
|
700
|
-
}
|
|
770
|
+
const item = itemOfCollection(data, mdesc, parts)
|
|
771
|
+
if (null != item) {
|
|
772
|
+
why_path.push(item.why + '=' + item.name)
|
|
773
|
+
entname = item.name
|
|
701
774
|
}
|
|
702
775
|
|
|
703
|
-
entname = resplitFromCmp(entname, ment.cmp as string, why_path)
|
|
704
|
-
|
|
705
776
|
// Keep the pre-truncation name so a truncated-name collision can tell a
|
|
706
777
|
// re-encounter of the SAME origin (merge) from a genuinely different one
|
|
707
778
|
// (numeric suffix) — see ensureMinEntityName.
|
|
@@ -734,10 +805,11 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
734
805
|
// split across entities by method (a PUT answering with a one-off
|
|
735
806
|
// acknowledgement is named after it), and the parent of a verb is the
|
|
736
807
|
// entity a read of the item returns.
|
|
737
|
-
work.pathowner = work.pathowner ?? {}
|
|
738
808
|
work.pathowner[pathStr] = work.pathowner[pathStr] ?? {}
|
|
739
809
|
work.pathowner[pathStr][methodName] = entname
|
|
740
810
|
|
|
811
|
+
markRecordOwner(work.recordowner, pathStr, parts, ment, entname)
|
|
812
|
+
|
|
741
813
|
// Same guard, same reason: the formatting is the cost, not the call.
|
|
742
814
|
if (debugpathOn()) {
|
|
743
815
|
debugpath(pathStr, methodName, 'RESOLVE-ENTITY-NAME',
|
|
@@ -746,6 +818,52 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
746
818
|
}
|
|
747
819
|
|
|
748
820
|
|
|
821
|
+
// The name a method's path shape and component give it, or null where neither
|
|
822
|
+
// does. A verb on a parent takes the name pathowner holds for the parent.
|
|
823
|
+
function pathEntityName(
|
|
824
|
+
data: { def: any, guide: any, work: any },
|
|
825
|
+
mdesc: any,
|
|
826
|
+
parts: string[],
|
|
827
|
+
pm: PathMatch | null,
|
|
828
|
+
pathowner: Record<string, Record<string, string>>,
|
|
829
|
+
why: string[],
|
|
830
|
+
): string | null {
|
|
831
|
+
if ('t/p/t/' === pm?.expr) {
|
|
832
|
+
return entityPathMatch_tpte(data, pm, mdesc, pathowner, why)
|
|
833
|
+
}
|
|
834
|
+
else if ('t/p/' === pm?.expr) {
|
|
835
|
+
return entityPathMatch_tpe(data, pm, mdesc, why)
|
|
836
|
+
}
|
|
837
|
+
else if ('p/t/' === pm?.expr) {
|
|
838
|
+
return entityPathMatch_pte(data, pm, mdesc, why)
|
|
839
|
+
}
|
|
840
|
+
else if ('t/' === pm?.expr) {
|
|
841
|
+
return entityPathMatch_te(data, pm, mdesc, why)
|
|
842
|
+
}
|
|
843
|
+
else if ('t/p/p' === pm?.expr) {
|
|
844
|
+
return entityPathMatch_tpp(data, pm, mdesc, why)
|
|
845
|
+
}
|
|
846
|
+
return inferEntityName(mdesc, parts, why)
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
// The entity a path's own record names, where the record carries the name the
|
|
851
|
+
// path's last segment gives.
|
|
852
|
+
function markRecordOwner(
|
|
853
|
+
recordowner: Record<string, string>,
|
|
854
|
+
pathStr: string,
|
|
855
|
+
parts: string[],
|
|
856
|
+
ment: Partial<MethodEntityDesc>,
|
|
857
|
+
entname: string,
|
|
858
|
+
) {
|
|
859
|
+
const last = parts.filter((part: string) => !isParam(part)).pop()
|
|
860
|
+
if (null == recordowner[pathStr] && isSchemaRef(ment.ref) &&
|
|
861
|
+
entname === ment.cmp && null != last && entname === canonize(last)) {
|
|
862
|
+
recordowner[pathStr] = entname
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
|
|
749
867
|
|
|
750
868
|
function RenameParams(spec: TaskSpec) {
|
|
751
869
|
const ctx = spec.ctx
|
|
@@ -776,7 +894,7 @@ function RenameParams(spec: TaskSpec) {
|
|
|
776
894
|
rename: pathDesc.rename.param = (pathDesc.rename.param ?? {}),
|
|
777
895
|
why: pathDesc.why_rename.why_param = (pathDesc.why_rename.why_param ?? {}),
|
|
778
896
|
}
|
|
779
|
-
const parts = pathdesc.parts
|
|
897
|
+
const parts = verbParts(pathdesc.parts)
|
|
780
898
|
|
|
781
899
|
const applySnakeCaseRename = () => {
|
|
782
900
|
for (const part of parts) {
|
|
@@ -871,7 +989,7 @@ function RenameParams(spec: TaskSpec) {
|
|
|
871
989
|
why.push('id-parent-ent')
|
|
872
990
|
}
|
|
873
991
|
|
|
874
|
-
else {
|
|
992
|
+
else if (!isNameParam(oldParam)) {
|
|
875
993
|
updateParamRename(
|
|
876
994
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
877
995
|
parentName + '_id', 'parent:' + parentName)
|
|
@@ -939,7 +1057,7 @@ function RenameParams(spec: TaskSpec) {
|
|
|
939
1057
|
why.push('default')
|
|
940
1058
|
|
|
941
1059
|
let newParamName = parentName + '_id'
|
|
942
|
-
if (newParamName != oldParam) {
|
|
1060
|
+
if (newParamName != oldParam && !isNameParam(oldParam)) {
|
|
943
1061
|
updateParamRename(
|
|
944
1062
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
945
1063
|
newParamName, 'not-primary')
|
|
@@ -1002,7 +1120,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1002
1120
|
pathdesc.action = (pathdesc.action ?? {})
|
|
1003
1121
|
pathdesc.why_action = (pathdesc.why_action ?? {})
|
|
1004
1122
|
|
|
1005
|
-
const parts = spec.data.work.pathmap[pathStr].parts
|
|
1123
|
+
const parts = verbParts(spec.data.work.pathmap[pathStr].parts)
|
|
1006
1124
|
|
|
1007
1125
|
const fourthLastPart = parts[parts.length - 4]
|
|
1008
1126
|
const fourthLastPartCanon = canonize(fourthLastPart)
|
|
@@ -1016,8 +1134,10 @@ function FindActions(spec: TaskSpec) {
|
|
|
1016
1134
|
const cmp = ment.cmp
|
|
1017
1135
|
|
|
1018
1136
|
if (null != ment.verb_on_parent) {
|
|
1019
|
-
|
|
1020
|
-
|
|
1137
|
+
if (isVerb(lastPart)) {
|
|
1138
|
+
pathdesc.action[lastPartCanon] = pathdesc.action[lastPartCanon] ?? {
|
|
1139
|
+
why_action: ['ent', entdesc.name, 'verb-on-parent', lastPart, methodName],
|
|
1140
|
+
}
|
|
1021
1141
|
}
|
|
1022
1142
|
}
|
|
1023
1143
|
|
|
@@ -1027,7 +1147,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1027
1147
|
|| secondLastPartCanon === ment.origcmp
|
|
1028
1148
|
|| secondLastPartCanon === entname
|
|
1029
1149
|
) {
|
|
1030
|
-
if (
|
|
1150
|
+
if (isVerb(lastPart)) {
|
|
1031
1151
|
updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc, 'no-param')
|
|
1032
1152
|
}
|
|
1033
1153
|
}
|
|
@@ -1038,7 +1158,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1038
1158
|
|| thirdLastPartCanon === ment.origcmp
|
|
1039
1159
|
|| thirdLastPartCanon === entname
|
|
1040
1160
|
) {
|
|
1041
|
-
if (isParam(secondLastPart) &&
|
|
1161
|
+
if (isParam(secondLastPart) && isVerb(lastPart)) {
|
|
1042
1162
|
updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc,
|
|
1043
1163
|
'ent-param-2nd-last')
|
|
1044
1164
|
}
|
|
@@ -1050,7 +1170,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1050
1170
|
|| fourthLastPartCanon === ment.origcmp
|
|
1051
1171
|
|| fourthLastPartCanon === entname
|
|
1052
1172
|
) {
|
|
1053
|
-
if (isParam(thirdLastPart) &&
|
|
1173
|
+
if (isParam(thirdLastPart) && isVerb(secondLastPart) && isVerb(lastPart)) {
|
|
1054
1174
|
const oldActionName = secondLastPart + '/' + lastPart
|
|
1055
1175
|
const actionName = secondLastPartCanon + '_' + lastPartCanon
|
|
1056
1176
|
updateAction(methodName, oldActionName, actionName, entdesc, pathdesc,
|
|
@@ -1179,10 +1299,17 @@ function ResolveTransform(spec: TaskSpec) {
|
|
|
1179
1299
|
// declare properties of its own beside them.
|
|
1180
1300
|
const partprops = mergedProperties(resschema)
|
|
1181
1301
|
|
|
1182
|
-
|
|
1302
|
+
// A list reads records, so an object named for the entity does not win over
|
|
1303
|
+
// a list of them beside it, such as the matches beside the competition a
|
|
1304
|
+
// page of them repeats. The list is found in the same parts the name is.
|
|
1305
|
+
const records = 'list' === opname && null != partprops ? envelopeProp(partprops, opname) : null
|
|
1306
|
+
const holdsRecord = (prop: any) => isEntityWrapperProp(prop) &&
|
|
1307
|
+
(null == records || true === propIsList(prop))
|
|
1308
|
+
|
|
1309
|
+
if (named && holdsRecord(partprops?.[entdesc.origname])) {
|
|
1183
1310
|
transform.res = '`body.' + entdesc.origname + '`'
|
|
1184
1311
|
}
|
|
1185
|
-
else if (named &&
|
|
1312
|
+
else if (named && holdsRecord(partprops?.[entdesc.name])) {
|
|
1186
1313
|
transform.res = '`body.' + entdesc.name + '`'
|
|
1187
1314
|
}
|
|
1188
1315
|
else if (resprops) {
|
|
@@ -1204,35 +1331,20 @@ function ResolveTransform(spec: TaskSpec) {
|
|
|
1204
1331
|
}
|
|
1205
1332
|
}
|
|
1206
1333
|
|
|
1207
|
-
|
|
1208
|
-
const
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
keysof(reqprops).every((k: string) => k === name)
|
|
1215
|
-
|
|
1216
|
-
if (reqschema) {
|
|
1217
|
-
if (wraps(entdesc.origname)) {
|
|
1218
|
-
transform.req = { [entdesc.origname]: '`reqdata`' }
|
|
1219
|
-
}
|
|
1220
|
-
else if (wraps(entdesc.name)) {
|
|
1221
|
-
transform.req = { [entdesc.name]: '`reqdata`' }
|
|
1222
|
-
}
|
|
1223
|
-
else {
|
|
1224
|
-
// A CLOSED body schema names every property the server will accept, so
|
|
1225
|
-
// the body is those properties — not the whole request payload. The
|
|
1226
|
-
// payload also carries the op's PATH params (`id` for
|
|
1227
|
-
// `PUT /item/{id}`), and a closed shape rejects the entire request over
|
|
1228
|
-
// that one extra key: every update came back 400 with `invalid-data`.
|
|
1229
|
-
const body = closedBodyTransform(reqschema)
|
|
1230
|
-
if (null != body) {
|
|
1231
|
-
transform.req = body
|
|
1232
|
-
}
|
|
1334
|
+
// An item that is the entity's own component is the record, as above.
|
|
1335
|
+
const itemref = resschema?.items?.['x-ref']
|
|
1336
|
+
if (null == transform.res && 'list' === opname &&
|
|
1337
|
+
(null == itemref || null == entdesc.cmp || cmpRefName(itemref) !== entdesc.cmp)) {
|
|
1338
|
+
const key = itemEnvelopeKey(resschema, [entdesc.origname, entdesc.name])
|
|
1339
|
+
if (null != key) {
|
|
1340
|
+
transform.res = itemEnvelopeTransform(key)
|
|
1233
1341
|
}
|
|
1234
1342
|
}
|
|
1235
1343
|
|
|
1344
|
+
const reqschema = getRequestBodySchema(mdesc.requestBody)
|
|
1345
|
+
debugpath(pathStr, methodName, 'TRANSFORM-REQ', keysof(reqschema?.properties))
|
|
1346
|
+
transform.req = bodyRequestTransform(reqschema, [entdesc.origname, entdesc.name])
|
|
1347
|
+
|
|
1236
1348
|
if (!isempty(transform) && null != op[opname]) {
|
|
1237
1349
|
op[opname].transform = transform
|
|
1238
1350
|
}
|
|
@@ -1298,6 +1410,7 @@ function entityPathMatch_tpte(
|
|
|
1298
1410
|
data: { def: any, guide: any, work: any },
|
|
1299
1411
|
pm: PathMatch,
|
|
1300
1412
|
mdesc: any,
|
|
1413
|
+
pathowner: Record<string, Record<string, string>>,
|
|
1301
1414
|
why: string[]
|
|
1302
1415
|
) {
|
|
1303
1416
|
const ment = mdesc.MethodEntity
|
|
@@ -1310,7 +1423,7 @@ function entityPathMatch_tpte(
|
|
|
1310
1423
|
let ecm = undefined
|
|
1311
1424
|
|
|
1312
1425
|
if (null != ment.cmp) {
|
|
1313
|
-
const parent = verbOnParent(data, pm, mdesc)
|
|
1426
|
+
const parent = verbOnParent(data, pm, mdesc, pathowner)
|
|
1314
1427
|
if (null != parent) {
|
|
1315
1428
|
entname = parent
|
|
1316
1429
|
ment.verb_on_parent = getelem(pm, -1)
|
|
@@ -1362,6 +1475,7 @@ function verbOnParent(
|
|
|
1362
1475
|
data: { def: any, guide: any, work: any },
|
|
1363
1476
|
pm: PathMatch,
|
|
1364
1477
|
mdesc: any,
|
|
1478
|
+
pathowner: Record<string, Record<string, string>>,
|
|
1365
1479
|
): null | string {
|
|
1366
1480
|
const method = mdesc.method
|
|
1367
1481
|
if (READ_METHODS.includes(method)) {
|
|
@@ -1412,7 +1526,7 @@ function verbOnParent(
|
|
|
1412
1526
|
// known. The parent is the entity a READ of the item returns: a PUT on
|
|
1413
1527
|
// the item answering with a one-off acknowledgement is named after that
|
|
1414
1528
|
// and must not claim the verb. Fall back to any owner, then the literal.
|
|
1415
|
-
const owners =
|
|
1529
|
+
const owners = pathowner[itemPath] ?? {}
|
|
1416
1530
|
const parent = owners.GET ?? owners.QUERY ??
|
|
1417
1531
|
Object.values(owners).sort()[0] as undefined | string
|
|
1418
1532
|
if (null != parent) {
|
|
@@ -1423,6 +1537,137 @@ function verbOnParent(
|
|
|
1423
1537
|
}
|
|
1424
1538
|
|
|
1425
1539
|
|
|
1540
|
+
// The collection of an item route whose method its tag alone names, when the
|
|
1541
|
+
// route answers nothing.
|
|
1542
|
+
function tagItemCollection(
|
|
1543
|
+
data: { def: any, work: any },
|
|
1544
|
+
mdesc: any,
|
|
1545
|
+
parts: string[],
|
|
1546
|
+
): string | null {
|
|
1547
|
+
const ment = mdesc.MethodEntity
|
|
1548
|
+
if (null == ment || 'tag' !== ment.ref || null != ment.rescmp) {
|
|
1549
|
+
return null
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
const lits = parts.slice(0, parts.findIndex(isParam))
|
|
1553
|
+
if (0 === lits.length || !parts.slice(lits.length).every(isParam)) {
|
|
1554
|
+
return null
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
const answers = Object.entries(data.def?.paths?.[mdesc.path] ?? {})
|
|
1558
|
+
.some(([method, mdef]: [string, any]) =>
|
|
1559
|
+
null != METHOD_CONSIDER_ORDER[method.toUpperCase()] &&
|
|
1560
|
+
null != getResponseSchema(successResponse(mdef?.responses)))
|
|
1561
|
+
return answers ? null : '/' + lits.join('/')
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
|
|
1565
|
+
// The collection that names a tag-named item route, given the record owners
|
|
1566
|
+
// known so far: by its record, where the tag names another resource with one
|
|
1567
|
+
// (GitHub's invitations), or by its segment, where the routes the tag gathers
|
|
1568
|
+
// from several collections would share a selector (Apicurio's well-known
|
|
1569
|
+
// routes). Null where the route keeps the name its own path gives.
|
|
1570
|
+
function collectionNaming(
|
|
1571
|
+
data: { def: any, work: any },
|
|
1572
|
+
mdesc: any,
|
|
1573
|
+
parts: string[],
|
|
1574
|
+
recordowner: Record<string, string>,
|
|
1575
|
+
): { collection: string, record?: string } | null {
|
|
1576
|
+
const collection = tagItemCollection(data, mdesc, parts)
|
|
1577
|
+
if (null == collection) {
|
|
1578
|
+
return null
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
const work = data.work
|
|
1582
|
+
const cmp = mdesc.MethodEntity.cmp
|
|
1583
|
+
if (true === work.recordResources[cmp]) {
|
|
1584
|
+
const record = recordowner[collection] ?? recordowner[collection + '/']
|
|
1585
|
+
return null == record ? null : { collection, record }
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
return tagCollides(data.def, work.tagItems[cmp] ?? {}) ? { collection } : null
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
|
|
1592
|
+
// The entity of an item route its collection names: the collection's record,
|
|
1593
|
+
// or its segment. A route outside the collection that has the segment puts the
|
|
1594
|
+
// tag before it, and one that has that name too a number from 2 after it, up
|
|
1595
|
+
// to the first number that leaves the stored form as it was.
|
|
1596
|
+
function itemOfCollection(
|
|
1597
|
+
data: { def: any, work: any },
|
|
1598
|
+
mdesc: any,
|
|
1599
|
+
parts: string[],
|
|
1600
|
+
): { name: string, why: string } | null {
|
|
1601
|
+
const naming = collectionNaming(data, mdesc, parts, data.work.recordowner)
|
|
1602
|
+
if (null == naming) {
|
|
1603
|
+
return null
|
|
1604
|
+
}
|
|
1605
|
+
if (null != naming.record) {
|
|
1606
|
+
return { name: naming.record, why: 'collection-record' }
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
const collection = naming.collection
|
|
1610
|
+
const segment = canonize(collection.substring(collection.lastIndexOf('/') + 1))
|
|
1611
|
+
if (!segmentHeld(data.work, segment, collection)) {
|
|
1612
|
+
return { name: segment, why: 'collection-segment' }
|
|
1613
|
+
}
|
|
1614
|
+
const tagged = mdesc.MethodEntity.cmp + '_' + segment
|
|
1615
|
+
let name = tagged
|
|
1616
|
+
for (let i = 2; segmentHeld(data.work, name, collection); i++) {
|
|
1617
|
+
const stored = ensureMinEntityName(name, {})
|
|
1618
|
+
name = tagged + i
|
|
1619
|
+
if (ensureMinEntityName(name, {}) === stored) {
|
|
1620
|
+
break
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
return { name, why: 'collection-segment' }
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
|
|
1627
|
+
// A route outside the collection has the name: one named already under the
|
|
1628
|
+
// key the name would take now, or one that claims the name, its stored form
|
|
1629
|
+
// or that key. An item route its collection names makes no claim, so it
|
|
1630
|
+
// counts only once named.
|
|
1631
|
+
function segmentHeld(work: any, name: string, collection: string): boolean {
|
|
1632
|
+
const outside = (path: string) => !(path + '/').startsWith(collection + '/')
|
|
1633
|
+
const key = ensureMinEntityName(name, work.entmap)
|
|
1634
|
+
const stored = ensureMinEntityName(name, {})
|
|
1635
|
+
return [work.entmap[key]?.path, work.claims[name], work.claims[stored], work.claims[key]]
|
|
1636
|
+
.some((paths: Record<string, any> | undefined) => Object.keys(paths ?? {}).some(outside))
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
// Two of a tag's collections whose item routes take the same parameters, or
|
|
1641
|
+
// that are both read, would share a selector on the tag's entity.
|
|
1642
|
+
function tagCollides(def: any, items: Record<string, Record<string, boolean>>): boolean {
|
|
1643
|
+
const seen: Record<string, boolean> = {}
|
|
1644
|
+
let reads = 0
|
|
1645
|
+
for (const [collection, params] of Object.entries(items)) {
|
|
1646
|
+
for (const key of Object.keys(params)) {
|
|
1647
|
+
if (seen[key]) {
|
|
1648
|
+
return true
|
|
1649
|
+
}
|
|
1650
|
+
seen[key] = true
|
|
1651
|
+
}
|
|
1652
|
+
if (pathReads(def.paths?.[collection]) || pathReads(def.paths?.[collection + '/'])) {
|
|
1653
|
+
reads++
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
return 1 < reads
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
function pathReads(pathdef: any): boolean {
|
|
1661
|
+
return Object.entries(pathdef ?? {}).some(([method, mdef]: [string, any]) =>
|
|
1662
|
+
'GET' === method.toUpperCase() && null != mdef)
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
function isSchemaRef(ref: string | undefined): boolean {
|
|
1667
|
+
return null != ref && '' !== ref && 'tag' !== ref
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
|
|
1426
1671
|
function isOrigCmp(data: any, name: string) {
|
|
1427
1672
|
return null != data.guide.metrics.count.origcmprefs[name]
|
|
1428
1673
|
}
|
|
@@ -1526,8 +1771,7 @@ function entityPathMatch_tpp(
|
|
|
1526
1771
|
|
|
1527
1772
|
|
|
1528
1773
|
function getRequestBodySchema(requestBody: any) {
|
|
1529
|
-
return requestBody?.
|
|
1530
|
-
requestBody?.schema
|
|
1774
|
+
return jsonRequestSchema({ requestBody }) ?? requestBody?.schema
|
|
1531
1775
|
}
|
|
1532
1776
|
|
|
1533
1777
|
// The response an operation's result is read from, down to an Accepted
|
|
@@ -1547,6 +1791,61 @@ function successSchemas(responses: any): any[] {
|
|
|
1547
1791
|
}
|
|
1548
1792
|
|
|
1549
1793
|
|
|
1794
|
+
// A read answered only by an Accepted response is named from it when another
|
|
1795
|
+
// operation answers with the same record in a 200 or 201, which shows the
|
|
1796
|
+
// record is a resource rather than the work queued.
|
|
1797
|
+
function namingSchemas(
|
|
1798
|
+
method: string,
|
|
1799
|
+
responses: any,
|
|
1800
|
+
answered: Record<string, boolean>,
|
|
1801
|
+
envelope: Record<string, string>,
|
|
1802
|
+
): any[] {
|
|
1803
|
+
const schemas = successSchemas(responses)
|
|
1804
|
+
if (0 === schemas.length && READ_METHODS.includes(method)) {
|
|
1805
|
+
const accepted = getResponseSchema(responses?.[202])
|
|
1806
|
+
if (true === answered[namingRef(accepted, envelope) ?? '']) {
|
|
1807
|
+
schemas.push(accepted)
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
return schemas
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
function answeredRefs(
|
|
1815
|
+
def: any,
|
|
1816
|
+
envelope: Record<string, string>,
|
|
1817
|
+
): Record<string, boolean> {
|
|
1818
|
+
const answered: Record<string, boolean> = {}
|
|
1819
|
+
for (const pathdef of Object.values(def?.paths ?? {}) as any[]) {
|
|
1820
|
+
for (const [method, mdef] of Object.entries(pathdef ?? {}) as [string, any][]) {
|
|
1821
|
+
if (null == METHOD_CONSIDER_ORDER[method.toUpperCase()] ||
|
|
1822
|
+
null == mdef || 'object' !== typeof mdef) {
|
|
1823
|
+
continue
|
|
1824
|
+
}
|
|
1825
|
+
for (const schema of successSchemas(mdef.responses)) {
|
|
1826
|
+
const ref = namingRef(schema, envelope)
|
|
1827
|
+
if (null != ref) {
|
|
1828
|
+
answered[ref] = true
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
return answered
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
|
|
1837
|
+
// The component a response schema names through: the record an envelope
|
|
1838
|
+
// carries, else the schema's own component, else its array items'.
|
|
1839
|
+
function namingRef(schema: any, envelope: Record<string, string>): string | undefined {
|
|
1840
|
+
const xref = schema?.['x-ref']
|
|
1841
|
+
if (null != xref) {
|
|
1842
|
+
const itemref = envelope[xref]
|
|
1843
|
+
return null == itemref || '' === itemref ? xref : itemref
|
|
1844
|
+
}
|
|
1845
|
+
return 'array' === schema?.type ? schema.items?.['x-ref'] : undefined
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
|
|
1550
1849
|
function getResponseSchema(response: any) {
|
|
1551
1850
|
return response?.content?.['application/json']?.schema ??
|
|
1552
1851
|
response?.schema
|
|
@@ -1560,11 +1859,12 @@ function pathRecordRef(
|
|
|
1560
1859
|
pathStr: string,
|
|
1561
1860
|
methods: string[],
|
|
1562
1861
|
collection: boolean,
|
|
1862
|
+
accepted: boolean = true,
|
|
1563
1863
|
): string | null {
|
|
1564
1864
|
const rank = (method: string) => METHOD_CONSIDER_ORDER[method] ?? Number.MAX_SAFE_INTEGER
|
|
1565
1865
|
const ordered = [...methods].sort((a, b) => rank(a) - rank(b) || byCodePoint(a, b))
|
|
1566
1866
|
for (const method of ordered) {
|
|
1567
|
-
const ref = routeRecordRef(def, pathStr, method, collection)
|
|
1867
|
+
const ref = routeRecordRef(def, pathStr, method, collection, accepted)
|
|
1568
1868
|
if (null != ref) {
|
|
1569
1869
|
return ref
|
|
1570
1870
|
}
|
|
@@ -1577,15 +1877,17 @@ function pathRecordRef(
|
|
|
1577
1877
|
// items', or the one its envelope carries, such as the job summary in Mux's
|
|
1578
1878
|
// `{ data }`. Only a collection reads a page, so beneath the collection a
|
|
1579
1879
|
// team that holds nothing but its members is a team. Null when the answer
|
|
1580
|
-
// names no component.
|
|
1880
|
+
// names no component, or is Accepted and `accepted` excludes it.
|
|
1581
1881
|
function routeRecordRef(
|
|
1582
1882
|
def: any,
|
|
1583
1883
|
pathStr: string,
|
|
1584
1884
|
method: string,
|
|
1585
1885
|
collection: boolean,
|
|
1886
|
+
accepted: boolean = true,
|
|
1586
1887
|
): string | null {
|
|
1587
|
-
const
|
|
1588
|
-
|
|
1888
|
+
const responses = def?.paths?.[pathStr]?.[method.toLowerCase()]?.responses
|
|
1889
|
+
const schema = getResponseSchema(accepted ? successResponse(responses) :
|
|
1890
|
+
responses?.[200] ?? responses?.[201])
|
|
1589
1891
|
if (null == schema || 'object' !== typeof schema) {
|
|
1590
1892
|
return null
|
|
1591
1893
|
}
|
|
@@ -1972,14 +2274,39 @@ function refSchema(def: any, xref: string): any {
|
|
|
1972
2274
|
}
|
|
1973
2275
|
|
|
1974
2276
|
|
|
1975
|
-
//
|
|
1976
|
-
//
|
|
1977
|
-
|
|
1978
|
-
|
|
2277
|
+
// Whether a share's record is apart from an item's: both declare an `id`, and
|
|
2278
|
+
// the share has no more than half of its properties in common with the item,
|
|
2279
|
+
// since a collection's list is routinely a summary of its item's record.
|
|
2280
|
+
function distinctRecord(share: any, item: any): boolean {
|
|
2281
|
+
if (!declaresId(share) || !declaresId(item)) {
|
|
1979
2282
|
return false
|
|
1980
2283
|
}
|
|
1981
|
-
const
|
|
1982
|
-
|
|
2284
|
+
const own = Object.keys(mergedProperties(share) ?? {})
|
|
2285
|
+
const theirs = mergedProperties(item) ?? {}
|
|
2286
|
+
const common = own.filter((name) => Object.prototype.hasOwnProperty.call(theirs, name))
|
|
2287
|
+
return common.length * 2 <= own.length
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
|
|
2291
|
+
// Whether a collection share answers with a record apart from the one its item
|
|
2292
|
+
// route answers with, both read from a 200 or 201: an Accepted body may
|
|
2293
|
+
// describe the queued work rather than a resource.
|
|
2294
|
+
function distinctShare(
|
|
2295
|
+
def: any,
|
|
2296
|
+
sharePath: string,
|
|
2297
|
+
shareMethods: string[],
|
|
2298
|
+
itemPath: string,
|
|
2299
|
+
itemMethods: string[],
|
|
2300
|
+
): boolean {
|
|
2301
|
+
const share = pathRecordRef(def, sharePath, shareMethods, true, false)
|
|
2302
|
+
const item = pathRecordRef(def, itemPath, itemMethods, false, false)
|
|
2303
|
+
return null != share && null != item && share !== item &&
|
|
2304
|
+
distinctRecord(refSchema(def, share), refSchema(def, item))
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
function declaresId(schema: any): boolean {
|
|
2309
|
+
return null != mergedProperties(schema)?.id
|
|
1983
2310
|
}
|
|
1984
2311
|
|
|
1985
2312
|
|
|
@@ -2046,18 +2373,20 @@ function isListResponse(
|
|
|
2046
2373
|
}
|
|
2047
2374
|
|
|
2048
2375
|
|
|
2376
|
+
// struct's merge rewrites every list element it walks, not just the first, so
|
|
2377
|
+
// each schema is cloned: the definition shares one object per $ref target.
|
|
2049
2378
|
function resolveSchemaProperties(schema: any) {
|
|
2050
2379
|
let properties: Record<string, any> = {}
|
|
2051
2380
|
|
|
2052
2381
|
// This is definitely heuristic!
|
|
2053
2382
|
if (schema.allOf) {
|
|
2054
2383
|
for (let i = schema.allOf.length - 1; -1 < i; --i) {
|
|
2055
|
-
properties = merge([properties, schema.allOf[i].properties || {}])
|
|
2384
|
+
properties = merge([properties, clone(schema.allOf[i].properties || {})])
|
|
2056
2385
|
}
|
|
2057
2386
|
}
|
|
2058
2387
|
|
|
2059
2388
|
if (schema.properties) {
|
|
2060
|
-
properties = merge([properties, schema.properties])
|
|
2389
|
+
properties = merge([properties, clone(schema.properties)])
|
|
2061
2390
|
}
|
|
2062
2391
|
|
|
2063
2392
|
return properties
|
|
@@ -2177,6 +2506,123 @@ function isParam(partStr: string) {
|
|
|
2177
2506
|
}
|
|
2178
2507
|
|
|
2179
2508
|
|
|
2509
|
+
// A segment holding a placeholder, whole or beside other text, names no verb.
|
|
2510
|
+
function isVerb(partStr: string): boolean {
|
|
2511
|
+
return !String(partStr ?? '').includes('{')
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
|
|
2515
|
+
// A custom method (`/schedules:count`, `/users/{id}:activate`) reads as a
|
|
2516
|
+
// trailing verb segment after what it acts on.
|
|
2517
|
+
function verbParts(parts: string[]): string[] {
|
|
2518
|
+
const m = (parts[parts.length - 1] ?? '').match(/^(.+):([A-Za-z][\w-]*)$/)
|
|
2519
|
+
return null == m ? parts : [...parts.slice(0, -1), m[1], m[2]]
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
|
|
2523
|
+
// A parameter whose own name says it holds a name, such as `project_name`,
|
|
2524
|
+
// which a derived `<parent>_id` would misdescribe.
|
|
2525
|
+
function isNameParam(param: string): boolean {
|
|
2526
|
+
return /(^|_)name$/.test(snakify(normalizeFieldName(param)))
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
|
|
2530
|
+
function nameEntityParams(entity: GuideEntity) {
|
|
2531
|
+
const given: Record<string, Record<string, string>> = {}
|
|
2532
|
+
for (const [pathStr, guidePath] of Object.entries(entity.path ?? {})) {
|
|
2533
|
+
given[pathStr] = {}
|
|
2534
|
+
for (const [param, rename] of Object.entries(guidePath.rename?.param ?? {})) {
|
|
2535
|
+
given[pathStr][param] = rename.target
|
|
2536
|
+
}
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2539
|
+
const named = entityParamNames(given)
|
|
2540
|
+
|
|
2541
|
+
for (const [pathStr, guidePath] of Object.entries(entity.path ?? {})) {
|
|
2542
|
+
guidePath.rename = guidePath.rename ?? { param: {} }
|
|
2543
|
+
const renames = guidePath.rename.param = guidePath.rename.param ?? {}
|
|
2544
|
+
for (const param of Object.keys(renames)) {
|
|
2545
|
+
if (null == named[pathStr][param]) {
|
|
2546
|
+
delete renames[param]
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
for (const [param, target] of Object.entries(named[pathStr])) {
|
|
2550
|
+
if (target !== renames[param]?.target) {
|
|
2551
|
+
renames[param] = { target, why_rename: ['entity-param'] }
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
|
|
2558
|
+
// An entity's paths with the renames of their parameters, given back with one
|
|
2559
|
+
// name for each parameter at the same place in several paths: its own name,
|
|
2560
|
+
// snake-cased as the paths would, unless some path makes it the entity's `id`.
|
|
2561
|
+
function entityParamNames(
|
|
2562
|
+
paths: Record<string, Record<string, string>>
|
|
2563
|
+
): Record<string, Record<string, string>> {
|
|
2564
|
+
const out: Record<string, Record<string, string>> = {}
|
|
2565
|
+
const places: Record<string, { pathStr: string, param: string }[]> = {}
|
|
2566
|
+
|
|
2567
|
+
for (const pathStr of Object.keys(paths).sort(byCodePoint)) {
|
|
2568
|
+
out[pathStr] = { ...paths[pathStr] }
|
|
2569
|
+
const parts = verbParts(pathStr.split('/').filter((p) => '' !== p))
|
|
2570
|
+
parts.forEach((part, partI) => {
|
|
2571
|
+
if (isParam(part)) {
|
|
2572
|
+
const place = parts.slice(0, partI + 1).join('/')
|
|
2573
|
+
;(places[place] = places[place] ?? []).push({ pathStr, param: part.slice(1, -1) })
|
|
2574
|
+
}
|
|
2575
|
+
})
|
|
2576
|
+
}
|
|
2577
|
+
|
|
2578
|
+
for (const place of Object.keys(places).sort(byCodePoint)) {
|
|
2579
|
+
const uses = places[place]
|
|
2580
|
+
const names = uses.map((use) => out[use.pathStr][use.param] ?? use.param)
|
|
2581
|
+
const param = uses[0].param
|
|
2582
|
+
const snake = depluralize(snakify(normalizeFieldName(param)))
|
|
2583
|
+
const name = names.includes('id') ? null :
|
|
2584
|
+
names.includes(snake) ? snake :
|
|
2585
|
+
names.includes(param) ? param : null
|
|
2586
|
+
|
|
2587
|
+
if (null == name || names.every((n) => n === name)) {
|
|
2588
|
+
continue
|
|
2589
|
+
}
|
|
2590
|
+
|
|
2591
|
+
for (const use of uses) {
|
|
2592
|
+
const renames = out[use.pathStr]
|
|
2593
|
+
if (paramNameTaken(use.pathStr, renames, use.param, name)) {
|
|
2594
|
+
continue
|
|
2595
|
+
}
|
|
2596
|
+
if (name === use.param) {
|
|
2597
|
+
delete renames[use.param]
|
|
2598
|
+
}
|
|
2599
|
+
else {
|
|
2600
|
+
renames[use.param] = name
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
return out
|
|
2606
|
+
}
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
// As updateParamRename judges a clash: by what the path's other parameters
|
|
2610
|
+
// end up called.
|
|
2611
|
+
function paramNameTaken(
|
|
2612
|
+
pathStr: string,
|
|
2613
|
+
renames: Record<string, string>,
|
|
2614
|
+
param: string,
|
|
2615
|
+
name: string,
|
|
2616
|
+
): boolean {
|
|
2617
|
+
return Object.keys(renames).some((other) =>
|
|
2618
|
+
other !== param && renames[other] === name) ||
|
|
2619
|
+
(pathStr.match(/\{([^}]+)\}/g) || [])
|
|
2620
|
+
.map((seg) => seg.slice(1, -1))
|
|
2621
|
+
.some((other) => other !== param && null == renames[other] &&
|
|
2622
|
+
canonize(other) === name)
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
|
|
2180
2626
|
|
|
2181
2627
|
|
|
2182
2628
|
function findcmps(
|
|
@@ -2238,24 +2684,19 @@ function findPotentialSchemaRefs(
|
|
|
2238
2684
|
methodName: string,
|
|
2239
2685
|
responses: any,
|
|
2240
2686
|
envelope: Record<string, string>,
|
|
2687
|
+
answered: Record<string, boolean>,
|
|
2241
2688
|
why: string[],
|
|
2242
2689
|
) {
|
|
2243
2690
|
const xrefs: string[] = []
|
|
2244
|
-
for (const schema of
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
if ('' !==
|
|
2691
|
+
for (const schema of namingSchemas(methodName, responses, answered, envelope)) {
|
|
2692
|
+
// An envelope component names its wrapping, not the entity: the
|
|
2693
|
+
// component it carries takes its place.
|
|
2694
|
+
const ref = namingRef(schema, envelope)
|
|
2695
|
+
if (null != ref) {
|
|
2696
|
+
if (null != schema['x-ref'] && ref !== schema['x-ref']) {
|
|
2250
2697
|
why.push('envelope=' + cmpRefName(schema['x-ref']))
|
|
2251
|
-
xrefs.push(itemref)
|
|
2252
|
-
}
|
|
2253
|
-
else {
|
|
2254
|
-
xrefs.push(schema['x-ref'])
|
|
2255
2698
|
}
|
|
2256
|
-
|
|
2257
|
-
else if ('array' === schema.type && null != schema.items?.['x-ref']) {
|
|
2258
|
-
xrefs.push(schema.items?.['x-ref'])
|
|
2699
|
+
xrefs.push(ref)
|
|
2259
2700
|
}
|
|
2260
2701
|
}
|
|
2261
2702
|
|
|
@@ -2264,12 +2705,44 @@ function findPotentialSchemaRefs(
|
|
|
2264
2705
|
}
|
|
2265
2706
|
|
|
2266
2707
|
|
|
2708
|
+
const CMP_REF_RE = /\/(components\/schemas|definitions)\/(.+)$/
|
|
2709
|
+
|
|
2267
2710
|
function cmpRefName(xref: string): string {
|
|
2268
|
-
const m = xref.match(
|
|
2711
|
+
const m = xref.match(CMP_REF_RE)
|
|
2269
2712
|
return null == m ? xref : canonizeCmpName(m[2])
|
|
2270
2713
|
}
|
|
2271
2714
|
|
|
2272
2715
|
|
|
2716
|
+
// An array request body is sent from a field named for the records it lists:
|
|
2717
|
+
// its items' component, or the one their allOf parts name, cleaned as an
|
|
2718
|
+
// entity's is, else the entity itself. A pointer into a component names a
|
|
2719
|
+
// part of it, not a record. A name another route of the operation has is taken.
|
|
2720
|
+
function arrayBodyField(schema: any, entname: string, taken: string[] = []): string {
|
|
2721
|
+
const refs = [...new Set(itemRefs(schema?.items, new Set()))]
|
|
2722
|
+
const cmp = String(1 === refs.length ? refs[0] : '').match(CMP_REF_RE)?.[2]
|
|
2723
|
+
const cleaned = null == cmp || cmp.includes('/') ? '' :
|
|
2724
|
+
prefixLeadingDigit(cleanComponentName(canonizeCmpName(cmp)))
|
|
2725
|
+
const record = '' === cleaned ? entname : cleaned
|
|
2726
|
+
let name = pluralize(record)
|
|
2727
|
+
for (let n = 1; taken.includes(name); n++) {
|
|
2728
|
+
name = record + '_list' + (1 < n ? n : '')
|
|
2729
|
+
}
|
|
2730
|
+
return name
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
|
|
2734
|
+
function itemRefs(items: any, seen: Set<any>): string[] {
|
|
2735
|
+
if (null == items || 'object' !== typeof items || seen.has(items)) {
|
|
2736
|
+
return []
|
|
2737
|
+
}
|
|
2738
|
+
seen.add(items)
|
|
2739
|
+
if ('string' === typeof items['x-ref']) {
|
|
2740
|
+
return [items['x-ref']]
|
|
2741
|
+
}
|
|
2742
|
+
return (Array.isArray(items.allOf) ? items.allOf : []).flatMap((part: any) => itemRefs(part, seen))
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
|
|
2273
2746
|
function hasMethod(def: any, pathStr: string, methodName: string) {
|
|
2274
2747
|
const pathDef = def?.paths?.[pathStr]
|
|
2275
2748
|
const found = (
|
|
@@ -2285,7 +2758,17 @@ function hasMethod(def: any, pathStr: string, methodName: string) {
|
|
|
2285
2758
|
|
|
2286
2759
|
|
|
2287
2760
|
export {
|
|
2761
|
+
answeredRefs,
|
|
2762
|
+
arrayBodyField,
|
|
2763
|
+
distinctRecord,
|
|
2764
|
+
distinctShare,
|
|
2288
2765
|
heuristic01,
|
|
2766
|
+
namingRef,
|
|
2767
|
+
namingSchemas,
|
|
2289
2768
|
pathResource,
|
|
2290
2769
|
sharedRoutes,
|
|
2770
|
+
entityParamNames,
|
|
2771
|
+
isNameParam,
|
|
2772
|
+
isVerb,
|
|
2773
|
+
verbParts,
|
|
2291
2774
|
}
|