@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
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.answeredRefs = answeredRefs;
|
|
4
|
+
exports.arrayBodyField = arrayBodyField;
|
|
5
|
+
exports.distinctRecord = distinctRecord;
|
|
6
|
+
exports.distinctShare = distinctShare;
|
|
3
7
|
exports.heuristic01 = heuristic01;
|
|
8
|
+
exports.namingRef = namingRef;
|
|
9
|
+
exports.namingSchemas = namingSchemas;
|
|
4
10
|
exports.pathResource = pathResource;
|
|
5
11
|
exports.sharedRoutes = sharedRoutes;
|
|
12
|
+
exports.entityParamNames = entityParamNames;
|
|
13
|
+
exports.isNameParam = isNameParam;
|
|
14
|
+
exports.isVerb = isVerb;
|
|
15
|
+
exports.verbParts = verbParts;
|
|
6
16
|
const ordu_1 = require("ordu");
|
|
7
17
|
const jostraca_1 = require("jostraca");
|
|
8
18
|
const struct_1 = require("@voxgig/struct");
|
|
9
19
|
const utility_1 = require("../utility");
|
|
20
|
+
const body_1 = require("../transform/body");
|
|
10
21
|
const utility_2 = require("../utility");
|
|
11
22
|
const jostraca_2 = require("jostraca");
|
|
12
23
|
const entity_1 = require("../transform/entity");
|
|
@@ -61,11 +72,13 @@ async function heuristic01(ctx) {
|
|
|
61
72
|
{ select: selectCmpXrefs, apply: MeasureRef },
|
|
62
73
|
{ select: selectAllMethods, apply: MeasureEnvelope },
|
|
63
74
|
MeasureEnvelopeItems,
|
|
75
|
+
MeasureAnswered,
|
|
64
76
|
{ select: selectAllMethods, apply: MeasureSharing },
|
|
65
77
|
MeasureShared,
|
|
78
|
+
{ select: selectAllMethods, apply: [ResolveEntityComponent, MeasureTagItems] },
|
|
79
|
+
{ select: selectAllMethods, apply: MeasureClaims },
|
|
66
80
|
{
|
|
67
81
|
select: selectAllMethods, apply: [
|
|
68
|
-
ResolveEntityComponent,
|
|
69
82
|
ResolveEntityName,
|
|
70
83
|
RenameParams,
|
|
71
84
|
FindActions,
|
|
@@ -81,7 +94,11 @@ async function heuristic01(ctx) {
|
|
|
81
94
|
throw result.err;
|
|
82
95
|
}
|
|
83
96
|
const guide = result.data.guide;
|
|
84
|
-
guide.metrics.count.entity -= (0, entity_1.mergeCollectionPaths)(guide, ctx.log, (pathStr, methods, collection) => pathRecordRef(ctx.def, pathStr, methods, collection)).length;
|
|
97
|
+
guide.metrics.count.entity -= (0, entity_1.mergeCollectionPaths)(guide, ctx.log, (pathStr, methods, collection) => pathRecordRef(ctx.def, pathStr, methods, collection), (sharePath, shareMethods, itemPath, itemMethods) => distinctShare(ctx.def, sharePath, shareMethods, itemPath, itemMethods)).length;
|
|
98
|
+
// After the merge, which can move a path onto another entity.
|
|
99
|
+
for (const entity of Object.values(guide.entity)) {
|
|
100
|
+
nameEntityParams(entity);
|
|
101
|
+
}
|
|
85
102
|
const metrics = guide.metrics;
|
|
86
103
|
const entities = Object.values(guide.entity);
|
|
87
104
|
const entityCount = entities.length;
|
|
@@ -136,6 +153,13 @@ function Prepare(spec) {
|
|
|
136
153
|
listEnvelope: {},
|
|
137
154
|
envelopePaths: {},
|
|
138
155
|
sharing: { routes: [], records: {}, yields: {} },
|
|
156
|
+
recordResources: {},
|
|
157
|
+
tagItems: {},
|
|
158
|
+
claims: {},
|
|
159
|
+
claimowner: {},
|
|
160
|
+
claimrecord: {},
|
|
161
|
+
pathowner: {},
|
|
162
|
+
recordowner: {},
|
|
139
163
|
entity: {
|
|
140
164
|
count: {
|
|
141
165
|
seen: 0,
|
|
@@ -258,6 +282,11 @@ function MeasureEnvelopeItems(spec) {
|
|
|
258
282
|
}
|
|
259
283
|
}
|
|
260
284
|
}
|
|
285
|
+
// The records some operation answers with in a 200 or 201, read through the
|
|
286
|
+
// envelopes MeasureEnvelopeItems settles.
|
|
287
|
+
function MeasureAnswered(spec) {
|
|
288
|
+
spec.data.work.answered = answeredRefs(spec.data.def, spec.data.work.envelope);
|
|
289
|
+
}
|
|
261
290
|
function isPageAndItsItem(work, xrefs) {
|
|
262
291
|
const pages = xrefs.filter((xref) => work.listEnvelope[xref]);
|
|
263
292
|
const items = xrefs.filter((xref) => !work.listEnvelope[xref]);
|
|
@@ -276,13 +305,18 @@ function MeasureSharing(spec) {
|
|
|
276
305
|
const parts = work.pathmap[mdesc.path].parts;
|
|
277
306
|
const op = methodOpname(mdesc, matchEntityPath(parts), []) ?? '';
|
|
278
307
|
const sharing = work.sharing;
|
|
279
|
-
const
|
|
308
|
+
const resource = pathResource(parts, mdesc.method);
|
|
309
|
+
const xrefs = findPotentialSchemaRefs(mdesc.path, mdesc.method, mdesc.responses, work.envelope, work.answered, []);
|
|
280
310
|
for (const xref of xrefs) {
|
|
281
311
|
const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/);
|
|
282
312
|
if (null != m) {
|
|
283
313
|
const cmp = (0, utility_2.canonizeCmpName)(m[2]);
|
|
314
|
+
const record = declaresId(refSchema(spec.data.def, xref));
|
|
284
315
|
sharing.routes.push({ cmp, method: mdesc.method, path: mdesc.path, op });
|
|
285
|
-
sharing.records[cmp] = true === sharing.records[cmp] ||
|
|
316
|
+
sharing.records[cmp] = true === sharing.records[cmp] || record;
|
|
317
|
+
if (record && null != resource) {
|
|
318
|
+
work.recordResources[resource] = true;
|
|
319
|
+
}
|
|
286
320
|
}
|
|
287
321
|
}
|
|
288
322
|
}
|
|
@@ -293,8 +327,13 @@ function MeasureShared(spec) {
|
|
|
293
327
|
sharing.yields[key] = true;
|
|
294
328
|
}
|
|
295
329
|
}
|
|
330
|
+
// One list for every pass, so a method keeps the component resolved for it.
|
|
296
331
|
function selectAllMethods(_source, spec) {
|
|
297
332
|
const ctx = spec.ctx;
|
|
333
|
+
const work = spec.data.work;
|
|
334
|
+
if (null != work.methods) {
|
|
335
|
+
return work.methods;
|
|
336
|
+
}
|
|
298
337
|
let caught = { methods: [] };
|
|
299
338
|
for (const [path, pdef] of (0, utility_2.sortedEntries)(ctx.def.paths)) {
|
|
300
339
|
for (const [m, mdef] of (0, utility_2.sortedEntries)(pdef)) {
|
|
@@ -336,7 +375,49 @@ function selectAllMethods(_source, spec) {
|
|
|
336
375
|
return 0;
|
|
337
376
|
}
|
|
338
377
|
});
|
|
339
|
-
|
|
378
|
+
work.methods = caught.methods;
|
|
379
|
+
return work.methods;
|
|
380
|
+
}
|
|
381
|
+
// The collections each tag gathers item routes from, before any is named, with
|
|
382
|
+
// the parameters of each collection's item routes.
|
|
383
|
+
function MeasureTagItems(spec) {
|
|
384
|
+
const work = spec.data.work;
|
|
385
|
+
const mdesc = spec.node.val;
|
|
386
|
+
const parts = work.pathmap[mdesc.path].parts;
|
|
387
|
+
const collection = tagItemCollection(spec.data, mdesc, parts);
|
|
388
|
+
if (null != collection) {
|
|
389
|
+
const cmp = mdesc.MethodEntity.cmp;
|
|
390
|
+
const items = work.tagItems[cmp] = work.tagItems[cmp] ?? {};
|
|
391
|
+
items[collection] = items[collection] ?? {};
|
|
392
|
+
items[collection][parts.filter(isParam)
|
|
393
|
+
.map((part) => (0, utility_2.canonizeParam)(part.slice(1, -1))).sort().join(',')] = true;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
// The name each route takes by its path and component, and its stored form,
|
|
397
|
+
// found before any route is named so an item route sees one sorting after it.
|
|
398
|
+
// The rules run on copies. A verb takes its parent's claim; an item route its
|
|
399
|
+
// collection names makes none, so a verb on one claims the route's segment.
|
|
400
|
+
function MeasureClaims(spec) {
|
|
401
|
+
const data = spec.data;
|
|
402
|
+
const work = data.work;
|
|
403
|
+
const mdesc = spec.node.val;
|
|
404
|
+
const parts = work.pathmap[mdesc.path].parts;
|
|
405
|
+
if (null != collectionNaming(data, mdesc, parts, work.claimrecord)) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const ment = { ...(mdesc.MethodEntity ?? makeMethodEntityDesc({})) };
|
|
409
|
+
const name = pathEntityName(data, { ...mdesc, MethodEntity: ment }, parts, matchEntityPath(parts), work.claimowner, []);
|
|
410
|
+
if (null != name) {
|
|
411
|
+
const claim = (0, utility_2.resplitFromCmp)(name, ment.cmp, []);
|
|
412
|
+
const stored = (0, utility_2.ensureMinEntityName)(claim, {});
|
|
413
|
+
for (const key of [claim, stored]) {
|
|
414
|
+
const claims = work.claims[key] = work.claims[key] ?? {};
|
|
415
|
+
claims[mdesc.path] = true;
|
|
416
|
+
}
|
|
417
|
+
const owners = work.claimowner[mdesc.path] = work.claimowner[mdesc.path] ?? {};
|
|
418
|
+
owners[mdesc.method] = claim;
|
|
419
|
+
markRecordOwner(work.claimrecord, mdesc.path, parts, ment, stored);
|
|
420
|
+
}
|
|
340
421
|
}
|
|
341
422
|
function ResolveEntityComponent(spec) {
|
|
342
423
|
const guide = spec.data.guide;
|
|
@@ -348,7 +429,7 @@ function ResolveEntityComponent(spec) {
|
|
|
348
429
|
const parts = work.pathmap[pathStr].parts;
|
|
349
430
|
let why_cmp = [];
|
|
350
431
|
let responses = methodDef.responses;
|
|
351
|
-
let origxrefs = findPotentialSchemaRefs(pathStr, methodName, responses, work.envelope, why_cmp).map(val => ({
|
|
432
|
+
let origxrefs = findPotentialSchemaRefs(pathStr, methodName, responses, work.envelope, work.answered, why_cmp).map(val => ({
|
|
352
433
|
val
|
|
353
434
|
}));
|
|
354
435
|
let cmpxrefs = origxrefs
|
|
@@ -479,31 +560,18 @@ function ResolveEntityName(spec) {
|
|
|
479
560
|
ment = mdesc.MethodEntity;
|
|
480
561
|
}
|
|
481
562
|
why_path.push(...(ment.why_cmp ?? []));
|
|
482
|
-
let entname;
|
|
483
563
|
const pm = matchEntityPath(parts);
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
entname = entityPathMatch_tpe(data, pm, mdesc, why_path);
|
|
489
|
-
}
|
|
490
|
-
else if ('p/t/' === pm?.expr) {
|
|
491
|
-
entname = entityPathMatch_pte(data, pm, mdesc, why_path);
|
|
492
|
-
}
|
|
493
|
-
else if ('t/' === pm?.expr) {
|
|
494
|
-
entname = entityPathMatch_te(data, pm, mdesc, why_path);
|
|
495
|
-
}
|
|
496
|
-
else if ('t/p/p' === pm?.expr) {
|
|
497
|
-
entname = entityPathMatch_tpp(data, pm, mdesc, why_path);
|
|
498
|
-
}
|
|
499
|
-
else {
|
|
500
|
-
entname = inferEntityName(mdesc, parts, why_path);
|
|
501
|
-
if (null == entname) {
|
|
502
|
-
work.entity.count.unresolved++;
|
|
503
|
-
entname = 'entity' + work.entity.count.unresolved;
|
|
504
|
-
}
|
|
564
|
+
let entname = pathEntityName(data, mdesc, parts, pm, work.pathowner, why_path);
|
|
565
|
+
if (null == entname) {
|
|
566
|
+
work.entity.count.unresolved++;
|
|
567
|
+
entname = 'entity' + work.entity.count.unresolved;
|
|
505
568
|
}
|
|
506
569
|
entname = (0, utility_2.resplitFromCmp)(entname, ment.cmp, why_path);
|
|
570
|
+
const item = itemOfCollection(data, mdesc, parts);
|
|
571
|
+
if (null != item) {
|
|
572
|
+
why_path.push(item.why + '=' + item.name);
|
|
573
|
+
entname = item.name;
|
|
574
|
+
}
|
|
507
575
|
// Keep the pre-truncation name so a truncated-name collision can tell a
|
|
508
576
|
// re-encounter of the SAME origin (merge) from a genuinely different one
|
|
509
577
|
// (numeric suffix) — see ensureMinEntityName.
|
|
@@ -532,14 +600,43 @@ function ResolveEntityName(spec) {
|
|
|
532
600
|
// split across entities by method (a PUT answering with a one-off
|
|
533
601
|
// acknowledgement is named after it), and the parent of a verb is the
|
|
534
602
|
// entity a read of the item returns.
|
|
535
|
-
work.pathowner = work.pathowner ?? {};
|
|
536
603
|
work.pathowner[pathStr] = work.pathowner[pathStr] ?? {};
|
|
537
604
|
work.pathowner[pathStr][methodName] = entname;
|
|
605
|
+
markRecordOwner(work.recordowner, pathStr, parts, ment, entname);
|
|
538
606
|
// Same guard, same reason: the formatting is the cost, not the call.
|
|
539
607
|
if ((0, utility_2.debugpathOn)()) {
|
|
540
608
|
(0, utility_2.debugpath)(pathStr, methodName, 'RESOLVE-ENTITY-NAME', (0, utility_2.formatJSONIC)({ entdesc, ment }, { hsepd: 0, $: true, color: true }));
|
|
541
609
|
}
|
|
542
610
|
}
|
|
611
|
+
// The name a method's path shape and component give it, or null where neither
|
|
612
|
+
// does. A verb on a parent takes the name pathowner holds for the parent.
|
|
613
|
+
function pathEntityName(data, mdesc, parts, pm, pathowner, why) {
|
|
614
|
+
if ('t/p/t/' === pm?.expr) {
|
|
615
|
+
return entityPathMatch_tpte(data, pm, mdesc, pathowner, why);
|
|
616
|
+
}
|
|
617
|
+
else if ('t/p/' === pm?.expr) {
|
|
618
|
+
return entityPathMatch_tpe(data, pm, mdesc, why);
|
|
619
|
+
}
|
|
620
|
+
else if ('p/t/' === pm?.expr) {
|
|
621
|
+
return entityPathMatch_pte(data, pm, mdesc, why);
|
|
622
|
+
}
|
|
623
|
+
else if ('t/' === pm?.expr) {
|
|
624
|
+
return entityPathMatch_te(data, pm, mdesc, why);
|
|
625
|
+
}
|
|
626
|
+
else if ('t/p/p' === pm?.expr) {
|
|
627
|
+
return entityPathMatch_tpp(data, pm, mdesc, why);
|
|
628
|
+
}
|
|
629
|
+
return inferEntityName(mdesc, parts, why);
|
|
630
|
+
}
|
|
631
|
+
// The entity a path's own record names, where the record carries the name the
|
|
632
|
+
// path's last segment gives.
|
|
633
|
+
function markRecordOwner(recordowner, pathStr, parts, ment, entname) {
|
|
634
|
+
const last = parts.filter((part) => !isParam(part)).pop();
|
|
635
|
+
if (null == recordowner[pathStr] && isSchemaRef(ment.ref) &&
|
|
636
|
+
entname === ment.cmp && null != last && entname === (0, utility_2.canonize)(last)) {
|
|
637
|
+
recordowner[pathStr] = entname;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
543
640
|
function RenameParams(spec) {
|
|
544
641
|
const ctx = spec.ctx;
|
|
545
642
|
const data = spec.data;
|
|
@@ -560,7 +657,7 @@ function RenameParams(spec) {
|
|
|
560
657
|
rename: pathDesc.rename.param = (pathDesc.rename.param ?? {}),
|
|
561
658
|
why: pathDesc.why_rename.why_param = (pathDesc.why_rename.why_param ?? {}),
|
|
562
659
|
};
|
|
563
|
-
const parts = pathdesc.parts;
|
|
660
|
+
const parts = verbParts(pathdesc.parts);
|
|
564
661
|
const applySnakeCaseRename = () => {
|
|
565
662
|
for (const part of parts) {
|
|
566
663
|
const m = part.match(/^\{(.+)\}$/);
|
|
@@ -630,7 +727,7 @@ function RenameParams(spec) {
|
|
|
630
727
|
updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'id-parent-ent');
|
|
631
728
|
why.push('id-parent-ent');
|
|
632
729
|
}
|
|
633
|
-
else {
|
|
730
|
+
else if (!isNameParam(oldParam)) {
|
|
634
731
|
updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, parentName + '_id', 'parent:' + parentName);
|
|
635
732
|
why.push('parent');
|
|
636
733
|
}
|
|
@@ -671,7 +768,7 @@ function RenameParams(spec) {
|
|
|
671
768
|
else {
|
|
672
769
|
why.push('default');
|
|
673
770
|
let newParamName = parentName + '_id';
|
|
674
|
-
if (newParamName != oldParam) {
|
|
771
|
+
if (newParamName != oldParam && !isNameParam(oldParam)) {
|
|
675
772
|
updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, newParamName, 'not-primary');
|
|
676
773
|
why.push('not-primary');
|
|
677
774
|
}
|
|
@@ -717,7 +814,7 @@ function FindActions(spec) {
|
|
|
717
814
|
const methodName = mdesc.method;
|
|
718
815
|
pathdesc.action = (pathdesc.action ?? {});
|
|
719
816
|
pathdesc.why_action = (pathdesc.why_action ?? {});
|
|
720
|
-
const parts = spec.data.work.pathmap[pathStr].parts;
|
|
817
|
+
const parts = verbParts(spec.data.work.pathmap[pathStr].parts);
|
|
721
818
|
const fourthLastPart = parts[parts.length - 4];
|
|
722
819
|
const fourthLastPartCanon = (0, utility_2.canonize)(fourthLastPart);
|
|
723
820
|
const thirdLastPart = parts[parts.length - 3];
|
|
@@ -728,15 +825,17 @@ function FindActions(spec) {
|
|
|
728
825
|
const lastPartCanon = (0, utility_2.canonize)(lastPart);
|
|
729
826
|
const cmp = ment.cmp;
|
|
730
827
|
if (null != ment.verb_on_parent) {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
828
|
+
if (isVerb(lastPart)) {
|
|
829
|
+
pathdesc.action[lastPartCanon] = pathdesc.action[lastPartCanon] ?? {
|
|
830
|
+
why_action: ['ent', entdesc.name, 'verb-on-parent', lastPart, methodName],
|
|
831
|
+
};
|
|
832
|
+
}
|
|
734
833
|
}
|
|
735
834
|
// /api/foo/bar where foo is the entity and bar is the action, no id param
|
|
736
835
|
else if (secondLastPartCanon === cmp
|
|
737
836
|
|| secondLastPartCanon === ment.origcmp
|
|
738
837
|
|| secondLastPartCanon === entname) {
|
|
739
|
-
if (
|
|
838
|
+
if (isVerb(lastPart)) {
|
|
740
839
|
updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc, 'no-param');
|
|
741
840
|
}
|
|
742
841
|
}
|
|
@@ -744,7 +843,7 @@ function FindActions(spec) {
|
|
|
744
843
|
else if (thirdLastPartCanon === cmp
|
|
745
844
|
|| thirdLastPartCanon === ment.origcmp
|
|
746
845
|
|| thirdLastPartCanon === entname) {
|
|
747
|
-
if (isParam(secondLastPart) &&
|
|
846
|
+
if (isParam(secondLastPart) && isVerb(lastPart)) {
|
|
748
847
|
updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc, 'ent-param-2nd-last');
|
|
749
848
|
}
|
|
750
849
|
}
|
|
@@ -752,7 +851,7 @@ function FindActions(spec) {
|
|
|
752
851
|
else if (fourthLastPartCanon === cmp
|
|
753
852
|
|| fourthLastPartCanon === ment.origcmp
|
|
754
853
|
|| fourthLastPartCanon === entname) {
|
|
755
|
-
if (isParam(thirdLastPart) &&
|
|
854
|
+
if (isParam(thirdLastPart) && isVerb(secondLastPart) && isVerb(lastPart)) {
|
|
756
855
|
const oldActionName = secondLastPart + '/' + lastPart;
|
|
757
856
|
const actionName = secondLastPartCanon + '_' + lastPartCanon;
|
|
758
857
|
updateAction(methodName, oldActionName, actionName, entdesc, pathdesc, 'ent-param-3rd-last');
|
|
@@ -841,10 +940,16 @@ function ResolveTransform(spec) {
|
|
|
841
940
|
// project in Neon's project create beside its branch and roles, and may
|
|
842
941
|
// declare properties of its own beside them.
|
|
843
942
|
const partprops = (0, utility_2.mergedProperties)(resschema);
|
|
844
|
-
|
|
943
|
+
// A list reads records, so an object named for the entity does not win over
|
|
944
|
+
// a list of them beside it, such as the matches beside the competition a
|
|
945
|
+
// page of them repeats. The list is found in the same parts the name is.
|
|
946
|
+
const records = 'list' === opname && null != partprops ? (0, utility_1.envelopeProp)(partprops, opname) : null;
|
|
947
|
+
const holdsRecord = (prop) => (0, utility_1.isEntityWrapperProp)(prop) &&
|
|
948
|
+
(null == records || true === (0, utility_1.propIsList)(prop));
|
|
949
|
+
if (named && holdsRecord(partprops?.[entdesc.origname])) {
|
|
845
950
|
transform.res = '`body.' + entdesc.origname + '`';
|
|
846
951
|
}
|
|
847
|
-
else if (named && (
|
|
952
|
+
else if (named && holdsRecord(partprops?.[entdesc.name])) {
|
|
848
953
|
transform.res = '`body.' + entdesc.name + '`';
|
|
849
954
|
}
|
|
850
955
|
else if (resprops) {
|
|
@@ -864,33 +969,18 @@ function ResolveTransform(spec) {
|
|
|
864
969
|
transform.res = '`body.' + envelope + '`';
|
|
865
970
|
}
|
|
866
971
|
}
|
|
867
|
-
|
|
868
|
-
const
|
|
869
|
-
(
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
(0, struct_1.keysof)(reqprops).every((k) => k === name);
|
|
875
|
-
if (reqschema) {
|
|
876
|
-
if (wraps(entdesc.origname)) {
|
|
877
|
-
transform.req = { [entdesc.origname]: '`reqdata`' };
|
|
878
|
-
}
|
|
879
|
-
else if (wraps(entdesc.name)) {
|
|
880
|
-
transform.req = { [entdesc.name]: '`reqdata`' };
|
|
881
|
-
}
|
|
882
|
-
else {
|
|
883
|
-
// A CLOSED body schema names every property the server will accept, so
|
|
884
|
-
// the body is those properties — not the whole request payload. The
|
|
885
|
-
// payload also carries the op's PATH params (`id` for
|
|
886
|
-
// `PUT /item/{id}`), and a closed shape rejects the entire request over
|
|
887
|
-
// that one extra key: every update came back 400 with `invalid-data`.
|
|
888
|
-
const body = (0, utility_1.closedBodyTransform)(reqschema);
|
|
889
|
-
if (null != body) {
|
|
890
|
-
transform.req = body;
|
|
891
|
-
}
|
|
972
|
+
// An item that is the entity's own component is the record, as above.
|
|
973
|
+
const itemref = resschema?.items?.['x-ref'];
|
|
974
|
+
if (null == transform.res && 'list' === opname &&
|
|
975
|
+
(null == itemref || null == entdesc.cmp || cmpRefName(itemref) !== entdesc.cmp)) {
|
|
976
|
+
const key = (0, utility_1.itemEnvelopeKey)(resschema, [entdesc.origname, entdesc.name]);
|
|
977
|
+
if (null != key) {
|
|
978
|
+
transform.res = (0, utility_1.itemEnvelopeTransform)(key);
|
|
892
979
|
}
|
|
893
980
|
}
|
|
981
|
+
const reqschema = getRequestBodySchema(mdesc.requestBody);
|
|
982
|
+
(0, utility_2.debugpath)(pathStr, methodName, 'TRANSFORM-REQ', (0, struct_1.keysof)(reqschema?.properties));
|
|
983
|
+
transform.req = (0, utility_1.bodyRequestTransform)(reqschema, [entdesc.origname, entdesc.name]);
|
|
894
984
|
if (!(0, struct_1.isempty)(transform) && null != op[opname]) {
|
|
895
985
|
op[opname].transform = transform;
|
|
896
986
|
}
|
|
@@ -933,7 +1023,7 @@ function BuildEntity(spec) {
|
|
|
933
1023
|
}
|
|
934
1024
|
entityMap[entdesc.name] = guideEntity;
|
|
935
1025
|
}
|
|
936
|
-
function entityPathMatch_tpte(data, pm, mdesc, why) {
|
|
1026
|
+
function entityPathMatch_tpte(data, pm, mdesc, pathowner, why) {
|
|
937
1027
|
const ment = mdesc.MethodEntity;
|
|
938
1028
|
const pathNameIndex = PATH_NAME_INDEX['t/p/t/'];
|
|
939
1029
|
why.push('path=t/p/t/');
|
|
@@ -941,7 +1031,7 @@ function entityPathMatch_tpte(data, pm, mdesc, why) {
|
|
|
941
1031
|
let entname = (0, utility_2.canonize)(origPathName);
|
|
942
1032
|
let ecm = undefined;
|
|
943
1033
|
if (null != ment.cmp) {
|
|
944
|
-
const parent = verbOnParent(data, pm, mdesc);
|
|
1034
|
+
const parent = verbOnParent(data, pm, mdesc, pathowner);
|
|
945
1035
|
if (null != parent) {
|
|
946
1036
|
entname = parent;
|
|
947
1037
|
ment.verb_on_parent = (0, struct_1.getelem)(pm, -1);
|
|
@@ -983,7 +1073,7 @@ function endsWithCmp(data, pm) {
|
|
|
983
1073
|
const last = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -1));
|
|
984
1074
|
return isOrigCmp(data, last);
|
|
985
1075
|
}
|
|
986
|
-
function verbOnParent(data, pm, mdesc) {
|
|
1076
|
+
function verbOnParent(data, pm, mdesc, pathowner) {
|
|
987
1077
|
const method = mdesc.method;
|
|
988
1078
|
if (READ_METHODS.includes(method)) {
|
|
989
1079
|
return null;
|
|
@@ -1026,7 +1116,7 @@ function verbOnParent(data, pm, mdesc) {
|
|
|
1026
1116
|
// known. The parent is the entity a READ of the item returns: a PUT on
|
|
1027
1117
|
// the item answering with a one-off acknowledgement is named after that
|
|
1028
1118
|
// and must not claim the verb. Fall back to any owner, then the literal.
|
|
1029
|
-
const owners =
|
|
1119
|
+
const owners = pathowner[itemPath] ?? {};
|
|
1030
1120
|
const parent = owners.GET ?? owners.QUERY ??
|
|
1031
1121
|
Object.values(owners).sort()[0];
|
|
1032
1122
|
if (null != parent) {
|
|
@@ -1034,6 +1124,103 @@ function verbOnParent(data, pm, mdesc) {
|
|
|
1034
1124
|
}
|
|
1035
1125
|
return (0, utility_2.canonize)((0, struct_1.getelem)(pm, -3));
|
|
1036
1126
|
}
|
|
1127
|
+
// The collection of an item route whose method its tag alone names, when the
|
|
1128
|
+
// route answers nothing.
|
|
1129
|
+
function tagItemCollection(data, mdesc, parts) {
|
|
1130
|
+
const ment = mdesc.MethodEntity;
|
|
1131
|
+
if (null == ment || 'tag' !== ment.ref || null != ment.rescmp) {
|
|
1132
|
+
return null;
|
|
1133
|
+
}
|
|
1134
|
+
const lits = parts.slice(0, parts.findIndex(isParam));
|
|
1135
|
+
if (0 === lits.length || !parts.slice(lits.length).every(isParam)) {
|
|
1136
|
+
return null;
|
|
1137
|
+
}
|
|
1138
|
+
const answers = Object.entries(data.def?.paths?.[mdesc.path] ?? {})
|
|
1139
|
+
.some(([method, mdef]) => null != METHOD_CONSIDER_ORDER[method.toUpperCase()] &&
|
|
1140
|
+
null != getResponseSchema(successResponse(mdef?.responses)));
|
|
1141
|
+
return answers ? null : '/' + lits.join('/');
|
|
1142
|
+
}
|
|
1143
|
+
// The collection that names a tag-named item route, given the record owners
|
|
1144
|
+
// known so far: by its record, where the tag names another resource with one
|
|
1145
|
+
// (GitHub's invitations), or by its segment, where the routes the tag gathers
|
|
1146
|
+
// from several collections would share a selector (Apicurio's well-known
|
|
1147
|
+
// routes). Null where the route keeps the name its own path gives.
|
|
1148
|
+
function collectionNaming(data, mdesc, parts, recordowner) {
|
|
1149
|
+
const collection = tagItemCollection(data, mdesc, parts);
|
|
1150
|
+
if (null == collection) {
|
|
1151
|
+
return null;
|
|
1152
|
+
}
|
|
1153
|
+
const work = data.work;
|
|
1154
|
+
const cmp = mdesc.MethodEntity.cmp;
|
|
1155
|
+
if (true === work.recordResources[cmp]) {
|
|
1156
|
+
const record = recordowner[collection] ?? recordowner[collection + '/'];
|
|
1157
|
+
return null == record ? null : { collection, record };
|
|
1158
|
+
}
|
|
1159
|
+
return tagCollides(data.def, work.tagItems[cmp] ?? {}) ? { collection } : null;
|
|
1160
|
+
}
|
|
1161
|
+
// The entity of an item route its collection names: the collection's record,
|
|
1162
|
+
// or its segment. A route outside the collection that has the segment puts the
|
|
1163
|
+
// tag before it, and one that has that name too a number from 2 after it, up
|
|
1164
|
+
// to the first number that leaves the stored form as it was.
|
|
1165
|
+
function itemOfCollection(data, mdesc, parts) {
|
|
1166
|
+
const naming = collectionNaming(data, mdesc, parts, data.work.recordowner);
|
|
1167
|
+
if (null == naming) {
|
|
1168
|
+
return null;
|
|
1169
|
+
}
|
|
1170
|
+
if (null != naming.record) {
|
|
1171
|
+
return { name: naming.record, why: 'collection-record' };
|
|
1172
|
+
}
|
|
1173
|
+
const collection = naming.collection;
|
|
1174
|
+
const segment = (0, utility_2.canonize)(collection.substring(collection.lastIndexOf('/') + 1));
|
|
1175
|
+
if (!segmentHeld(data.work, segment, collection)) {
|
|
1176
|
+
return { name: segment, why: 'collection-segment' };
|
|
1177
|
+
}
|
|
1178
|
+
const tagged = mdesc.MethodEntity.cmp + '_' + segment;
|
|
1179
|
+
let name = tagged;
|
|
1180
|
+
for (let i = 2; segmentHeld(data.work, name, collection); i++) {
|
|
1181
|
+
const stored = (0, utility_2.ensureMinEntityName)(name, {});
|
|
1182
|
+
name = tagged + i;
|
|
1183
|
+
if ((0, utility_2.ensureMinEntityName)(name, {}) === stored) {
|
|
1184
|
+
break;
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
return { name, why: 'collection-segment' };
|
|
1188
|
+
}
|
|
1189
|
+
// A route outside the collection has the name: one named already under the
|
|
1190
|
+
// key the name would take now, or one that claims the name, its stored form
|
|
1191
|
+
// or that key. An item route its collection names makes no claim, so it
|
|
1192
|
+
// counts only once named.
|
|
1193
|
+
function segmentHeld(work, name, collection) {
|
|
1194
|
+
const outside = (path) => !(path + '/').startsWith(collection + '/');
|
|
1195
|
+
const key = (0, utility_2.ensureMinEntityName)(name, work.entmap);
|
|
1196
|
+
const stored = (0, utility_2.ensureMinEntityName)(name, {});
|
|
1197
|
+
return [work.entmap[key]?.path, work.claims[name], work.claims[stored], work.claims[key]]
|
|
1198
|
+
.some((paths) => Object.keys(paths ?? {}).some(outside));
|
|
1199
|
+
}
|
|
1200
|
+
// Two of a tag's collections whose item routes take the same parameters, or
|
|
1201
|
+
// that are both read, would share a selector on the tag's entity.
|
|
1202
|
+
function tagCollides(def, items) {
|
|
1203
|
+
const seen = {};
|
|
1204
|
+
let reads = 0;
|
|
1205
|
+
for (const [collection, params] of Object.entries(items)) {
|
|
1206
|
+
for (const key of Object.keys(params)) {
|
|
1207
|
+
if (seen[key]) {
|
|
1208
|
+
return true;
|
|
1209
|
+
}
|
|
1210
|
+
seen[key] = true;
|
|
1211
|
+
}
|
|
1212
|
+
if (pathReads(def.paths?.[collection]) || pathReads(def.paths?.[collection + '/'])) {
|
|
1213
|
+
reads++;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
return 1 < reads;
|
|
1217
|
+
}
|
|
1218
|
+
function pathReads(pathdef) {
|
|
1219
|
+
return Object.entries(pathdef ?? {}).some(([method, mdef]) => 'GET' === method.toUpperCase() && null != mdef);
|
|
1220
|
+
}
|
|
1221
|
+
function isSchemaRef(ref) {
|
|
1222
|
+
return null != ref && '' !== ref && 'tag' !== ref;
|
|
1223
|
+
}
|
|
1037
1224
|
function isOrigCmp(data, name) {
|
|
1038
1225
|
return null != data.guide.metrics.count.origcmprefs[name];
|
|
1039
1226
|
}
|
|
@@ -1101,8 +1288,7 @@ function entityPathMatch_tpp(data, pm, mdesc, why) {
|
|
|
1101
1288
|
return entname;
|
|
1102
1289
|
}
|
|
1103
1290
|
function getRequestBodySchema(requestBody) {
|
|
1104
|
-
return requestBody?.
|
|
1105
|
-
requestBody?.schema;
|
|
1291
|
+
return (0, body_1.jsonRequestSchema)({ requestBody }) ?? requestBody?.schema;
|
|
1106
1292
|
}
|
|
1107
1293
|
// The response an operation's result is read from, down to an Accepted
|
|
1108
1294
|
// response when nothing else answers, whose body may be the job it queued.
|
|
@@ -1117,17 +1303,58 @@ function successSchemas(responses) {
|
|
|
1117
1303
|
.map((rescode) => getResponseSchema(responses?.[rescode]))
|
|
1118
1304
|
.filter((schema) => null != schema);
|
|
1119
1305
|
}
|
|
1306
|
+
// A read answered only by an Accepted response is named from it when another
|
|
1307
|
+
// operation answers with the same record in a 200 or 201, which shows the
|
|
1308
|
+
// record is a resource rather than the work queued.
|
|
1309
|
+
function namingSchemas(method, responses, answered, envelope) {
|
|
1310
|
+
const schemas = successSchemas(responses);
|
|
1311
|
+
if (0 === schemas.length && READ_METHODS.includes(method)) {
|
|
1312
|
+
const accepted = getResponseSchema(responses?.[202]);
|
|
1313
|
+
if (true === answered[namingRef(accepted, envelope) ?? '']) {
|
|
1314
|
+
schemas.push(accepted);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
return schemas;
|
|
1318
|
+
}
|
|
1319
|
+
function answeredRefs(def, envelope) {
|
|
1320
|
+
const answered = {};
|
|
1321
|
+
for (const pathdef of Object.values(def?.paths ?? {})) {
|
|
1322
|
+
for (const [method, mdef] of Object.entries(pathdef ?? {})) {
|
|
1323
|
+
if (null == METHOD_CONSIDER_ORDER[method.toUpperCase()] ||
|
|
1324
|
+
null == mdef || 'object' !== typeof mdef) {
|
|
1325
|
+
continue;
|
|
1326
|
+
}
|
|
1327
|
+
for (const schema of successSchemas(mdef.responses)) {
|
|
1328
|
+
const ref = namingRef(schema, envelope);
|
|
1329
|
+
if (null != ref) {
|
|
1330
|
+
answered[ref] = true;
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
return answered;
|
|
1336
|
+
}
|
|
1337
|
+
// The component a response schema names through: the record an envelope
|
|
1338
|
+
// carries, else the schema's own component, else its array items'.
|
|
1339
|
+
function namingRef(schema, envelope) {
|
|
1340
|
+
const xref = schema?.['x-ref'];
|
|
1341
|
+
if (null != xref) {
|
|
1342
|
+
const itemref = envelope[xref];
|
|
1343
|
+
return null == itemref || '' === itemref ? xref : itemref;
|
|
1344
|
+
}
|
|
1345
|
+
return 'array' === schema?.type ? schema.items?.['x-ref'] : undefined;
|
|
1346
|
+
}
|
|
1120
1347
|
function getResponseSchema(response) {
|
|
1121
1348
|
return response?.content?.['application/json']?.schema ??
|
|
1122
1349
|
response?.schema;
|
|
1123
1350
|
}
|
|
1124
1351
|
// The record a path answers with, read from its methods in the order they are
|
|
1125
1352
|
// considered, so a read decides before a write.
|
|
1126
|
-
function pathRecordRef(def, pathStr, methods, collection) {
|
|
1353
|
+
function pathRecordRef(def, pathStr, methods, collection, accepted = true) {
|
|
1127
1354
|
const rank = (method) => METHOD_CONSIDER_ORDER[method] ?? Number.MAX_SAFE_INTEGER;
|
|
1128
1355
|
const ordered = [...methods].sort((a, b) => rank(a) - rank(b) || (0, refcount_1.byCodePoint)(a, b));
|
|
1129
1356
|
for (const method of ordered) {
|
|
1130
|
-
const ref = routeRecordRef(def, pathStr, method, collection);
|
|
1357
|
+
const ref = routeRecordRef(def, pathStr, method, collection, accepted);
|
|
1131
1358
|
if (null != ref) {
|
|
1132
1359
|
return ref;
|
|
1133
1360
|
}
|
|
@@ -1138,9 +1365,11 @@ function pathRecordRef(def, pathStr, methods, collection) {
|
|
|
1138
1365
|
// items', or the one its envelope carries, such as the job summary in Mux's
|
|
1139
1366
|
// `{ data }`. Only a collection reads a page, so beneath the collection a
|
|
1140
1367
|
// team that holds nothing but its members is a team. Null when the answer
|
|
1141
|
-
// names no component.
|
|
1142
|
-
function routeRecordRef(def, pathStr, method, collection) {
|
|
1143
|
-
const
|
|
1368
|
+
// names no component, or is Accepted and `accepted` excludes it.
|
|
1369
|
+
function routeRecordRef(def, pathStr, method, collection, accepted = true) {
|
|
1370
|
+
const responses = def?.paths?.[pathStr]?.[method.toLowerCase()]?.responses;
|
|
1371
|
+
const schema = getResponseSchema(accepted ? successResponse(responses) :
|
|
1372
|
+
responses?.[200] ?? responses?.[201]);
|
|
1144
1373
|
if (null == schema || 'object' !== typeof schema) {
|
|
1145
1374
|
return null;
|
|
1146
1375
|
}
|
|
@@ -1425,14 +1654,29 @@ function refSchema(def, xref) {
|
|
|
1425
1654
|
}
|
|
1426
1655
|
return node;
|
|
1427
1656
|
}
|
|
1428
|
-
//
|
|
1429
|
-
//
|
|
1430
|
-
|
|
1431
|
-
|
|
1657
|
+
// Whether a share's record is apart from an item's: both declare an `id`, and
|
|
1658
|
+
// the share has no more than half of its properties in common with the item,
|
|
1659
|
+
// since a collection's list is routinely a summary of its item's record.
|
|
1660
|
+
function distinctRecord(share, item) {
|
|
1661
|
+
if (!declaresId(share) || !declaresId(item)) {
|
|
1432
1662
|
return false;
|
|
1433
1663
|
}
|
|
1434
|
-
const
|
|
1435
|
-
|
|
1664
|
+
const own = Object.keys((0, utility_2.mergedProperties)(share) ?? {});
|
|
1665
|
+
const theirs = (0, utility_2.mergedProperties)(item) ?? {};
|
|
1666
|
+
const common = own.filter((name) => Object.prototype.hasOwnProperty.call(theirs, name));
|
|
1667
|
+
return common.length * 2 <= own.length;
|
|
1668
|
+
}
|
|
1669
|
+
// Whether a collection share answers with a record apart from the one its item
|
|
1670
|
+
// route answers with, both read from a 200 or 201: an Accepted body may
|
|
1671
|
+
// describe the queued work rather than a resource.
|
|
1672
|
+
function distinctShare(def, sharePath, shareMethods, itemPath, itemMethods) {
|
|
1673
|
+
const share = pathRecordRef(def, sharePath, shareMethods, true, false);
|
|
1674
|
+
const item = pathRecordRef(def, itemPath, itemMethods, false, false);
|
|
1675
|
+
return null != share && null != item && share !== item &&
|
|
1676
|
+
distinctRecord(refSchema(def, share), refSchema(def, item));
|
|
1677
|
+
}
|
|
1678
|
+
function declaresId(schema) {
|
|
1679
|
+
return null != (0, utility_2.mergedProperties)(schema)?.id;
|
|
1436
1680
|
}
|
|
1437
1681
|
// The operation ResolveOperation will assign, needed before the entity is
|
|
1438
1682
|
// named: whether a response unwraps as an envelope depends on it.
|
|
@@ -1475,16 +1719,18 @@ function isListResponse(mdesc, pm, pathStr, why) {
|
|
|
1475
1719
|
(0, utility_2.debugpath)(pathStr, mdesc.method, 'IS-LIST', islist, why, schema);
|
|
1476
1720
|
return islist;
|
|
1477
1721
|
}
|
|
1722
|
+
// struct's merge rewrites every list element it walks, not just the first, so
|
|
1723
|
+
// each schema is cloned: the definition shares one object per $ref target.
|
|
1478
1724
|
function resolveSchemaProperties(schema) {
|
|
1479
1725
|
let properties = {};
|
|
1480
1726
|
// This is definitely heuristic!
|
|
1481
1727
|
if (schema.allOf) {
|
|
1482
1728
|
for (let i = schema.allOf.length - 1; -1 < i; --i) {
|
|
1483
|
-
properties = (0, struct_1.merge)([properties, schema.allOf[i].properties || {}]);
|
|
1729
|
+
properties = (0, struct_1.merge)([properties, (0, struct_1.clone)(schema.allOf[i].properties || {})]);
|
|
1484
1730
|
}
|
|
1485
1731
|
}
|
|
1486
1732
|
if (schema.properties) {
|
|
1487
|
-
properties = (0, struct_1.merge)([properties, schema.properties]);
|
|
1733
|
+
properties = (0, struct_1.merge)([properties, (0, struct_1.clone)(schema.properties)]);
|
|
1488
1734
|
}
|
|
1489
1735
|
return properties;
|
|
1490
1736
|
}
|
|
@@ -1560,6 +1806,96 @@ function updateParamRename(ctx, data, path, method, paramRenameCapture, oldParam
|
|
|
1560
1806
|
function isParam(partStr) {
|
|
1561
1807
|
return '{' === partStr[0] && '}' === partStr[partStr.length - 1];
|
|
1562
1808
|
}
|
|
1809
|
+
// A segment holding a placeholder, whole or beside other text, names no verb.
|
|
1810
|
+
function isVerb(partStr) {
|
|
1811
|
+
return !String(partStr ?? '').includes('{');
|
|
1812
|
+
}
|
|
1813
|
+
// A custom method (`/schedules:count`, `/users/{id}:activate`) reads as a
|
|
1814
|
+
// trailing verb segment after what it acts on.
|
|
1815
|
+
function verbParts(parts) {
|
|
1816
|
+
const m = (parts[parts.length - 1] ?? '').match(/^(.+):([A-Za-z][\w-]*)$/);
|
|
1817
|
+
return null == m ? parts : [...parts.slice(0, -1), m[1], m[2]];
|
|
1818
|
+
}
|
|
1819
|
+
// A parameter whose own name says it holds a name, such as `project_name`,
|
|
1820
|
+
// which a derived `<parent>_id` would misdescribe.
|
|
1821
|
+
function isNameParam(param) {
|
|
1822
|
+
return /(^|_)name$/.test((0, jostraca_2.snakify)((0, utility_2.normalizeFieldName)(param)));
|
|
1823
|
+
}
|
|
1824
|
+
function nameEntityParams(entity) {
|
|
1825
|
+
const given = {};
|
|
1826
|
+
for (const [pathStr, guidePath] of Object.entries(entity.path ?? {})) {
|
|
1827
|
+
given[pathStr] = {};
|
|
1828
|
+
for (const [param, rename] of Object.entries(guidePath.rename?.param ?? {})) {
|
|
1829
|
+
given[pathStr][param] = rename.target;
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
const named = entityParamNames(given);
|
|
1833
|
+
for (const [pathStr, guidePath] of Object.entries(entity.path ?? {})) {
|
|
1834
|
+
guidePath.rename = guidePath.rename ?? { param: {} };
|
|
1835
|
+
const renames = guidePath.rename.param = guidePath.rename.param ?? {};
|
|
1836
|
+
for (const param of Object.keys(renames)) {
|
|
1837
|
+
if (null == named[pathStr][param]) {
|
|
1838
|
+
delete renames[param];
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
for (const [param, target] of Object.entries(named[pathStr])) {
|
|
1842
|
+
if (target !== renames[param]?.target) {
|
|
1843
|
+
renames[param] = { target, why_rename: ['entity-param'] };
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
// An entity's paths with the renames of their parameters, given back with one
|
|
1849
|
+
// name for each parameter at the same place in several paths: its own name,
|
|
1850
|
+
// snake-cased as the paths would, unless some path makes it the entity's `id`.
|
|
1851
|
+
function entityParamNames(paths) {
|
|
1852
|
+
const out = {};
|
|
1853
|
+
const places = {};
|
|
1854
|
+
for (const pathStr of Object.keys(paths).sort(refcount_1.byCodePoint)) {
|
|
1855
|
+
out[pathStr] = { ...paths[pathStr] };
|
|
1856
|
+
const parts = verbParts(pathStr.split('/').filter((p) => '' !== p));
|
|
1857
|
+
parts.forEach((part, partI) => {
|
|
1858
|
+
if (isParam(part)) {
|
|
1859
|
+
const place = parts.slice(0, partI + 1).join('/');
|
|
1860
|
+
(places[place] = places[place] ?? []).push({ pathStr, param: part.slice(1, -1) });
|
|
1861
|
+
}
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
for (const place of Object.keys(places).sort(refcount_1.byCodePoint)) {
|
|
1865
|
+
const uses = places[place];
|
|
1866
|
+
const names = uses.map((use) => out[use.pathStr][use.param] ?? use.param);
|
|
1867
|
+
const param = uses[0].param;
|
|
1868
|
+
const snake = (0, utility_2.depluralize)((0, jostraca_2.snakify)((0, utility_2.normalizeFieldName)(param)));
|
|
1869
|
+
const name = names.includes('id') ? null :
|
|
1870
|
+
names.includes(snake) ? snake :
|
|
1871
|
+
names.includes(param) ? param : null;
|
|
1872
|
+
if (null == name || names.every((n) => n === name)) {
|
|
1873
|
+
continue;
|
|
1874
|
+
}
|
|
1875
|
+
for (const use of uses) {
|
|
1876
|
+
const renames = out[use.pathStr];
|
|
1877
|
+
if (paramNameTaken(use.pathStr, renames, use.param, name)) {
|
|
1878
|
+
continue;
|
|
1879
|
+
}
|
|
1880
|
+
if (name === use.param) {
|
|
1881
|
+
delete renames[use.param];
|
|
1882
|
+
}
|
|
1883
|
+
else {
|
|
1884
|
+
renames[use.param] = name;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
return out;
|
|
1889
|
+
}
|
|
1890
|
+
// As updateParamRename judges a clash: by what the path's other parameters
|
|
1891
|
+
// end up called.
|
|
1892
|
+
function paramNameTaken(pathStr, renames, param, name) {
|
|
1893
|
+
return Object.keys(renames).some((other) => other !== param && renames[other] === name) ||
|
|
1894
|
+
(pathStr.match(/\{([^}]+)\}/g) || [])
|
|
1895
|
+
.map((seg) => seg.slice(1, -1))
|
|
1896
|
+
.some((other) => other !== param && null == renames[other] &&
|
|
1897
|
+
(0, utility_2.canonize)(other) === name);
|
|
1898
|
+
}
|
|
1563
1899
|
function findcmps(data, pathStr, underprops, opts) {
|
|
1564
1900
|
const cmplist = [];
|
|
1565
1901
|
const cmpset = new Set();
|
|
@@ -1598,32 +1934,53 @@ function makeMethodEntityDesc(desc) {
|
|
|
1598
1934
|
};
|
|
1599
1935
|
return ment;
|
|
1600
1936
|
}
|
|
1601
|
-
function findPotentialSchemaRefs(pathStr, methodName, responses, envelope, why) {
|
|
1937
|
+
function findPotentialSchemaRefs(pathStr, methodName, responses, envelope, answered, why) {
|
|
1602
1938
|
const xrefs = [];
|
|
1603
|
-
for (const schema of
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
if ('' !==
|
|
1939
|
+
for (const schema of namingSchemas(methodName, responses, answered, envelope)) {
|
|
1940
|
+
// An envelope component names its wrapping, not the entity: the
|
|
1941
|
+
// component it carries takes its place.
|
|
1942
|
+
const ref = namingRef(schema, envelope);
|
|
1943
|
+
if (null != ref) {
|
|
1944
|
+
if (null != schema['x-ref'] && ref !== schema['x-ref']) {
|
|
1609
1945
|
why.push('envelope=' + cmpRefName(schema['x-ref']));
|
|
1610
|
-
xrefs.push(itemref);
|
|
1611
|
-
}
|
|
1612
|
-
else {
|
|
1613
|
-
xrefs.push(schema['x-ref']);
|
|
1614
1946
|
}
|
|
1615
|
-
|
|
1616
|
-
else if ('array' === schema.type && null != schema.items?.['x-ref']) {
|
|
1617
|
-
xrefs.push(schema.items?.['x-ref']);
|
|
1947
|
+
xrefs.push(ref);
|
|
1618
1948
|
}
|
|
1619
1949
|
}
|
|
1620
1950
|
(0, utility_2.debugpath)(pathStr, methodName, 'POTENTIAL-SCHEMA-REFS', xrefs);
|
|
1621
1951
|
return xrefs;
|
|
1622
1952
|
}
|
|
1953
|
+
const CMP_REF_RE = /\/(components\/schemas|definitions)\/(.+)$/;
|
|
1623
1954
|
function cmpRefName(xref) {
|
|
1624
|
-
const m = xref.match(
|
|
1955
|
+
const m = xref.match(CMP_REF_RE);
|
|
1625
1956
|
return null == m ? xref : (0, utility_2.canonizeCmpName)(m[2]);
|
|
1626
1957
|
}
|
|
1958
|
+
// An array request body is sent from a field named for the records it lists:
|
|
1959
|
+
// its items' component, or the one their allOf parts name, cleaned as an
|
|
1960
|
+
// entity's is, else the entity itself. A pointer into a component names a
|
|
1961
|
+
// part of it, not a record. A name another route of the operation has is taken.
|
|
1962
|
+
function arrayBodyField(schema, entname, taken = []) {
|
|
1963
|
+
const refs = [...new Set(itemRefs(schema?.items, new Set()))];
|
|
1964
|
+
const cmp = String(1 === refs.length ? refs[0] : '').match(CMP_REF_RE)?.[2];
|
|
1965
|
+
const cleaned = null == cmp || cmp.includes('/') ? '' :
|
|
1966
|
+
(0, utility_2.prefixLeadingDigit)((0, utility_2.cleanComponentName)((0, utility_2.canonizeCmpName)(cmp)));
|
|
1967
|
+
const record = '' === cleaned ? entname : cleaned;
|
|
1968
|
+
let name = (0, utility_2.pluralize)(record);
|
|
1969
|
+
for (let n = 1; taken.includes(name); n++) {
|
|
1970
|
+
name = record + '_list' + (1 < n ? n : '');
|
|
1971
|
+
}
|
|
1972
|
+
return name;
|
|
1973
|
+
}
|
|
1974
|
+
function itemRefs(items, seen) {
|
|
1975
|
+
if (null == items || 'object' !== typeof items || seen.has(items)) {
|
|
1976
|
+
return [];
|
|
1977
|
+
}
|
|
1978
|
+
seen.add(items);
|
|
1979
|
+
if ('string' === typeof items['x-ref']) {
|
|
1980
|
+
return [items['x-ref']];
|
|
1981
|
+
}
|
|
1982
|
+
return (Array.isArray(items.allOf) ? items.allOf : []).flatMap((part) => itemRefs(part, seen));
|
|
1983
|
+
}
|
|
1627
1984
|
function hasMethod(def, pathStr, methodName) {
|
|
1628
1985
|
const pathDef = def?.paths?.[pathStr];
|
|
1629
1986
|
const found = (null != pathDef
|