@voxgig/apidef 8.23.0 → 8.25.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.
Files changed (42) hide show
  1. package/dist/builder/entity/entity.d.ts +2 -1
  2. package/dist/builder/entity/entity.js +14 -11
  3. package/dist/builder/entity/entity.js.map +1 -1
  4. package/dist/guide/guide.js +5 -2
  5. package/dist/guide/guide.js.map +1 -1
  6. package/dist/guide/heuristic01.d.ts +2 -1
  7. package/dist/guide/heuristic01.js +251 -86
  8. package/dist/guide/heuristic01.js.map +1 -1
  9. package/dist/model.d.ts +1 -0
  10. package/dist/transform/args.d.ts +4 -1
  11. package/dist/transform/args.js +42 -13
  12. package/dist/transform/args.js.map +1 -1
  13. package/dist/transform/body.d.ts +21 -2
  14. package/dist/transform/body.js +228 -15
  15. package/dist/transform/body.js.map +1 -1
  16. package/dist/transform/field.d.ts +3 -1
  17. package/dist/transform/field.js +154 -91
  18. package/dist/transform/field.js.map +1 -1
  19. package/dist/transform/operation.js +77 -24
  20. package/dist/transform/operation.js.map +1 -1
  21. package/dist/transform/select.js +10 -4
  22. package/dist/transform/select.js.map +1 -1
  23. package/dist/transform/top.d.ts +2 -1
  24. package/dist/transform/top.js +50 -3
  25. package/dist/transform/top.js.map +1 -1
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/dist/utility.d.ts +10 -1
  28. package/dist/utility.js +145 -13
  29. package/dist/utility.js.map +1 -1
  30. package/model/apidef.aontu +1 -0
  31. package/package.json +1 -1
  32. package/src/builder/entity/entity.ts +17 -15
  33. package/src/guide/guide.ts +5 -3
  34. package/src/guide/heuristic01.ts +316 -94
  35. package/src/model.ts +2 -0
  36. package/src/transform/args.ts +50 -14
  37. package/src/transform/body.ts +280 -16
  38. package/src/transform/field.ts +184 -91
  39. package/src/transform/operation.ts +97 -26
  40. package/src/transform/select.ts +13 -4
  41. package/src/transform/top.ts +59 -3
  42. package/src/utility.ts +177 -13
@@ -391,9 +391,11 @@ async function buildBaseGuide(ctx: ApiDefContext) {
391
391
  if (null != op.optype) {
392
392
  guideBlocks.push(` op: ${opname}: optype: *${op.optype}`)
393
393
  }
394
- if (null != op.transform.res) {
395
- guideBlocks.push(
396
- ` op: ${opname}: transform: res: *${qt(op.transform.res)}|top`)
394
+ // aontu generates a list default only when it is not parenthesized.
395
+ const res = op.transform.res
396
+ if (null != res) {
397
+ guideBlocks.push(` op: ${opname}: transform: res: ` +
398
+ `*${'string' === typeof res ? qt(res) : qs(res)}|top`)
397
399
  }
398
400
  const reqmap: any = op.transform.req
399
401
  if (null != reqmap && 'object' === typeof reqmap) {
@@ -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
- closedBodyTransform, authExchangeOp, specSecuredByDefault,
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,
@@ -141,9 +147,10 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
141
147
  MeasureAnswered,
142
148
  { select: selectAllMethods, apply: MeasureSharing },
143
149
  MeasureShared,
150
+ { select: selectAllMethods, apply: [ResolveEntityComponent, MeasureTagItems] },
151
+ { select: selectAllMethods, apply: MeasureClaims },
144
152
  {
145
153
  select: selectAllMethods, apply: [
146
- ResolveEntityComponent,
147
154
  ResolveEntityName,
148
155
  RenameParams,
149
156
  FindActions,
@@ -238,6 +245,12 @@ function Prepare(spec: TaskSpec) {
238
245
  envelopePaths: {},
239
246
  sharing: { routes: [], records: {}, yields: {} },
240
247
  recordResources: {},
248
+ tagItems: {},
249
+ claims: {},
250
+ claimowner: {},
251
+ claimrecord: {},
252
+ pathowner: {},
253
+ recordowner: {},
241
254
  entity: {
242
255
  count: {
243
256
  seen: 0,
@@ -444,8 +457,13 @@ function MeasureShared(spec: TaskSpec) {
444
457
  }
445
458
 
446
459
 
460
+ // One list for every pass, so a method keeps the component resolved for it.
447
461
  function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
448
462
  const ctx = spec.ctx
463
+ const work = spec.data.work
464
+ if (null != work.methods) {
465
+ return work.methods
466
+ }
449
467
 
450
468
 
451
469
  let caught: any = { methods: [] }
@@ -494,7 +512,55 @@ function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
494
512
  }
495
513
  })
496
514
 
497
- return caught.methods || []
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
+ }
498
564
  }
499
565
 
500
566
 
@@ -691,51 +757,31 @@ function ResolveEntityName(spec: TaskSpec) {
691
757
 
692
758
  why_path.push(...(ment.why_cmp ?? []))
693
759
 
694
- let entname
695
-
696
760
  const pm = matchEntityPath(parts)
697
761
 
698
- if ('t/p/t/' === pm?.expr) {
699
- entname = entityPathMatch_tpte(data, pm, mdesc, why_path)
700
- }
701
-
702
- else if ('t/p/' === pm?.expr) {
703
- entname = entityPathMatch_tpe(data, pm, mdesc, why_path)
704
- }
705
-
706
- else if ('p/t/' === pm?.expr) {
707
- entname = entityPathMatch_pte(data, pm, mdesc, why_path)
708
- }
709
-
710
- else if ('t/' === pm?.expr) {
711
- entname = entityPathMatch_te(data, pm, mdesc, why_path)
712
- }
713
-
714
- else if ('t/p/p' === pm?.expr) {
715
- entname = entityPathMatch_tpp(data, pm, mdesc, why_path)
716
- }
717
-
718
- else {
719
- entname = inferEntityName(mdesc, parts, why_path)
720
- if (null == entname) {
721
- work.entity.count.unresolved++
722
- entname = 'entity' + work.entity.count.unresolved
723
- }
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
724
766
  }
725
767
 
726
768
  entname = resplitFromCmp(entname, ment.cmp as string, why_path)
727
769
 
728
- const collectionEntity = itemOfCollection(data, mdesc, parts)
729
- if (null != collectionEntity) {
730
- why_path.push('collection-record=' + collectionEntity)
731
- entname = collectionEntity
770
+ const item = itemOfCollection(data, mdesc, parts)
771
+ if (null != item) {
772
+ why_path.push(item.why + '=' + item.name)
773
+ entname = item.name
732
774
  }
733
775
 
734
776
  // Keep the pre-truncation name so a truncated-name collision can tell a
735
777
  // re-encounter of the SAME origin (merge) from a genuinely different one
736
778
  // (numeric suffix) — see ensureMinEntityName.
737
779
  const rawEntname = entname
738
- entname = ensureMinEntityName(entname, work.entmap)
780
+ // A numbered stored form, or an entity's own name a verb reads, is kept,
781
+ // since a cut would drop its number.
782
+ if (true !== item?.stored && null == work.entmap[entname]) {
783
+ entname = ensureMinEntityName(entname, work.entmap)
784
+ }
739
785
 
740
786
  const entdesc = work.entmap[entname] = work.entmap[entname] ?? {
741
787
  name: entname,
@@ -763,18 +809,10 @@ function ResolveEntityName(spec: TaskSpec) {
763
809
  // split across entities by method (a PUT answering with a one-off
764
810
  // acknowledgement is named after it), and the parent of a verb is the
765
811
  // entity a read of the item returns.
766
- work.pathowner = work.pathowner ?? {}
767
812
  work.pathowner[pathStr] = work.pathowner[pathStr] ?? {}
768
813
  work.pathowner[pathStr][methodName] = entname
769
814
 
770
- // The entity a path's own record names, where the record carries the name
771
- // the path's last segment gives.
772
- const last = parts.filter((part: string) => !isParam(part)).pop()
773
- work.recordowner = work.recordowner ?? {}
774
- if (null == work.recordowner[pathStr] && isSchemaRef(ment.ref) &&
775
- entname === ment.cmp && null != last && entname === canonize(last)) {
776
- work.recordowner[pathStr] = entname
777
- }
815
+ markRecordOwner(work.recordowner, pathStr, parts, ment, entname)
778
816
 
779
817
  // Same guard, same reason: the formatting is the cost, not the call.
780
818
  if (debugpathOn()) {
@@ -784,6 +822,52 @@ function ResolveEntityName(spec: TaskSpec) {
784
822
  }
785
823
 
786
824
 
825
+ // The name a method's path shape and component give it, or null where neither
826
+ // does. A verb on a parent takes the name pathowner holds for the parent.
827
+ function pathEntityName(
828
+ data: { def: any, guide: any, work: any },
829
+ mdesc: any,
830
+ parts: string[],
831
+ pm: PathMatch | null,
832
+ pathowner: Record<string, Record<string, string>>,
833
+ why: string[],
834
+ ): string | null {
835
+ if ('t/p/t/' === pm?.expr) {
836
+ return entityPathMatch_tpte(data, pm, mdesc, pathowner, why)
837
+ }
838
+ else if ('t/p/' === pm?.expr) {
839
+ return entityPathMatch_tpe(data, pm, mdesc, why)
840
+ }
841
+ else if ('p/t/' === pm?.expr) {
842
+ return entityPathMatch_pte(data, pm, mdesc, why)
843
+ }
844
+ else if ('t/' === pm?.expr) {
845
+ return entityPathMatch_te(data, pm, mdesc, why)
846
+ }
847
+ else if ('t/p/p' === pm?.expr) {
848
+ return entityPathMatch_tpp(data, pm, mdesc, why)
849
+ }
850
+ return inferEntityName(mdesc, parts, why)
851
+ }
852
+
853
+
854
+ // The entity a path's own record names, where the record carries the name the
855
+ // path's last segment gives.
856
+ function markRecordOwner(
857
+ recordowner: Record<string, string>,
858
+ pathStr: string,
859
+ parts: string[],
860
+ ment: Partial<MethodEntityDesc>,
861
+ entname: string,
862
+ ) {
863
+ const last = parts.filter((part: string) => !isParam(part)).pop()
864
+ if (null == recordowner[pathStr] && isSchemaRef(ment.ref) &&
865
+ entname === ment.cmp && null != last && entname === canonize(last)) {
866
+ recordowner[pathStr] = entname
867
+ }
868
+ }
869
+
870
+
787
871
 
788
872
  function RenameParams(spec: TaskSpec) {
789
873
  const ctx = spec.ctx
@@ -1219,10 +1303,17 @@ function ResolveTransform(spec: TaskSpec) {
1219
1303
  // declare properties of its own beside them.
1220
1304
  const partprops = mergedProperties(resschema)
1221
1305
 
1222
- if (named && isEntityWrapperProp(partprops?.[entdesc.origname])) {
1306
+ // A list reads records, so an object named for the entity does not win over
1307
+ // a list of them beside it, such as the matches beside the competition a
1308
+ // page of them repeats. The list is found in the same parts the name is.
1309
+ const records = 'list' === opname && null != partprops ? envelopeProp(partprops, opname) : null
1310
+ const holdsRecord = (prop: any) => isEntityWrapperProp(prop) &&
1311
+ (null == records || true === propIsList(prop))
1312
+
1313
+ if (named && holdsRecord(partprops?.[entdesc.origname])) {
1223
1314
  transform.res = '`body.' + entdesc.origname + '`'
1224
1315
  }
1225
- else if (named && isEntityWrapperProp(partprops?.[entdesc.name])) {
1316
+ else if (named && holdsRecord(partprops?.[entdesc.name])) {
1226
1317
  transform.res = '`body.' + entdesc.name + '`'
1227
1318
  }
1228
1319
  else if (resprops) {
@@ -1244,35 +1335,20 @@ function ResolveTransform(spec: TaskSpec) {
1244
1335
  }
1245
1336
  }
1246
1337
 
1247
- const reqschema = getRequestBodySchema(mdesc.requestBody)
1248
- const reqprops = reqschema?.properties
1249
- debugpath(pathStr, methodName, 'TRANSFORM-REQ', keysof(reqprops))
1250
- // A body wraps the record under the entity's name only when that is all it
1251
- // holds, and it is structured. Otherwise the name is one field of the record,
1252
- // such as the container in SaladCloud's container group create.
1253
- const wraps = (name: string) => isEntityWrapperProp(reqprops?.[name]) &&
1254
- keysof(reqprops).every((k: string) => k === name)
1255
-
1256
- if (reqschema) {
1257
- if (wraps(entdesc.origname)) {
1258
- transform.req = { [entdesc.origname]: '`reqdata`' }
1259
- }
1260
- else if (wraps(entdesc.name)) {
1261
- transform.req = { [entdesc.name]: '`reqdata`' }
1262
- }
1263
- else {
1264
- // A CLOSED body schema names every property the server will accept, so
1265
- // the body is those properties — not the whole request payload. The
1266
- // payload also carries the op's PATH params (`id` for
1267
- // `PUT /item/{id}`), and a closed shape rejects the entire request over
1268
- // that one extra key: every update came back 400 with `invalid-data`.
1269
- const body = closedBodyTransform(reqschema)
1270
- if (null != body) {
1271
- transform.req = body
1272
- }
1338
+ // An item that is the entity's own component is the record, as above.
1339
+ const itemref = resschema?.items?.['x-ref']
1340
+ if (null == transform.res && 'list' === opname &&
1341
+ (null == itemref || null == entdesc.cmp || cmpRefName(itemref) !== entdesc.cmp)) {
1342
+ const key = itemEnvelopeKey(resschema, [entdesc.origname, entdesc.name])
1343
+ if (null != key) {
1344
+ transform.res = itemEnvelopeTransform(key)
1273
1345
  }
1274
1346
  }
1275
1347
 
1348
+ const reqschema = getRequestBodySchema(mdesc.requestBody)
1349
+ debugpath(pathStr, methodName, 'TRANSFORM-REQ', keysof(reqschema?.properties))
1350
+ transform.req = bodyRequestTransform(reqschema, [entdesc.origname, entdesc.name])
1351
+
1276
1352
  if (!isempty(transform) && null != op[opname]) {
1277
1353
  op[opname].transform = transform
1278
1354
  }
@@ -1338,6 +1414,7 @@ function entityPathMatch_tpte(
1338
1414
  data: { def: any, guide: any, work: any },
1339
1415
  pm: PathMatch,
1340
1416
  mdesc: any,
1417
+ pathowner: Record<string, Record<string, string>>,
1341
1418
  why: string[]
1342
1419
  ) {
1343
1420
  const ment = mdesc.MethodEntity
@@ -1350,7 +1427,7 @@ function entityPathMatch_tpte(
1350
1427
  let ecm = undefined
1351
1428
 
1352
1429
  if (null != ment.cmp) {
1353
- const parent = verbOnParent(data, pm, mdesc)
1430
+ const parent = verbOnParent(data, pm, mdesc, pathowner)
1354
1431
  if (null != parent) {
1355
1432
  entname = parent
1356
1433
  ment.verb_on_parent = getelem(pm, -1)
@@ -1402,6 +1479,7 @@ function verbOnParent(
1402
1479
  data: { def: any, guide: any, work: any },
1403
1480
  pm: PathMatch,
1404
1481
  mdesc: any,
1482
+ pathowner: Record<string, Record<string, string>>,
1405
1483
  ): null | string {
1406
1484
  const method = mdesc.method
1407
1485
  if (READ_METHODS.includes(method)) {
@@ -1452,7 +1530,7 @@ function verbOnParent(
1452
1530
  // known. The parent is the entity a READ of the item returns: a PUT on
1453
1531
  // the item answering with a one-off acknowledgement is named after that
1454
1532
  // and must not claim the verb. Fall back to any owner, then the literal.
1455
- const owners = data.work.pathowner?.[itemPath] ?? {}
1533
+ const owners = pathowner[itemPath] ?? {}
1456
1534
  const parent = owners.GET ?? owners.QUERY ??
1457
1535
  Object.values(owners).sort()[0] as undefined | string
1458
1536
  if (null != parent) {
@@ -1463,19 +1541,15 @@ function verbOnParent(
1463
1541
  }
1464
1542
 
1465
1543
 
1466
- // The entity of an item route's collection, for a method on the item route
1467
- // named by its tag alone, when the route answers nothing and the tag names
1468
- // another resource with a record of its own, which the item's operations
1469
- // would join: the collection's record then names the item too, as GitHub's
1470
- // repository invitations do beside its repositories.
1471
- function itemOfCollection(
1544
+ // The collection of an item route whose method its tag alone names, when the
1545
+ // route answers nothing.
1546
+ function tagItemCollection(
1472
1547
  data: { def: any, work: any },
1473
1548
  mdesc: any,
1474
1549
  parts: string[],
1475
1550
  ): string | null {
1476
1551
  const ment = mdesc.MethodEntity
1477
- if ('tag' !== ment.ref || null != ment.rescmp ||
1478
- true !== data.work.recordResources[ment.cmp]) {
1552
+ if (null == ment || 'tag' !== ment.ref || null != ment.rescmp) {
1479
1553
  return null
1480
1554
  }
1481
1555
 
@@ -1488,13 +1562,127 @@ function itemOfCollection(
1488
1562
  .some(([method, mdef]: [string, any]) =>
1489
1563
  null != METHOD_CONSIDER_ORDER[method.toUpperCase()] &&
1490
1564
  null != getResponseSchema(successResponse(mdef?.responses)))
1491
- if (answers) {
1565
+ return answers ? null : '/' + lits.join('/')
1566
+ }
1567
+
1568
+
1569
+ // The collection that names a tag-named item route, given the record owners
1570
+ // known so far: by its record, where the tag names another resource with one
1571
+ // (GitHub's invitations), or by its segment, where the routes the tag gathers
1572
+ // from several collections would share a selector (Apicurio's well-known
1573
+ // routes). Null where the route keeps the name its own path gives.
1574
+ function collectionNaming(
1575
+ data: { def: any, work: any },
1576
+ mdesc: any,
1577
+ parts: string[],
1578
+ recordowner: Record<string, string>,
1579
+ ): { collection: string, record?: string } | null {
1580
+ const collection = tagItemCollection(data, mdesc, parts)
1581
+ if (null == collection) {
1492
1582
  return null
1493
1583
  }
1494
1584
 
1495
- const collection = '/' + lits.join('/')
1496
- return data.work.recordowner?.[collection] ??
1497
- data.work.recordowner?.[collection + '/'] ?? null
1585
+ const work = data.work
1586
+ const cmp = mdesc.MethodEntity.cmp
1587
+ if (true === work.recordResources[cmp]) {
1588
+ const record = recordowner[collection] ?? recordowner[collection + '/']
1589
+ return null == record ? null : { collection, record }
1590
+ }
1591
+
1592
+ return tagCollides(data.def, work.tagItems[cmp] ?? {}) ? { collection } : null
1593
+ }
1594
+
1595
+
1596
+ // The entity of an item route its collection names: the collection's record,
1597
+ // or its segment. A route outside the collection that has the segment puts the
1598
+ // tag before it, and one that has that name too a number from 2 after the
1599
+ // stored form, which a cut would otherwise drop.
1600
+ function itemOfCollection(
1601
+ data: { def: any, work: any },
1602
+ mdesc: any,
1603
+ parts: string[],
1604
+ ): { name: string, why: string, stored?: boolean } | null {
1605
+ const naming = collectionNaming(data, mdesc, parts, data.work.recordowner)
1606
+ if (null == naming) {
1607
+ return null
1608
+ }
1609
+ if (null != naming.record) {
1610
+ return { name: naming.record, why: 'collection-record' }
1611
+ }
1612
+
1613
+ const work = data.work
1614
+ const collection = naming.collection
1615
+ const segment = canonize(collection.substring(collection.lastIndexOf('/') + 1))
1616
+ const tagged = mdesc.MethodEntity.cmp + '_' + segment
1617
+ for (const name of [segment, tagged]) {
1618
+ if (!segmentHeld(work, name, collection)) {
1619
+ return { name, why: 'collection-segment' }
1620
+ }
1621
+ }
1622
+ const stored = ensureMinEntityName(tagged, {})
1623
+ let i = 2
1624
+ while (keyHeld(work, [stored + i], collection)) {
1625
+ i++
1626
+ }
1627
+ return { name: stored + i, why: 'collection-segment', stored: true }
1628
+ }
1629
+
1630
+
1631
+ // A route outside the collection has the name: one named already under its
1632
+ // stored form or the key it would take now, or one that claims the name, its
1633
+ // stored form or that key.
1634
+ function segmentHeld(work: any, name: string, collection: string): boolean {
1635
+ return keyHeld(work, [name, ensureMinEntityName(name, {}),
1636
+ ensureMinEntityName(name, work.entmap)], collection)
1637
+ }
1638
+
1639
+
1640
+ // A route outside the collection is named under one of the keys or claims one.
1641
+ // An item route its collection names makes no claim, so it counts only once
1642
+ // named.
1643
+ function keyHeld(work: any, keys: string[], collection: string): boolean {
1644
+ return keys.some((key) => [work.entmap[key]?.path, work.claims[key]]
1645
+ .some((paths: Record<string, any> | undefined) =>
1646
+ Object.keys(paths ?? {}).some((path) => outsideCollection(path, collection))))
1647
+ }
1648
+
1649
+
1650
+ // A route under the collection is its own when it has no parameter or its
1651
+ // first parameter follows the collection: one under a collection nested in it
1652
+ // is outside.
1653
+ function outsideCollection(path: string, collection: string): boolean {
1654
+ if (!(path + '/').startsWith(collection + '/')) {
1655
+ return true
1656
+ }
1657
+ const parts = path.split('/').filter((part) => '' !== part)
1658
+ const first = parts.findIndex(isParam)
1659
+ return -1 < first && collection !== '/' + parts.slice(0, first).join('/')
1660
+ }
1661
+
1662
+
1663
+ // Two of a tag's collections whose item routes take the same parameters, or
1664
+ // that are both read, would share a selector on the tag's entity.
1665
+ function tagCollides(def: any, items: Record<string, Record<string, boolean>>): boolean {
1666
+ const seen: Record<string, boolean> = {}
1667
+ let reads = 0
1668
+ for (const [collection, params] of Object.entries(items)) {
1669
+ for (const key of Object.keys(params)) {
1670
+ if (seen[key]) {
1671
+ return true
1672
+ }
1673
+ seen[key] = true
1674
+ }
1675
+ if (pathReads(def.paths?.[collection]) || pathReads(def.paths?.[collection + '/'])) {
1676
+ reads++
1677
+ }
1678
+ }
1679
+ return 1 < reads
1680
+ }
1681
+
1682
+
1683
+ function pathReads(pathdef: any): boolean {
1684
+ return Object.entries(pathdef ?? {}).some(([method, mdef]: [string, any]) =>
1685
+ 'GET' === method.toUpperCase() && null != mdef)
1498
1686
  }
1499
1687
 
1500
1688
 
@@ -1606,8 +1794,7 @@ function entityPathMatch_tpp(
1606
1794
 
1607
1795
 
1608
1796
  function getRequestBodySchema(requestBody: any) {
1609
- return requestBody?.content?.['application/json']?.schema ??
1610
- requestBody?.schema
1797
+ return jsonRequestSchema({ requestBody }) ?? requestBody?.schema
1611
1798
  }
1612
1799
 
1613
1800
  // The response an operation's result is read from, down to an Accepted
@@ -2209,18 +2396,20 @@ function isListResponse(
2209
2396
  }
2210
2397
 
2211
2398
 
2399
+ // struct's merge rewrites every list element it walks, not just the first, so
2400
+ // each schema is cloned: the definition shares one object per $ref target.
2212
2401
  function resolveSchemaProperties(schema: any) {
2213
2402
  let properties: Record<string, any> = {}
2214
2403
 
2215
2404
  // This is definitely heuristic!
2216
2405
  if (schema.allOf) {
2217
2406
  for (let i = schema.allOf.length - 1; -1 < i; --i) {
2218
- properties = merge([properties, schema.allOf[i].properties || {}])
2407
+ properties = merge([properties, clone(schema.allOf[i].properties || {})])
2219
2408
  }
2220
2409
  }
2221
2410
 
2222
2411
  if (schema.properties) {
2223
- properties = merge([properties, schema.properties])
2412
+ properties = merge([properties, clone(schema.properties)])
2224
2413
  }
2225
2414
 
2226
2415
  return properties
@@ -2539,12 +2728,44 @@ function findPotentialSchemaRefs(
2539
2728
  }
2540
2729
 
2541
2730
 
2731
+ const CMP_REF_RE = /\/(components\/schemas|definitions)\/(.+)$/
2732
+
2542
2733
  function cmpRefName(xref: string): string {
2543
- const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/)
2734
+ const m = xref.match(CMP_REF_RE)
2544
2735
  return null == m ? xref : canonizeCmpName(m[2])
2545
2736
  }
2546
2737
 
2547
2738
 
2739
+ // An array request body is sent from a field named for the records it lists:
2740
+ // its items' component, or the one their allOf parts name, cleaned as an
2741
+ // entity's is, else the entity itself. A pointer into a component names a
2742
+ // part of it, not a record. A name another route of the operation has is taken.
2743
+ function arrayBodyField(schema: any, entname: string, taken: string[] = []): string {
2744
+ const refs = [...new Set(itemRefs(schema?.items, new Set()))]
2745
+ const cmp = String(1 === refs.length ? refs[0] : '').match(CMP_REF_RE)?.[2]
2746
+ const cleaned = null == cmp || cmp.includes('/') ? '' :
2747
+ prefixLeadingDigit(cleanComponentName(canonizeCmpName(cmp)))
2748
+ const record = '' === cleaned ? entname : cleaned
2749
+ let name = pluralize(record)
2750
+ for (let n = 1; taken.includes(name); n++) {
2751
+ name = record + '_list' + (1 < n ? n : '')
2752
+ }
2753
+ return name
2754
+ }
2755
+
2756
+
2757
+ function itemRefs(items: any, seen: Set<any>): string[] {
2758
+ if (null == items || 'object' !== typeof items || seen.has(items)) {
2759
+ return []
2760
+ }
2761
+ seen.add(items)
2762
+ if ('string' === typeof items['x-ref']) {
2763
+ return [items['x-ref']]
2764
+ }
2765
+ return (Array.isArray(items.allOf) ? items.allOf : []).flatMap((part: any) => itemRefs(part, seen))
2766
+ }
2767
+
2768
+
2548
2769
  function hasMethod(def: any, pathStr: string, methodName: string) {
2549
2770
  const pathDef = def?.paths?.[pathStr]
2550
2771
  const found = (
@@ -2561,6 +2782,7 @@ function hasMethod(def: any, pathStr: string, methodName: string) {
2561
2782
 
2562
2783
  export {
2563
2784
  answeredRefs,
2785
+ arrayBodyField,
2564
2786
  distinctRecord,
2565
2787
  distinctShare,
2566
2788
  heuristic01,
package/src/model.ts CHANGED
@@ -180,6 +180,8 @@ type ModelPoint = {
180
180
  $action?: string
181
181
  }
182
182
  rb?: ModelBody
183
+ // false for none, as aontu drops an optional key holding an empty list.
184
+ bf?: string[] | false
183
185
  rs?: ModelBody
184
186
  }
185
187