@voxgig/apidef 8.15.2 → 8.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/apidef.js +15 -0
  2. package/dist/apidef.js.map +1 -1
  3. package/dist/builder/entity/entity.js +8 -5
  4. package/dist/builder/entity/entity.js.map +1 -1
  5. package/dist/builder/entity/info.js +1 -1
  6. package/dist/builder/entity/info.js.map +1 -1
  7. package/dist/builder/flow.js +3 -3
  8. package/dist/builder/flow.js.map +1 -1
  9. package/dist/cli.js +8 -10
  10. package/dist/cli.js.map +1 -1
  11. package/dist/desc.d.ts +2 -0
  12. package/dist/guide/guide.d.ts +14 -1
  13. package/dist/guide/guide.js +106 -62
  14. package/dist/guide/guide.js.map +1 -1
  15. package/dist/guide/heuristic01.d.ts +9 -1
  16. package/dist/guide/heuristic01.js +290 -71
  17. package/dist/guide/heuristic01.js.map +1 -1
  18. package/dist/parse.d.ts +4 -1
  19. package/dist/parse.js +183 -133
  20. package/dist/parse.js.map +1 -1
  21. package/dist/refcount.d.ts +6 -0
  22. package/dist/refcount.js +215 -0
  23. package/dist/refcount.js.map +1 -0
  24. package/dist/transform/entity.d.ts +1 -1
  25. package/dist/transform/entity.js +12 -3
  26. package/dist/transform/entity.js.map +1 -1
  27. package/dist/transform/field.js +1 -1
  28. package/dist/transform/field.js.map +1 -1
  29. package/dist/transform/operation.js +1 -1
  30. package/dist/transform/operation.js.map +1 -1
  31. package/dist/transform/top.js +1 -1
  32. package/dist/transform/top.js.map +1 -1
  33. package/dist/tsconfig.tsbuildinfo +1 -1
  34. package/dist/utility.d.ts +3 -1
  35. package/dist/utility.js +85 -6
  36. package/dist/utility.js.map +1 -1
  37. package/model/{guide.aon → guide.aontu} +1 -1
  38. package/package.json +3 -3
  39. package/src/apidef.ts +20 -1
  40. package/src/builder/entity/entity.ts +8 -5
  41. package/src/builder/entity/info.ts +1 -1
  42. package/src/builder/flow.ts +3 -3
  43. package/src/cli.ts +8 -10
  44. package/src/desc.ts +3 -0
  45. package/src/guide/guide.ts +136 -71
  46. package/src/guide/heuristic01.ts +358 -90
  47. package/src/parse.ts +221 -126
  48. package/src/refcount.ts +244 -0
  49. package/src/transform/entity.ts +16 -5
  50. package/src/transform/field.ts +1 -1
  51. package/src/transform/operation.ts +1 -1
  52. package/src/transform/top.ts +1 -1
  53. package/src/types.ts +2 -2
  54. package/src/utility.ts +102 -6
  55. /package/model/{apidef.aon → apidef.aontu} +0 -0
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.heuristic01 = heuristic01;
4
+ exports.pathResource = pathResource;
5
+ exports.sharedRoutes = sharedRoutes;
4
6
  const ordu_1 = require("ordu");
5
7
  const jostraca_1 = require("jostraca");
6
8
  const struct_1 = require("@voxgig/struct");
@@ -8,11 +10,12 @@ const utility_1 = require("../utility");
8
10
  const utility_2 = require("../utility");
9
11
  const jostraca_2 = require("jostraca");
10
12
  const entity_1 = require("../transform/entity");
13
+ const refcount_1 = require("../refcount");
11
14
  const KONSOLE_LOG = console['log'];
12
15
  // Log non - fatal wierdness.
13
16
  const dlog = (0, utility_2.getdlog)('apidef', __filename);
14
- // Schema components that occur less than this rate(over total method count) qualify
15
- // as unique entities, not shared schemas
17
+ // A schema whose per-use occurrences, over the method count or over the path
18
+ // count, fall below these rates names an entity rather than a shared shape.
16
19
  const IS_ENTCMP_METHOD_RATE = 0.21;
17
20
  const IS_ENTCMP_PATH_RATE = 0.41;
18
21
  const METHOD_IDOP = {
@@ -28,6 +31,13 @@ const METHOD_IDOP = {
28
31
  HEAD: 'head',
29
32
  OPTIONS: 'OPTIONS',
30
33
  };
34
+ // Tried in order: the first shape a path matches decides how its entity is named.
35
+ const ENTITY_PATH_SHAPES = ['t/p/t/', 't/p/', 'p/t/', 't/', 't/p/p'];
36
+ // The matched part that gives each shape its name.
37
+ const PATH_NAME_INDEX = {
38
+ 't/p/t/': 2, 't/p/': 0, 'p/t/': 1, 't/': 0, 't/p/p': 0,
39
+ };
40
+ const READ_METHODS = ['GET', 'QUERY', 'HEAD', 'OPTIONS'];
31
41
  const METHOD_CONSIDER_ORDER = {
32
42
  'GET': 100,
33
43
  'QUERY': 150,
@@ -49,6 +59,10 @@ async function heuristic01(ctx) {
49
59
  ]
50
60
  },
51
61
  { select: selectCmpXrefs, apply: MeasureRef },
62
+ { select: selectAllMethods, apply: MeasureEnvelope },
63
+ MeasureEnvelopeItems,
64
+ { select: selectAllMethods, apply: MeasureSharing },
65
+ MeasureShared,
52
66
  {
53
67
  select: selectAllMethods, apply: [
54
68
  ResolveEntityComponent,
@@ -67,7 +81,7 @@ async function heuristic01(ctx) {
67
81
  throw result.err;
68
82
  }
69
83
  const guide = result.data.guide;
70
- (0, entity_1.mergeCollectionPaths)(guide, ctx.log);
84
+ guide.metrics.count.entity -= (0, entity_1.mergeCollectionPaths)(guide, ctx.log).length;
71
85
  const metrics = guide.metrics;
72
86
  const entities = Object.values(guide.entity);
73
87
  const entityCount = entities.length;
@@ -118,6 +132,8 @@ function Prepare(spec) {
118
132
  work: {
119
133
  pathmap: {},
120
134
  entmap: {},
135
+ envelope: {},
136
+ sharing: { routes: [], records: {}, yields: {} },
121
137
  entity: {
122
138
  count: {
123
139
  seen: 0,
@@ -175,32 +191,99 @@ function PreparePath(spec) {
175
191
  work.pathmap[pathstr] = pathdesc;
176
192
  }
177
193
  function selectCmpXrefs(_source, spec) {
178
- const out = (0, utility_2.find)(spec.ctx.def, 'x-ref')
179
- .filter(xref => xref.val.match(/\/(components\/schemas|definitions)\//));
180
- return out;
194
+ const counts = (0, refcount_1.countRefs)(spec.ctx.def);
195
+ return Object.keys(counts)
196
+ .sort(refcount_1.byCodePoint)
197
+ .filter(val => val.match(/\/(components\/schemas|definitions)\//))
198
+ .map(val => ({ val, count: counts[val] }));
181
199
  }
182
200
  function MeasureRef(spec) {
183
201
  const guide = spec.data.guide;
184
202
  const metrics = guide.metrics;
185
- let m = spec.node.val.val.match(/\/(components\/schemas|definitions)\/(.+)$/);
203
+ const xref = spec.node.val;
204
+ let m = xref.val.match(/\/(components\/schemas|definitions)\/(.+)$/);
186
205
  if (m) {
187
206
  const name = (0, utility_2.canonizeCmpName)(m[2]);
188
207
  if (null == metrics.count.origcmprefs[name]) {
189
208
  metrics.count.cmp++;
190
209
  metrics.count.origcmprefs[name] = 0;
191
210
  }
192
- metrics.count.origcmprefs[name]++;
211
+ metrics.count.origcmprefs[name] =
212
+ (0, refcount_1.satAdd)(metrics.count.origcmprefs[name], xref.count);
193
213
  if (null == metrics.found.cmp[name]) {
194
214
  metrics.found.cmp[name] = { orig: m[2] };
195
215
  }
196
216
  }
197
217
  }
218
+ // Being an envelope belongs to the component, not to one operation: it names
219
+ // through the record it carries only when every operation answering with it
220
+ // unwraps it, so the operations on one resource are never split between the
221
+ // record's name and the envelope's. An operation unwraps only the response
222
+ // ResolveTransform reads. The entry is the item's reference, or '' for none.
223
+ function MeasureEnvelope(spec) {
224
+ const work = spec.data.work;
225
+ const mdesc = spec.node.val;
226
+ const opname = methodOpname(mdesc, matchEntityPath(work.pathmap[mdesc.path].parts), []);
227
+ const unwrapref = getResponseSchema(successResponse(mdesc.responses))?.['x-ref'];
228
+ for (const schema of successSchemas(mdesc.responses)) {
229
+ const xref = schema['x-ref'];
230
+ if (null != xref) {
231
+ const itemref = null == opname || xref !== unwrapref ? null :
232
+ (0, utility_1.envelopeItemRef)(schema, opname);
233
+ work.envelope[xref] = '' === work.envelope[xref] || null == itemref ? '' : itemref;
234
+ }
235
+ }
236
+ }
237
+ // An item carried by more than one envelope is named by none of them: the
238
+ // envelopes' own names are then what tell the resources apart.
239
+ function MeasureEnvelopeItems(spec) {
240
+ const envelope = spec.data.work.envelope;
241
+ const carriers = {};
242
+ for (const itemref of Object.values(envelope)) {
243
+ carriers[itemref] = (carriers[itemref] ?? 0) + 1;
244
+ }
245
+ for (const xref of Object.keys(envelope)) {
246
+ if (1 < carriers[envelope[xref]]) {
247
+ envelope[xref] = '';
248
+ }
249
+ }
250
+ }
251
+ // Records every route whose operation answers with a response component, and
252
+ // whether that component declares an `id`, before any method is named.
253
+ function MeasureSharing(spec) {
254
+ const work = spec.data.work;
255
+ const mdesc = spec.node.val;
256
+ const parts = work.pathmap[mdesc.path].parts;
257
+ const op = methodOpname(mdesc, matchEntityPath(parts), []) ?? '';
258
+ const sharing = work.sharing;
259
+ const xrefs = findPotentialSchemaRefs(mdesc.path, mdesc.method, mdesc.responses, work.envelope, []);
260
+ for (const xref of xrefs) {
261
+ const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/);
262
+ if (null != m) {
263
+ const cmp = (0, utility_2.canonizeCmpName)(m[2]);
264
+ sharing.routes.push({ cmp, method: mdesc.method, path: mdesc.path, op });
265
+ sharing.records[cmp] = true === sharing.records[cmp] || declaresId(refSchema(spec.data.def, xref));
266
+ }
267
+ }
268
+ }
269
+ function MeasureShared(spec) {
270
+ const sharing = spec.data.work.sharing;
271
+ const records = Object.keys(sharing.records).filter((cmp) => sharing.records[cmp]);
272
+ for (const key of sharedRoutes(sharing.routes, records)) {
273
+ sharing.yields[key] = true;
274
+ }
275
+ }
198
276
  function selectAllMethods(_source, spec) {
199
277
  const ctx = spec.ctx;
200
278
  let caught = { methods: [] };
201
279
  for (const [path, pdef] of (0, utility_2.sortedEntries)(ctx.def.paths)) {
202
280
  for (const [m, mdef] of (0, utility_2.sortedEntries)(pdef)) {
203
281
  const method = m.toUpperCase();
282
+ // A path item also holds parameters, servers, summary and the like.
283
+ if (null == METHOD_CONSIDER_ORDER[method] ||
284
+ null == mdef || 'object' !== typeof mdef || Array.isArray(mdef)) {
285
+ continue;
286
+ }
204
287
  caught.methods.push({
205
288
  path,
206
289
  method,
@@ -245,7 +328,7 @@ function ResolveEntityComponent(spec) {
245
328
  const parts = work.pathmap[pathStr].parts;
246
329
  let why_cmp = [];
247
330
  let responses = methodDef.responses;
248
- let origxrefs = findPotentialSchemaRefs(pathStr, methodName, responses).map(val => ({
331
+ let origxrefs = findPotentialSchemaRefs(pathStr, methodName, responses, work.envelope, why_cmp).map(val => ({
249
332
  val
250
333
  }));
251
334
  let cmpxrefs = origxrefs
@@ -332,6 +415,8 @@ function ResolveEntityComponent(spec) {
332
415
  }
333
416
  else if ((pathStr.includes('/' + ftag + '/') || pathStr.includes('/' + tagdesc.canon + '/'))
334
417
  && out.cmp !== tagdesc.canon) {
418
+ const rescmp = out.cmp;
419
+ const rescmpoccur = metrics.count.origcmprefs[out.origcmpref ?? ''] ?? 0;
335
420
  out = makeMethodEntityDesc({
336
421
  ref: 'tag',
337
422
  cmp: tagdesc.canon,
@@ -339,6 +424,8 @@ function ResolveEntityComponent(spec) {
339
424
  why_cmp,
340
425
  entname: tagdesc.canon,
341
426
  });
427
+ out.rescmp = rescmp;
428
+ out.rescmpoccur = rescmpoccur;
342
429
  why_cmp.push('tag/path=' + out.cmp);
343
430
  }
344
431
  }
@@ -373,20 +460,20 @@ function ResolveEntityName(spec) {
373
460
  }
374
461
  why_path.push(...(ment.why_cmp ?? []));
375
462
  let entname;
376
- let pm = undefined;
377
- if (pm = (0, utility_2.pathMatch)(parts, 't/p/t/')) {
463
+ const pm = matchEntityPath(parts);
464
+ if ('t/p/t/' === pm?.expr) {
378
465
  entname = entityPathMatch_tpte(data, pm, mdesc, why_path);
379
466
  }
380
- else if (pm = (0, utility_2.pathMatch)(parts, 't/p/')) {
467
+ else if ('t/p/' === pm?.expr) {
381
468
  entname = entityPathMatch_tpe(data, pm, mdesc, why_path);
382
469
  }
383
- else if (pm = (0, utility_2.pathMatch)(parts, 'p/t/')) {
470
+ else if ('p/t/' === pm?.expr) {
384
471
  entname = entityPathMatch_pte(data, pm, mdesc, why_path);
385
472
  }
386
- else if (pm = (0, utility_2.pathMatch)(parts, 't/')) {
473
+ else if ('t/' === pm?.expr) {
387
474
  entname = entityPathMatch_te(data, pm, mdesc, why_path);
388
475
  }
389
- else if (pm = (0, utility_2.pathMatch)(parts, 't/p/p')) {
476
+ else if ('t/p/p' === pm?.expr) {
390
477
  entname = entityPathMatch_tpp(data, pm, mdesc, why_path);
391
478
  }
392
479
  else {
@@ -436,8 +523,6 @@ function ResolveEntityName(spec) {
436
523
  function RenameParams(spec) {
437
524
  const ctx = spec.ctx;
438
525
  const data = spec.data;
439
- const guide = data.guide;
440
- const metrics = guide.metrics;
441
526
  const mdesc = spec.node.val;
442
527
  const ment = mdesc.MethodEntity;
443
528
  const pathStr = mdesc.path;
@@ -477,10 +562,6 @@ function RenameParams(spec) {
477
562
  ment.why_rename = paramRenameCapture.why;
478
563
  return;
479
564
  }
480
- const cmpname = mdesc.cmp;
481
- const considerCmp = null != cmpname &&
482
- 0 < metrics.count.uniqschema &&
483
- mdesc.method_rate < IS_ENTCMP_METHOD_RATE;
484
565
  const origParams = [];
485
566
  for (let partI = 0; partI < parts.length; partI++) {
486
567
  let partStr = parts[partI];
@@ -519,18 +600,12 @@ function RenameParams(spec) {
519
600
  why.push('maybe-parent');
520
601
  // actually an action
521
602
  if (secondLastPart
522
- && ((parentName !== entdesc.name
523
- && entdesc.name.startsWith(parentName + '_'))
524
- // || parentName === cmp.name
525
- || parentName === cmpname)) {
603
+ && parentName !== entdesc.name
604
+ && entdesc.name.startsWith(parentName + '_')) {
526
605
  updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'action-parent:' + entdesc.name);
527
606
  why.push('action');
528
607
  updateAction(methodName, oldParam, parts[partI + 1], entdesc, pathDesc, 'action-not-parent');
529
608
  }
530
- else if (hasParent && parentName === cmpname) {
531
- updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'id-parent-cmp');
532
- why.push('id-parent-cmp');
533
- }
534
609
  else if (hasParent && parentName === entdesc.name) {
535
610
  updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'id-parent-ent');
536
611
  why.push('id-parent-ent');
@@ -548,10 +623,8 @@ function RenameParams(spec) {
548
623
  && not_exact_id
549
624
  && (!hasParent
550
625
  || (parentName === entdesc.name
551
- || entdesc.name.endsWith('_' + parentName)))
552
- && (!considerCmp || cmpname === entdesc.name)) {
553
- updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'end-id;' + methodName + ';parent=' + hasParent + '/' + parentName +
554
- ';cmp=' + considerCmp + (null == cmpname ? '' : '/' + cmpname));
626
+ || entdesc.name.endsWith('_' + parentName)))) {
627
+ updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'end-id;' + methodName + ';parent=' + hasParent + '/' + parentName);
555
628
  why.push('end-id');
556
629
  }
557
630
  // Mot at end, has preceding non-param part.
@@ -574,12 +647,6 @@ function RenameParams(spec) {
574
647
  why.push('not-end-action');
575
648
  }
576
649
  }
577
- // Primary ent id not at end!
578
- else if (hasParent
579
- && parentName === cmpname) {
580
- updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'id-not-last');
581
- why.push('id-not-last');
582
- }
583
650
  // Not primary ent.
584
651
  else {
585
652
  why.push('default');
@@ -609,9 +676,6 @@ function RenameParams(spec) {
609
676
  parentName,
610
677
  not_exact_id,
611
678
  probably_an_id,
612
- considerCmp,
613
- cmp: mdesc.cmp,
614
- cmpname,
615
679
  paramRenameCapture,
616
680
  entdesc
617
681
  });
@@ -694,7 +758,7 @@ function ResolveOperation(spec) {
694
758
  return;
695
759
  }
696
760
  if ('load' === standard_opname) {
697
- const islist = isListResponse(mdesc, pathStr, why_op);
761
+ const islist = isListResponse(mdesc, ment.pm, pathStr, why_op);
698
762
  opname = islist ? 'list' : opname;
699
763
  }
700
764
  else {
@@ -743,8 +807,7 @@ function ResolveTransform(spec) {
743
807
  req: undefined,
744
808
  res: undefined,
745
809
  };
746
- const resokdef = mdesc.responses?.[200] || mdesc.responses?.[201];
747
- const resprops = getResponseSchema(resokdef)?.properties;
810
+ const resprops = getResponseSchema(successResponse(mdesc.responses))?.properties;
748
811
  (0, utility_2.debugpath)(pathStr, methodName, 'TRANSFORM-RES', (0, struct_1.keysof)(resprops));
749
812
  if (resprops) {
750
813
  if ((0, utility_1.isEntityWrapperProp)(resprops[entdesc.origname])) {
@@ -831,7 +894,7 @@ function BuildEntity(spec) {
831
894
  }
832
895
  function entityPathMatch_tpte(data, pm, mdesc, why) {
833
896
  const ment = mdesc.MethodEntity;
834
- const pathNameIndex = 2;
897
+ const pathNameIndex = PATH_NAME_INDEX['t/p/t/'];
835
898
  why.push('path=t/p/t/');
836
899
  const origPathName = pm[pathNameIndex];
837
900
  let entname = (0, utility_2.canonize)(origPathName);
@@ -881,11 +944,11 @@ function endsWithCmp(data, pm) {
881
944
  }
882
945
  function verbOnParent(data, pm, mdesc) {
883
946
  const method = mdesc.method;
884
- if ('GET' === method || 'QUERY' === method || 'HEAD' === method || 'OPTIONS' === method) {
947
+ if (READ_METHODS.includes(method)) {
885
948
  return null;
886
949
  }
887
950
  const ment = mdesc.MethodEntity;
888
- if (1 < (ment.cmpoccur ?? 0)) {
951
+ if (1 < (ment.rescmpoccur ?? ment.cmpoccur ?? 0)) {
889
952
  return null;
890
953
  }
891
954
  const lit = (0, jostraca_2.snakify)((0, struct_1.getelem)(pm, -1));
@@ -893,7 +956,7 @@ function verbOnParent(data, pm, mdesc) {
893
956
  return null;
894
957
  }
895
958
  const verb = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -1));
896
- const cmp = String(ment.cmp ?? '');
959
+ const cmp = String(ment.rescmp ?? ment.cmp ?? '');
897
960
  if ('' === verb || cmp === verb || cmp.endsWith('_' + verb)) {
898
961
  return null;
899
962
  }
@@ -938,7 +1001,7 @@ function entityOccursInPath(parts, entname) {
938
1001
  }
939
1002
  function entityPathMatch_tpe(data, pm, mdesc, why) {
940
1003
  const ment = mdesc.MethodEntity;
941
- const pathNameIndex = 0;
1004
+ const pathNameIndex = PATH_NAME_INDEX['t/p/'];
942
1005
  why.push('path=t/p/');
943
1006
  const origPathName = pm[pathNameIndex];
944
1007
  let entname = (0, utility_2.canonize)(origPathName);
@@ -953,7 +1016,7 @@ function entityPathMatch_tpe(data, pm, mdesc, why) {
953
1016
  }
954
1017
  function entityPathMatch_pte(data, pm, mdesc, why) {
955
1018
  const ment = mdesc.MethodEntity;
956
- const pathNameIndex = 1;
1019
+ const pathNameIndex = PATH_NAME_INDEX['p/t/'];
957
1020
  why.push('path=p/t/');
958
1021
  const origPathName = pm[pathNameIndex];
959
1022
  let entname = (0, utility_2.canonize)(origPathName);
@@ -968,7 +1031,7 @@ function entityPathMatch_pte(data, pm, mdesc, why) {
968
1031
  }
969
1032
  function entityPathMatch_te(data, pm, mdesc, why) {
970
1033
  const ment = mdesc.MethodEntity;
971
- const pathNameIndex = 0;
1034
+ const pathNameIndex = PATH_NAME_INDEX['t/'];
972
1035
  why.push('path=t/');
973
1036
  const origPathName = pm[pathNameIndex];
974
1037
  let entname = (0, utility_2.canonize)(origPathName);
@@ -983,7 +1046,7 @@ function entityPathMatch_te(data, pm, mdesc, why) {
983
1046
  }
984
1047
  function entityPathMatch_tpp(data, pm, mdesc, why) {
985
1048
  const ment = mdesc.MethodEntity;
986
- const pathNameIndex = 0;
1049
+ const pathNameIndex = PATH_NAME_INDEX['t/p/p'];
987
1050
  why.push('path=t/p/p');
988
1051
  const origPathName = pm[pathNameIndex];
989
1052
  let entname = (0, utility_2.canonize)(origPathName);
@@ -1000,6 +1063,17 @@ function getRequestBodySchema(requestBody) {
1000
1063
  return requestBody?.content?.['application/json']?.schema ??
1001
1064
  requestBody?.schema;
1002
1065
  }
1066
+ // The response an operation's result is read from.
1067
+ function successResponse(responses) {
1068
+ return responses?.[200] ?? responses?.[201];
1069
+ }
1070
+ // The response schemas an operation answers with when it succeeds, in the
1071
+ // order they are tried.
1072
+ function successSchemas(responses) {
1073
+ return ['200', '201']
1074
+ .map((rescode) => getResponseSchema(responses?.[rescode]))
1075
+ .filter((schema) => null != schema);
1076
+ }
1003
1077
  function getResponseSchema(response) {
1004
1078
  return response?.content?.['application/json']?.schema ??
1005
1079
  response?.schema;
@@ -1088,10 +1162,14 @@ function entityCmpMatch(data, entname, mdesc, why) {
1088
1162
  };
1089
1163
  const cmpInfrequent = (ment.method_rate < IS_ENTCMP_METHOD_RATE
1090
1164
  || ment.path_rate < IS_ENTCMP_PATH_RATE);
1165
+ const cmpShared = true === data.work.sharing.yields[ment.origcmpref + ' ' + mdesc.method + ' ' + mdesc.path];
1091
1166
  if (null != ment.cmp
1092
1167
  && entname != ment.cmp
1093
1168
  && !ment.cmp.startsWith(entname)) {
1094
- if (cmpInfrequent) {
1169
+ if (cmpInfrequent && cmpShared) {
1170
+ why.push('cmp-shared');
1171
+ }
1172
+ if (cmpInfrequent && !cmpShared) {
1095
1173
  why.push('cmp-primary');
1096
1174
  out.name = ment.cmp;
1097
1175
  out.orig = ment.origcmp;
@@ -1143,9 +1221,145 @@ function cmpOccursInPath(data, cmpname) {
1143
1221
  }
1144
1222
  return null != data.work.potentialCmpsFromPaths[cmpname];
1145
1223
  }
1146
- function isListResponse(mdesc, pathStr, why) {
1147
- const ment = mdesc.MethodEntity;
1148
- const pm = ment.pm;
1224
+ function matchEntityPath(parts) {
1225
+ for (const shape of ENTITY_PATH_SHAPES) {
1226
+ const pm = (0, utility_2.pathMatch)(parts, shape);
1227
+ if (null != pm) {
1228
+ return pm;
1229
+ }
1230
+ }
1231
+ return null;
1232
+ }
1233
+ // The resource a method's path names by itself, as its shape names it. A
1234
+ // write to a singular trailing literal is a verb on another resource, and
1235
+ // names none.
1236
+ function pathResource(parts, method) {
1237
+ const pm = matchEntityPath(parts);
1238
+ if (null == pm) {
1239
+ return null;
1240
+ }
1241
+ const last = parts[parts.length - 1];
1242
+ if (!READ_METHODS.includes(method) && !isParam(last)) {
1243
+ const lit = (0, jostraca_2.snakify)(last);
1244
+ if ('' !== lit && (0, utility_2.depluralize)(lit) === lit) {
1245
+ return null;
1246
+ }
1247
+ }
1248
+ return (0, utility_2.canonize)(pm[PATH_NAME_INDEX[pm.expr]]);
1249
+ }
1250
+ // The routes that keep their own path's name for the component they answer
1251
+ // with, each keyed `cmp METHOD path`: those of a component that several
1252
+ // resources share, where a record (one that declares an `id`) is never shared.
1253
+ function sharedRoutes(routes, records) {
1254
+ const bycmp = {};
1255
+ for (const route of routes) {
1256
+ const parts = route.path.split('/').filter((p) => '' !== p);
1257
+ const resource = pathResource(parts, route.method);
1258
+ if (null != resource && !records.includes(route.cmp)) {
1259
+ (bycmp[route.cmp] = bycmp[route.cmp] ?? []).push({ ...route, parts, resource });
1260
+ }
1261
+ }
1262
+ const taking = [];
1263
+ for (const cmp of Object.keys(bycmp).sort()) {
1264
+ const names = new Set(bycmp[cmp].map((entry) => entry.resource));
1265
+ const counted = bycmp[cmp].filter((entry) => !isSharingView(entry, names));
1266
+ const group = aliasGroups(counted);
1267
+ if (new Set(counted.map((entry) => group[entry.resource])).size < 2) {
1268
+ continue;
1269
+ }
1270
+ const members = {};
1271
+ for (const name of new Set(counted.map((entry) => entry.resource))) {
1272
+ members[group[name]] = (members[group[name]] ?? 0) + 1;
1273
+ }
1274
+ taking.push(...counted.filter((entry) => 1 === members[group[entry.resource]]));
1275
+ }
1276
+ const cmps = {};
1277
+ const routesOf = {};
1278
+ for (const entry of taking) {
1279
+ (cmps[entry.resource] = cmps[entry.resource] ?? new Set()).add(entry.cmp);
1280
+ const key = entry.resource + ' ' + entry.op + ' ' + paramNames(entry.parts);
1281
+ (routesOf[key] = routesOf[key] ?? new Set()).add(entry.path);
1282
+ }
1283
+ const blocked = new Set(Object.keys(cmps).filter((name) => 1 < cmps[name].size));
1284
+ for (const key of Object.keys(routesOf)) {
1285
+ if (1 < routesOf[key].size) {
1286
+ blocked.add(key.split(' ')[0]);
1287
+ }
1288
+ }
1289
+ return taking
1290
+ .filter((entry) => !blocked.has(entry.resource))
1291
+ .map((entry) => entry.cmp + ' ' + entry.method + ' ' + entry.path)
1292
+ .sort();
1293
+ }
1294
+ // A route beneath a segment that names another of the component's resources
1295
+ // is a view of that resource, such as `/builds/latest` of `/builds`.
1296
+ function isSharingView(entry, names) {
1297
+ for (let i = 1; i < entry.parts.length; i++) {
1298
+ const name = pathResource(entry.parts.slice(0, i), 'GET');
1299
+ if (null != name && name !== entry.resource && names.has(name)) {
1300
+ return true;
1301
+ }
1302
+ }
1303
+ return false;
1304
+ }
1305
+ // Item routes under the same parent keyed by the same parameters address the
1306
+ // same records, so the resources they name are one: each maps to its group.
1307
+ function aliasGroups(entries) {
1308
+ const group = {};
1309
+ const find = (name) => group[name] === name ? name : find(group[name]);
1310
+ for (const entry of entries) {
1311
+ group[entry.resource] = entry.resource;
1312
+ }
1313
+ const first = {};
1314
+ for (const entry of entries) {
1315
+ if (!isParam(entry.parts[entry.parts.length - 1])) {
1316
+ continue;
1317
+ }
1318
+ const pm = matchEntityPath(entry.parts);
1319
+ const parent = entry.parts.slice(0, pm.index + PATH_NAME_INDEX[pm.expr])
1320
+ .map((p) => isParam(p) ? '{}' : p).join('/');
1321
+ const key = parent + ' ' + paramNames(entry.parts);
1322
+ const other = first[key] = first[key] ?? entry.resource;
1323
+ const [a, b] = [find(other), find(entry.resource)].sort();
1324
+ group[b] = a;
1325
+ }
1326
+ for (const name of Object.keys(group)) {
1327
+ group[name] = find(name);
1328
+ }
1329
+ return group;
1330
+ }
1331
+ function paramNames(parts) {
1332
+ return parts.filter(isParam).sort().join(',');
1333
+ }
1334
+ // The schema a local `$ref` pointer names in the definition.
1335
+ function refSchema(def, xref) {
1336
+ if (!xref.startsWith('#/')) {
1337
+ return undefined;
1338
+ }
1339
+ let node = def;
1340
+ for (const seg of xref.slice(2).split('/')) {
1341
+ const key = seg.replace(/~1/g, '/').replace(/~0/g, '~');
1342
+ node = null != node && 'object' === typeof node && Object.prototype.hasOwnProperty.call(node, key)
1343
+ ? node[key] : undefined;
1344
+ }
1345
+ return node;
1346
+ }
1347
+ // Read in place: resolveSchemaProperties merges, and merging rewrites the
1348
+ // nodes the def's schemas share.
1349
+ function declaresId(schema) {
1350
+ if (null == schema || 'object' !== typeof schema) {
1351
+ return false;
1352
+ }
1353
+ const parts = [schema, ...(Array.isArray(schema.allOf) ? schema.allOf : [])];
1354
+ return parts.some((part) => null != part?.properties?.id);
1355
+ }
1356
+ // The operation ResolveOperation will assign, needed before the entity is
1357
+ // named: whether a response unwraps as an envelope depends on it.
1358
+ function methodOpname(mdesc, pm, why) {
1359
+ const opname = METHOD_IDOP[mdesc.method];
1360
+ return 'load' === opname && isListResponse(mdesc, pm, mdesc.path, why) ? 'list' : opname;
1361
+ }
1362
+ function isListResponse(mdesc, pm, pathStr, why) {
1149
1363
  let islist = false;
1150
1364
  let schema;
1151
1365
  const endParamAnchored = !!(pm && pm.expr.endsWith('p/'));
@@ -1154,8 +1368,7 @@ function isListResponse(mdesc, pathStr, why) {
1154
1368
  why.push('end-param');
1155
1369
  }
1156
1370
  else {
1157
- const response = mdesc.responses?.[200] ?? mdesc.responses?.[201];
1158
- schema = getResponseSchema(response);
1371
+ schema = getResponseSchema(successResponse(mdesc.responses));
1159
1372
  if (null == schema) {
1160
1373
  why.push('no-schema');
1161
1374
  }
@@ -1304,26 +1517,32 @@ function makeMethodEntityDesc(desc) {
1304
1517
  };
1305
1518
  return ment;
1306
1519
  }
1307
- function findPotentialSchemaRefs(pathStr, methodName, responses) {
1520
+ function findPotentialSchemaRefs(pathStr, methodName, responses, envelope, why) {
1308
1521
  const xrefs = [];
1309
- if (null == responses) {
1310
- return xrefs;
1311
- }
1312
- const rescodes = ['200', '201'];
1313
- for (let rescode of rescodes) {
1314
- const schema = getResponseSchema(responses[rescode]);
1315
- if (null != schema) {
1316
- if (null != schema['x-ref']) {
1317
- xrefs.push(schema['x-ref']);
1522
+ for (const schema of successSchemas(responses)) {
1523
+ if (null != schema['x-ref']) {
1524
+ // An envelope component names its wrapping, not the entity: the
1525
+ // component it carries takes its place.
1526
+ const itemref = envelope[schema['x-ref']];
1527
+ if ('' !== itemref) {
1528
+ why.push('envelope=' + cmpRefName(schema['x-ref']));
1529
+ xrefs.push(itemref);
1318
1530
  }
1319
- else if ('array' === schema.type && null != schema.items?.['x-ref']) {
1320
- xrefs.push(schema.items?.['x-ref']);
1531
+ else {
1532
+ xrefs.push(schema['x-ref']);
1321
1533
  }
1322
1534
  }
1535
+ else if ('array' === schema.type && null != schema.items?.['x-ref']) {
1536
+ xrefs.push(schema.items?.['x-ref']);
1537
+ }
1323
1538
  }
1324
1539
  (0, utility_2.debugpath)(pathStr, methodName, 'POTENTIAL-SCHEMA-REFS', xrefs);
1325
1540
  return xrefs;
1326
1541
  }
1542
+ function cmpRefName(xref) {
1543
+ const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/);
1544
+ return null == m ? xref : (0, utility_2.canonizeCmpName)(m[2]);
1545
+ }
1327
1546
  function hasMethod(def, pathStr, methodName) {
1328
1547
  const pathDef = def?.paths?.[pathStr];
1329
1548
  const found = (null != pathDef