@voxgig/apidef 8.23.0 → 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.
Files changed (39) 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 +232 -85
  8. package/dist/guide/heuristic01.js.map +1 -1
  9. package/dist/transform/args.d.ts +4 -1
  10. package/dist/transform/args.js +42 -13
  11. package/dist/transform/args.js.map +1 -1
  12. package/dist/transform/body.d.ts +19 -2
  13. package/dist/transform/body.js +189 -15
  14. package/dist/transform/body.js.map +1 -1
  15. package/dist/transform/field.d.ts +3 -1
  16. package/dist/transform/field.js +159 -87
  17. package/dist/transform/field.js.map +1 -1
  18. package/dist/transform/operation.js +77 -24
  19. package/dist/transform/operation.js.map +1 -1
  20. package/dist/transform/select.js +10 -4
  21. package/dist/transform/select.js.map +1 -1
  22. package/dist/transform/top.d.ts +2 -1
  23. package/dist/transform/top.js +50 -3
  24. package/dist/transform/top.js.map +1 -1
  25. package/dist/tsconfig.tsbuildinfo +1 -1
  26. package/dist/utility.d.ts +9 -1
  27. package/dist/utility.js +139 -13
  28. package/dist/utility.js.map +1 -1
  29. package/package.json +1 -1
  30. package/src/builder/entity/entity.ts +17 -15
  31. package/src/guide/guide.ts +5 -3
  32. package/src/guide/heuristic01.ts +292 -93
  33. package/src/transform/args.ts +50 -14
  34. package/src/transform/body.ts +231 -15
  35. package/src/transform/field.ts +190 -88
  36. package/src/transform/operation.ts +97 -26
  37. package/src/transform/select.ts +13 -4
  38. package/src/transform/top.ts +59 -3
  39. package/src/utility.ts +169 -13
@@ -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,44 +757,20 @@ 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
@@ -763,18 +805,10 @@ function ResolveEntityName(spec: TaskSpec) {
763
805
  // split across entities by method (a PUT answering with a one-off
764
806
  // acknowledgement is named after it), and the parent of a verb is the
765
807
  // entity a read of the item returns.
766
- work.pathowner = work.pathowner ?? {}
767
808
  work.pathowner[pathStr] = work.pathowner[pathStr] ?? {}
768
809
  work.pathowner[pathStr][methodName] = entname
769
810
 
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
- }
811
+ markRecordOwner(work.recordowner, pathStr, parts, ment, entname)
778
812
 
779
813
  // Same guard, same reason: the formatting is the cost, not the call.
780
814
  if (debugpathOn()) {
@@ -784,6 +818,52 @@ function ResolveEntityName(spec: TaskSpec) {
784
818
  }
785
819
 
786
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
+
787
867
 
788
868
  function RenameParams(spec: TaskSpec) {
789
869
  const ctx = spec.ctx
@@ -1219,10 +1299,17 @@ function ResolveTransform(spec: TaskSpec) {
1219
1299
  // declare properties of its own beside them.
1220
1300
  const partprops = mergedProperties(resschema)
1221
1301
 
1222
- if (named && isEntityWrapperProp(partprops?.[entdesc.origname])) {
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])) {
1223
1310
  transform.res = '`body.' + entdesc.origname + '`'
1224
1311
  }
1225
- else if (named && isEntityWrapperProp(partprops?.[entdesc.name])) {
1312
+ else if (named && holdsRecord(partprops?.[entdesc.name])) {
1226
1313
  transform.res = '`body.' + entdesc.name + '`'
1227
1314
  }
1228
1315
  else if (resprops) {
@@ -1244,35 +1331,20 @@ function ResolveTransform(spec: TaskSpec) {
1244
1331
  }
1245
1332
  }
1246
1333
 
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
- }
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)
1273
1341
  }
1274
1342
  }
1275
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
+
1276
1348
  if (!isempty(transform) && null != op[opname]) {
1277
1349
  op[opname].transform = transform
1278
1350
  }
@@ -1338,6 +1410,7 @@ function entityPathMatch_tpte(
1338
1410
  data: { def: any, guide: any, work: any },
1339
1411
  pm: PathMatch,
1340
1412
  mdesc: any,
1413
+ pathowner: Record<string, Record<string, string>>,
1341
1414
  why: string[]
1342
1415
  ) {
1343
1416
  const ment = mdesc.MethodEntity
@@ -1350,7 +1423,7 @@ function entityPathMatch_tpte(
1350
1423
  let ecm = undefined
1351
1424
 
1352
1425
  if (null != ment.cmp) {
1353
- const parent = verbOnParent(data, pm, mdesc)
1426
+ const parent = verbOnParent(data, pm, mdesc, pathowner)
1354
1427
  if (null != parent) {
1355
1428
  entname = parent
1356
1429
  ment.verb_on_parent = getelem(pm, -1)
@@ -1402,6 +1475,7 @@ function verbOnParent(
1402
1475
  data: { def: any, guide: any, work: any },
1403
1476
  pm: PathMatch,
1404
1477
  mdesc: any,
1478
+ pathowner: Record<string, Record<string, string>>,
1405
1479
  ): null | string {
1406
1480
  const method = mdesc.method
1407
1481
  if (READ_METHODS.includes(method)) {
@@ -1452,7 +1526,7 @@ function verbOnParent(
1452
1526
  // known. The parent is the entity a READ of the item returns: a PUT on
1453
1527
  // the item answering with a one-off acknowledgement is named after that
1454
1528
  // and must not claim the verb. Fall back to any owner, then the literal.
1455
- const owners = data.work.pathowner?.[itemPath] ?? {}
1529
+ const owners = pathowner[itemPath] ?? {}
1456
1530
  const parent = owners.GET ?? owners.QUERY ??
1457
1531
  Object.values(owners).sort()[0] as undefined | string
1458
1532
  if (null != parent) {
@@ -1463,19 +1537,15 @@ function verbOnParent(
1463
1537
  }
1464
1538
 
1465
1539
 
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(
1540
+ // The collection of an item route whose method its tag alone names, when the
1541
+ // route answers nothing.
1542
+ function tagItemCollection(
1472
1543
  data: { def: any, work: any },
1473
1544
  mdesc: any,
1474
1545
  parts: string[],
1475
1546
  ): string | null {
1476
1547
  const ment = mdesc.MethodEntity
1477
- if ('tag' !== ment.ref || null != ment.rescmp ||
1478
- true !== data.work.recordResources[ment.cmp]) {
1548
+ if (null == ment || 'tag' !== ment.ref || null != ment.rescmp) {
1479
1549
  return null
1480
1550
  }
1481
1551
 
@@ -1488,13 +1558,108 @@ function itemOfCollection(
1488
1558
  .some(([method, mdef]: [string, any]) =>
1489
1559
  null != METHOD_CONSIDER_ORDER[method.toUpperCase()] &&
1490
1560
  null != getResponseSchema(successResponse(mdef?.responses)))
1491
- if (answers) {
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) {
1492
1578
  return null
1493
1579
  }
1494
1580
 
1495
- const collection = '/' + lits.join('/')
1496
- return data.work.recordowner?.[collection] ??
1497
- data.work.recordowner?.[collection + '/'] ?? null
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)
1498
1663
  }
1499
1664
 
1500
1665
 
@@ -1606,8 +1771,7 @@ function entityPathMatch_tpp(
1606
1771
 
1607
1772
 
1608
1773
  function getRequestBodySchema(requestBody: any) {
1609
- return requestBody?.content?.['application/json']?.schema ??
1610
- requestBody?.schema
1774
+ return jsonRequestSchema({ requestBody }) ?? requestBody?.schema
1611
1775
  }
1612
1776
 
1613
1777
  // The response an operation's result is read from, down to an Accepted
@@ -2209,18 +2373,20 @@ function isListResponse(
2209
2373
  }
2210
2374
 
2211
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.
2212
2378
  function resolveSchemaProperties(schema: any) {
2213
2379
  let properties: Record<string, any> = {}
2214
2380
 
2215
2381
  // This is definitely heuristic!
2216
2382
  if (schema.allOf) {
2217
2383
  for (let i = schema.allOf.length - 1; -1 < i; --i) {
2218
- properties = merge([properties, schema.allOf[i].properties || {}])
2384
+ properties = merge([properties, clone(schema.allOf[i].properties || {})])
2219
2385
  }
2220
2386
  }
2221
2387
 
2222
2388
  if (schema.properties) {
2223
- properties = merge([properties, schema.properties])
2389
+ properties = merge([properties, clone(schema.properties)])
2224
2390
  }
2225
2391
 
2226
2392
  return properties
@@ -2539,12 +2705,44 @@ function findPotentialSchemaRefs(
2539
2705
  }
2540
2706
 
2541
2707
 
2708
+ const CMP_REF_RE = /\/(components\/schemas|definitions)\/(.+)$/
2709
+
2542
2710
  function cmpRefName(xref: string): string {
2543
- const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/)
2711
+ const m = xref.match(CMP_REF_RE)
2544
2712
  return null == m ? xref : canonizeCmpName(m[2])
2545
2713
  }
2546
2714
 
2547
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
+
2548
2746
  function hasMethod(def: any, pathStr: string, methodName: string) {
2549
2747
  const pathDef = def?.paths?.[pathStr]
2550
2748
  const found = (
@@ -2561,6 +2759,7 @@ function hasMethod(def: any, pathStr: string, methodName: string) {
2561
2759
 
2562
2760
  export {
2563
2761
  answeredRefs,
2762
+ arrayBodyField,
2564
2763
  distinctRecord,
2565
2764
  distinctShare,
2566
2765
  heuristic01,