@voxgig/apidef 8.33.2 → 8.34.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/guide/heuristic01.d.ts +1 -1
- package/dist/guide/heuristic01.js +66 -10
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/parse.d.ts +2 -1
- package/dist/parse.js +41 -1
- package/dist/parse.js.map +1 -1
- package/dist/resolved.js +4 -2
- package/dist/resolved.js.map +1 -1
- package/dist/transform/args.js +1 -1
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/entity.js +6 -5
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.js +3 -2
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/top.js +8 -0
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +7 -2
- package/dist/utility.js +70 -3
- package/dist/utility.js.map +1 -1
- package/package.json +1 -1
- package/src/guide/heuristic01.ts +79 -9
- package/src/parse.ts +41 -1
- package/src/resolved.ts +5 -2
- package/src/transform/args.ts +2 -1
- package/src/transform/entity.ts +8 -6
- package/src/transform/field.ts +3 -2
- package/src/transform/top.ts +10 -1
- package/src/utility.ts +74 -3
package/src/guide/heuristic01.ts
CHANGED
|
@@ -136,9 +136,12 @@ const VERB_WORDS = new Set([
|
|
|
136
136
|
'accept', 'decline', 'notify', 'ping', 'count', 'exists', 'preview', 'render', 'generate',
|
|
137
137
|
'calculate', 'compute', 'convert', 'process', 'trigger', 'dispatch', 'apply', 'revoke',
|
|
138
138
|
'grant', 'save', 'lock', 'test', 'inject', 'analyze', 'autocomplete', 'bypass', 'contains',
|
|
139
|
-
'finish', 'reorder', 'shuffle',
|
|
139
|
+
'finish', 'reorder', 'shuffle', 'show', 'award', 'authorize', 'reload',
|
|
140
140
|
])
|
|
141
141
|
|
|
142
|
+
// A bare number is a release or an index, never a noun.
|
|
143
|
+
const NUMBER_PART_RE = /^\d+$/
|
|
144
|
+
|
|
142
145
|
// A literal that names an identifier keys a resource rather than naming one.
|
|
143
146
|
const ID_LITERAL_RE = /(^|_)ids?$/
|
|
144
147
|
|
|
@@ -146,6 +149,17 @@ const ID_LITERAL_RE = /(^|_)ids?$/
|
|
|
146
149
|
// of results, names no entity.
|
|
147
150
|
const GENERIC_CMP_WORDS = ['default', 'empty', 'ok', 'response', 'result', 'success', 'void']
|
|
148
151
|
|
|
152
|
+
// A name made only of these words, as ResponseListResource, names nothing.
|
|
153
|
+
const GENERIC_NAME_WORDS = [...GENERIC_CMP_WORDS, 'list', 'resource']
|
|
154
|
+
|
|
155
|
+
// A generic word and a number, as a generator names what it was not told
|
|
156
|
+
// to name: Example17, Model3, InlineObject2.
|
|
157
|
+
const GENERATED_CMP_RE = /^(example|model|schema|body|inline|inline_object|object)_?\d+$/
|
|
158
|
+
|
|
159
|
+
// The body a generator names for the operation it is sent to,
|
|
160
|
+
// Body_create_item_items_post, names nothing either.
|
|
161
|
+
const OPERATION_BODY_RE = /^body_.+_(get|put|post|patch|delete)$/
|
|
162
|
+
|
|
149
163
|
// A version tag groups routes by release, not by resource.
|
|
150
164
|
const VERSION_TAG_RE = /^v\d+([a-z]+\d*)?$/
|
|
151
165
|
|
|
@@ -1052,7 +1066,8 @@ function pathEntityName(
|
|
|
1052
1066
|
why: string[],
|
|
1053
1067
|
): string | null {
|
|
1054
1068
|
const ment = mdesc.MethodEntity
|
|
1055
|
-
const joined = methodOf(mdesc.path) ??
|
|
1069
|
+
const joined = methodOf(mdesc.path) ?? procedureOf(mdesc.path) ??
|
|
1070
|
+
idsOfItem(data, parts, pathowner) ?? singletonPartOf(data, parts, pathowner) ??
|
|
1056
1071
|
readOfItem(data, mdesc, parts, pathowner)
|
|
1057
1072
|
if (null != joined) {
|
|
1058
1073
|
ment.verb_on_parent = parts[parts.length - 1] ?? joined.action
|
|
@@ -1132,7 +1147,7 @@ function pathEntityName(
|
|
|
1132
1147
|
// A short literal names nothing: the component the route answers, else
|
|
1133
1148
|
// the route's key, else what a route with no literal is named by, else the
|
|
1134
1149
|
// padded literal.
|
|
1135
|
-
if (null != pm && shortLiteral(pm)) {
|
|
1150
|
+
if (null != pm && shortLiteral(pm, parts)) {
|
|
1136
1151
|
why.push('short-literal')
|
|
1137
1152
|
const answered = answeredCmp(mdesc)
|
|
1138
1153
|
if (null != answered) {
|
|
@@ -1502,6 +1517,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1502
1517
|
}
|
|
1503
1518
|
|
|
1504
1519
|
const parts = verbParts(spec.data.work.pathmap[pathStr].parts)
|
|
1520
|
+
.filter((part, i, all) => all.length - 1 === i || !NUMBER_PART_RE.test(part))
|
|
1505
1521
|
|
|
1506
1522
|
const fourthLastPart = parts[parts.length - 4]
|
|
1507
1523
|
const fourthLastPartCanon = literalName(fourthLastPart)
|
|
@@ -1936,7 +1952,9 @@ function BuildEntity(spec: TaskSpec) {
|
|
|
1936
1952
|
|
|
1937
1953
|
function genericCmp(name: string, refs: Record<string, number>, schema?: any): boolean {
|
|
1938
1954
|
if (GENERIC_CMP_WORDS.includes(name) || ID_LITERAL_RE.test(name) || emptyObjectSchema(schema) ||
|
|
1939
|
-
('object' === name && 0 === Object.keys(mergedProperties(schema) ?? {}).length)
|
|
1955
|
+
('object' === name && 0 === Object.keys(mergedProperties(schema) ?? {}).length) ||
|
|
1956
|
+
GENERATED_CMP_RE.test(name) || OPERATION_BODY_RE.test(name) ||
|
|
1957
|
+
(name.includes('_') && name.split('_').every((word) => GENERIC_NAME_WORDS.includes(word)))) {
|
|
1940
1958
|
return true
|
|
1941
1959
|
}
|
|
1942
1960
|
const at = name.lastIndexOf('_')
|
|
@@ -2016,7 +2034,7 @@ function pathNoun(pm: PathMatch, parts: string[], why: string[]): string {
|
|
|
2016
2034
|
const lit = snakify(parts[at])
|
|
2017
2035
|
if (null != custom || (isVerbWord(parts[at]) && !keyedBy(parts[at + 1], name))) {
|
|
2018
2036
|
for (let i = at - 1; 0 <= i; i--) {
|
|
2019
|
-
if (!isParam(parts[i])) {
|
|
2037
|
+
if (!isParam(parts[i]) && !NUMBER_PART_RE.test(parts[i])) {
|
|
2020
2038
|
// A version segment ends the base path, so no noun precedes the verb.
|
|
2021
2039
|
if (VERSION_TAG_RE.test(parts[i].toLowerCase())) {
|
|
2022
2040
|
break
|
|
@@ -2342,6 +2360,45 @@ function methodOf(pathStr: string): { parent: string, action: string, why: strin
|
|
|
2342
2360
|
}
|
|
2343
2361
|
|
|
2344
2362
|
|
|
2363
|
+
// A list of identifiers beneath an item key, and each identifier in it, is
|
|
2364
|
+
// an action of that item: `/lists/{id}/ids` and `/lists/{id}/ids/{itemId}`.
|
|
2365
|
+
function idsOfItem(
|
|
2366
|
+
data: { def: any, work: any },
|
|
2367
|
+
parts: string[],
|
|
2368
|
+
pathowner: Record<string, Record<string, string>>,
|
|
2369
|
+
): { parent: string, action: string, why: string } | null {
|
|
2370
|
+
const at = isParam(parts[parts.length - 1] ?? '') ? parts.length - 2 : parts.length - 1
|
|
2371
|
+
if (at < 2 || isParam(parts[at]) || !isParam(parts[at - 1]) ||
|
|
2372
|
+
'ids' !== snakify(parts[at]).replace(/^.*_/, '')) {
|
|
2373
|
+
return null
|
|
2374
|
+
}
|
|
2375
|
+
const route = pathNamed(data, '/' + parts.slice(0, at).join('/'))
|
|
2376
|
+
const owners = null == route ? {} : (pathowner[route] ?? {})
|
|
2377
|
+
const parent = owners.GET ?? owners.QUERY ?? Object.values(owners).sort()[0]
|
|
2378
|
+
return null == parent ? null : { parent, action: snakify(parts[at]), why: 'ids-of-item' }
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
|
|
2382
|
+
// Verbs a remote procedure opens with beside the verb words.
|
|
2383
|
+
const PROCEDURE_VERBS = ['put', 'set', 'post']
|
|
2384
|
+
|
|
2385
|
+
// A remote procedure in an `Action=` value or after a segment's last dot: the
|
|
2386
|
+
// object after its verb names the entity, and the verb is the action.
|
|
2387
|
+
function procedureOf(pathStr: string): { parent: string, action: string, why: string } | null {
|
|
2388
|
+
const segs = pathStr.replace(/[?#].*$/, '').split('/')
|
|
2389
|
+
const action = pathStr.match(/[#?&]Action=([A-Za-z][A-Za-z0-9_]*)/)?.[1]
|
|
2390
|
+
const named = action ?? segs[segs.length - 1].match(/^[^{}]*\.([A-Za-z][A-Za-z0-9_]*)$/)?.[1]
|
|
2391
|
+
const words = null == named ? [] : canonize(named).split('_')
|
|
2392
|
+
// An Action value may open with a word before its verb, as Batch does; a
|
|
2393
|
+
// dotted name only with the verb, so a class name is not read as one.
|
|
2394
|
+
const verb = words.slice(0, null == action ? 1 : 2).findIndex((word) =>
|
|
2395
|
+
VERB_WORDS.has(word) || PROCEDURE_VERBS.includes(word))
|
|
2396
|
+
const parent = verb < 0 ? '' : words.slice(verb + 1).join('_')
|
|
2397
|
+
return parent.replace(/_/g, '').length < 3 ? null :
|
|
2398
|
+
{ parent, action: words.slice(0, verb + 1).join('_'), why: 'procedure' }
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
|
|
2345
2402
|
// A sub-path of a keyless singleton, read, written and deleted with no schema
|
|
2346
2403
|
// and no key beneath it, is an action of that singleton, plural or not.
|
|
2347
2404
|
function singletonPartOf(
|
|
@@ -2391,6 +2448,15 @@ function readOfItem(
|
|
|
2391
2448
|
if (key < 0 || lastLit < 0) {
|
|
2392
2449
|
return null
|
|
2393
2450
|
}
|
|
2451
|
+
// A key of its literal's collection reads the item; a modifier, as a size,
|
|
2452
|
+
// after a literal that is no download itself, makes a download.
|
|
2453
|
+
const tail = parts[parts.length - 1]
|
|
2454
|
+
const prefix = pathNamed(data, '/' + parts.slice(0, -1).join('/'))
|
|
2455
|
+
const prefixRead = null == prefix ? null : data.def?.paths?.[prefix]?.get
|
|
2456
|
+
if (isParam(tail) && (keyedBy(tail, literalName(parts[lastLit])) || (null != prefix &&
|
|
2457
|
+
!(null != prefixRead && binaryResponse(prefixRead.responses))))) {
|
|
2458
|
+
return null
|
|
2459
|
+
}
|
|
2394
2460
|
const literals = parts.slice(key + 1).filter((part) => !isParam(part))
|
|
2395
2461
|
const detail = parts.length - 1 === lastLit && key === lastLit - 1 &&
|
|
2396
2462
|
ITEM_READ_WORDS.includes(literalName(parts[lastLit]))
|
|
@@ -2562,9 +2628,10 @@ function verbActionName(literals: string[]): string {
|
|
|
2562
2628
|
}
|
|
2563
2629
|
|
|
2564
2630
|
|
|
2565
|
-
// A literal too short for a word names nothing.
|
|
2566
|
-
function shortLiteral(pm: PathMatch): boolean {
|
|
2567
|
-
|
|
2631
|
+
// A literal too short for a word names nothing, nor does a verb whose noun is.
|
|
2632
|
+
function shortLiteral(pm: PathMatch, parts: string[]): boolean {
|
|
2633
|
+
const short = (lit: string) => canonize(lit).replace(/_/g, '').length < 3
|
|
2634
|
+
return short(pm[PATH_NAME_INDEX[pm.expr]]) || short(pathNoun(pm, parts, []))
|
|
2568
2635
|
}
|
|
2569
2636
|
|
|
2570
2637
|
|
|
@@ -2574,7 +2641,7 @@ function nounless(pm: PathMatch, parts: string[]): boolean {
|
|
|
2574
2641
|
const at = pm.index + PATH_NAME_INDEX[pm.expr]
|
|
2575
2642
|
const name = literalName(parts[at])
|
|
2576
2643
|
return GENERIC_WORDS.includes(name) ||
|
|
2577
|
-
(
|
|
2644
|
+
(isVerbWord(parts[at]) && parts.slice(0, at).every((part) => isParam(part)))
|
|
2578
2645
|
}
|
|
2579
2646
|
|
|
2580
2647
|
|
|
@@ -2847,6 +2914,9 @@ function commandBodyName(data: { def: any, guide: any, work: any }, mdesc: any):
|
|
|
2847
2914
|
return null
|
|
2848
2915
|
}
|
|
2849
2916
|
let name = cleanCmp(data, canonizeCmpName(cmpRefName(xref)))
|
|
2917
|
+
if (GENERATED_CMP_RE.test(name) || OPERATION_BODY_RE.test(name)) {
|
|
2918
|
+
return null
|
|
2919
|
+
}
|
|
2850
2920
|
for (;;) {
|
|
2851
2921
|
const suffix = COMMAND_SUFFIXES.find((sfx) => name.endsWith(sfx) && sfx.length < name.length)
|
|
2852
2922
|
if (null == suffix) {
|
package/src/parse.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { Jsonic } from '@tabnas/jsonic'
|
|
4
4
|
import { Yaml } from '@tabnas/yaml'
|
|
5
5
|
|
|
6
|
-
import { relativizePath } from './utility'
|
|
6
|
+
import { paramKey, relativizePath } from './utility'
|
|
7
7
|
import { yamlAliasedSize, yamlAliasLimit } from './yamlalias'
|
|
8
8
|
|
|
9
9
|
import { parseGraphQL } from './parse/graphql'
|
|
@@ -124,6 +124,7 @@ async function parseOpenAPI(source: any, _meta?: any) {
|
|
|
124
124
|
// parameter declared by `$ref` counts. See docs/design/derived-names.md
|
|
125
125
|
normalizeColonPathParams(parsed, _meta)
|
|
126
126
|
dropIgnoredHeaders(parsed)
|
|
127
|
+
dropDuplicateParams(parsed)
|
|
127
128
|
|
|
128
129
|
const def = decycle(parsed)
|
|
129
130
|
|
|
@@ -163,6 +164,44 @@ function dropIgnoredHeaders(parsed: any) {
|
|
|
163
164
|
}
|
|
164
165
|
|
|
165
166
|
|
|
167
|
+
// A parameter list must not repeat a location and name (OpenAPI 3 and Swagger
|
|
168
|
+
// 2 alike). A definition that does keeps the first and records the rest for a
|
|
169
|
+
// warning. Header names compare without case, as HTTP compares them.
|
|
170
|
+
const DUPLICATE_PARAMS_KEY = 'x-apidef-duplicate-params'
|
|
171
|
+
|
|
172
|
+
function dropDuplicateParams(parsed: any) {
|
|
173
|
+
delete parsed[DUPLICATE_PARAMS_KEY]
|
|
174
|
+
if (!isRecord(parsed.paths)) return
|
|
175
|
+
|
|
176
|
+
const dropped: { path: string, method?: string, in: string, name: string }[] = []
|
|
177
|
+
const kept = (list: any[], path: string, method?: string) => {
|
|
178
|
+
const seen = new Set<string>()
|
|
179
|
+
return list.filter((param: any) => {
|
|
180
|
+
const key = paramKey(param)
|
|
181
|
+
if (null == key) return true
|
|
182
|
+
if (!seen.has(key)) {
|
|
183
|
+
seen.add(key)
|
|
184
|
+
return true
|
|
185
|
+
}
|
|
186
|
+
dropped.push({ path, ...(method ? { method: method.toUpperCase() } : {}), in: param.in, name: param.name })
|
|
187
|
+
return false
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (const path of Object.keys(parsed.paths).sort()) {
|
|
192
|
+
const item = parsed.paths[path]
|
|
193
|
+
if (!isRecord(item)) continue
|
|
194
|
+
if (Array.isArray(item.parameters)) item.parameters = kept(item.parameters, path)
|
|
195
|
+
for (const method of METHODS) {
|
|
196
|
+
const op = item[method]
|
|
197
|
+
if (isRecord(op) && Array.isArray(op.parameters)) op.parameters = kept(op.parameters, path, method)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (0 < dropped.length) parsed[DUPLICATE_PARAMS_KEY] = dropped
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
166
205
|
// A surrogate escape, or a raw lone surrogate unit.
|
|
167
206
|
const SURROGATE_RE =
|
|
168
207
|
/\\(?:u\{?|U)0*[dD][89a-fA-F][0-9a-fA-F]{2}|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/
|
|
@@ -455,6 +494,7 @@ export {
|
|
|
455
494
|
normalizePathKeys,
|
|
456
495
|
colonPathKeys,
|
|
457
496
|
IGNORED_HEADERS_KEY,
|
|
497
|
+
DUPLICATE_PARAMS_KEY,
|
|
458
498
|
}
|
|
459
499
|
|
|
460
500
|
|
package/src/resolved.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
// See docs/design/resolved-spec-capability.md
|
|
4
4
|
|
|
5
|
+
import { routeParams } from './utility'
|
|
6
|
+
|
|
5
7
|
// An operation needs its argument types, including recursive input objects.
|
|
6
8
|
// Output types are represented by the field and generated invocation selection;
|
|
7
9
|
// copying the entire connected output graph per operation is quadratic in API size.
|
|
@@ -65,8 +67,9 @@ function operationFacts(def: any, point: { m: string, o: string }): OperationFac
|
|
|
65
67
|
if (undefined !== method[key]) facts[key] = method[key]
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
// A path item may declare parameters shared by every operation under it
|
|
69
|
-
|
|
70
|
+
// A path item may declare parameters shared by every operation under it;
|
|
71
|
+
// the operation's own declaration of one replaces it.
|
|
72
|
+
facts.parameters = routeParams(path, method)
|
|
70
73
|
|
|
71
74
|
facts.security = method.security ?? def.security
|
|
72
75
|
facts.securitySource = method.security !== undefined ? 'operation' :
|
package/src/transform/args.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
isFileType,
|
|
12
12
|
normalizeFieldName,
|
|
13
13
|
paramName,
|
|
14
|
+
routeParams,
|
|
14
15
|
validator,
|
|
15
16
|
} from '../utility'
|
|
16
17
|
|
|
@@ -91,7 +92,7 @@ const argsTransform: Transform = async function(
|
|
|
91
92
|
function routeArgdefs(def: any, mpoint: ModelPoint): ParameterDef[] {
|
|
92
93
|
const pathdef: PathDef = def.paths?.[mpoint.o]
|
|
93
94
|
const opdef: MethodDef = (pathdef as any)?.[mpoint.m.toLowerCase()]
|
|
94
|
-
return
|
|
95
|
+
return routeParams(pathdef, opdef)
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
|
package/src/transform/entity.ts
CHANGED
|
@@ -24,7 +24,8 @@ import type {
|
|
|
24
24
|
|
|
25
25
|
import {
|
|
26
26
|
canonize, cmpRefName, depluralize, foldName, guideActive, guideParts, guidePath, isParam,
|
|
27
|
-
|
|
27
|
+
selectorParts, selectorPath,
|
|
28
|
+
paramName, paramOf, READ_METHODS, routeParams,
|
|
28
29
|
} from '../utility'
|
|
29
30
|
|
|
30
31
|
import { byCodePoint } from '../refcount'
|
|
@@ -366,9 +367,10 @@ function mergeCommandEntities(guide: any, log?: any): string[] {
|
|
|
366
367
|
// are. Guide stage only.
|
|
367
368
|
function separateSharedPoints(guide: any, def: any, log?: any): string[] {
|
|
368
369
|
const separated: string[] = []
|
|
369
|
-
const literals = (key: string) =>
|
|
370
|
+
const literals = (key: string) => selectorParts(key).filter((part: string) => !isParam(part))
|
|
370
371
|
const actionOf = (lits: string[]) => lits
|
|
371
|
-
.map((lit: string) => snakify(lit.replace(/\.?\{[^}]*\}/g, '')
|
|
372
|
+
.map((lit: string) => snakify(lit.replace(/\.?\{[^}]*\}/g, '').replace(/\./g, '_'))
|
|
373
|
+
.replace(/[^a-zA-Z0-9_]/g, ''))
|
|
372
374
|
.filter((word: string) => '' !== word).join('_')
|
|
373
375
|
|
|
374
376
|
for (const ename of Object.keys(guide.entity ?? {}).sort(byCodePoint)) {
|
|
@@ -399,7 +401,7 @@ function separateSharedPoints(guide: any, def: any, log?: any): string[] {
|
|
|
399
401
|
const paths = groups[key].filter((pathStr: string) =>
|
|
400
402
|
0 === Object.keys(entity.path[pathStr]?.action ?? {}).length)
|
|
401
403
|
const names = (key: string) => depluralize(snakify(literals(key).pop() ?? '')) === ename ? 0 : 1
|
|
402
|
-
const keys = [...new Set(paths.map(
|
|
404
|
+
const keys = [...new Set(paths.map(selectorPath))].sort((a: string, b: string) =>
|
|
403
405
|
names(a) - names(b) || literals(a).length - literals(b).length ||
|
|
404
406
|
a.length - b.length || byCodePoint(a, b))
|
|
405
407
|
if (keys.length < 2) continue
|
|
@@ -411,7 +413,7 @@ function separateSharedPoints(guide: any, def: any, log?: any): string[] {
|
|
|
411
413
|
if ('' === action || taken.has(action)) action = actionOf(literals(other))
|
|
412
414
|
if ('' === action || taken.has(action)) continue
|
|
413
415
|
taken.add(action)
|
|
414
|
-
for (const pathStr of paths.filter((p: string) =>
|
|
416
|
+
for (const pathStr of paths.filter((p: string) => selectorPath(p) === other)) {
|
|
415
417
|
entity.path[pathStr].action = {
|
|
416
418
|
[action]: { why_action: ['ent', ename, 'shared-selector', plain] }
|
|
417
419
|
}
|
|
@@ -432,7 +434,7 @@ function separateSharedPoints(guide: any, def: any, log?: any): string[] {
|
|
|
432
434
|
// The names of the query, header and cookie arguments an operation requires.
|
|
433
435
|
function requiredArgs(def: any, pathStr: string, method: string): string[] {
|
|
434
436
|
const pathItem = def?.paths?.[pathStr] ?? {}
|
|
435
|
-
return
|
|
437
|
+
return routeParams(pathItem, pathItem[method])
|
|
436
438
|
.filter((param: any) => true === param?.required && 'path' !== param?.in &&
|
|
437
439
|
'body' !== param?.in && 'formData' !== param?.in && 'string' === typeof param?.name)
|
|
438
440
|
.map((param: any) => canonize(param.name))
|
package/src/transform/field.ts
CHANGED
|
@@ -731,7 +731,7 @@ function modelField(fielddef: SchemaDef): ModelField {
|
|
|
731
731
|
n: name,
|
|
732
732
|
h: humanTitle(name),
|
|
733
733
|
t: inferFieldType(name, validator(nullableType(fielddef))),
|
|
734
|
-
r:
|
|
734
|
+
r: true === (fielddef as any).required,
|
|
735
735
|
op: {},
|
|
736
736
|
}
|
|
737
737
|
const fdesc = (fielddef as any).description
|
|
@@ -1027,7 +1027,8 @@ function composedFields(schema: any): any[] {
|
|
|
1027
1027
|
return Object.keys(decls).map((name) => {
|
|
1028
1028
|
const property = 1 === decls[name].length ? decls[name][0] : mergeDeclarations(decls[name])
|
|
1029
1029
|
// A copy: parsed schemas are shared by every operation referencing them.
|
|
1030
|
-
|
|
1030
|
+
// An object property's own `required` lists ITS fields: only the parent's says it is required.
|
|
1031
|
+
return true !== property.required && required.has(name) ? { ...property, required: true } : property
|
|
1031
1032
|
})
|
|
1032
1033
|
}
|
|
1033
1034
|
|
package/src/transform/top.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
} from '../utility'
|
|
9
9
|
|
|
10
10
|
import { METHODS } from '../resolved'
|
|
11
|
-
import { IGNORED_HEADERS_KEY } from '../parse'
|
|
11
|
+
import { DUPLICATE_PARAMS_KEY, IGNORED_HEADERS_KEY } from '../parse'
|
|
12
12
|
|
|
13
13
|
import type { TransformResult } from '../transform'
|
|
14
14
|
|
|
@@ -143,6 +143,15 @@ const topTransform = async function(
|
|
|
143
143
|
})
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
for (const repeat of def[DUPLICATE_PARAMS_KEY] ?? []) {
|
|
147
|
+
ctx.warn?.({
|
|
148
|
+
note: repeat.in + ' parameter `' + repeat.name + '` of ' +
|
|
149
|
+
(null == repeat.method ? '' : repeat.method + ' ') + repeat.path +
|
|
150
|
+
' is declared more than once: the first declaration is used (OpenAPI forbids' +
|
|
151
|
+
' a repeated parameter; fix the definition)',
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
146
155
|
const summary = resolveSummary(def)
|
|
147
156
|
if (null != summary) {
|
|
148
157
|
kit.info.summary = summary
|
package/src/utility.ts
CHANGED
|
@@ -1261,7 +1261,18 @@ function stripSchemaNamespace(name: string): string {
|
|
|
1261
1261
|
// uniformly wherever schema refs are counted or resolved (MeasureRef,
|
|
1262
1262
|
// ResolveEntityComponent, findcmps) so the metric keys stay consistent.
|
|
1263
1263
|
function canonizeCmpName(orig: string): string {
|
|
1264
|
-
return canonize(stripSchemaNamespace(orig))
|
|
1264
|
+
return canonize(stripToolAffixes(stripSchemaNamespace(orig)))
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
const TOOL_SUFFIX_RE = /^(.*[a-z0-9])(Output|Input)$/
|
|
1269
|
+
|
|
1270
|
+
// A generator's module path and its Output or Input are no part of a name.
|
|
1271
|
+
function stripToolAffixes(name: string): string {
|
|
1272
|
+
if (null == name) return name
|
|
1273
|
+
const at = name.lastIndexOf('__')
|
|
1274
|
+
const bare = 0 < at && at + 2 < name.length ? name.slice(at + 2) : name
|
|
1275
|
+
return bare.match(TOOL_SUFFIX_RE)?.[1] ?? bare
|
|
1265
1276
|
}
|
|
1266
1277
|
|
|
1267
1278
|
|
|
@@ -1297,6 +1308,15 @@ const FORMAT_EXTENSIONS = new Set([
|
|
|
1297
1308
|
// repos.json and data.{format} are repos and data, and a format alone (json)
|
|
1298
1309
|
// names nothing.
|
|
1299
1310
|
function guideSegment(seg0: string): string | null {
|
|
1311
|
+
const seg = seg0.replace(/\$\{/g, '{')
|
|
1312
|
+
const dot = dotOutsidePlaceholder(seg)
|
|
1313
|
+
if (0 === dot) return null
|
|
1314
|
+
if (0 < dot) return seg.slice(0, dot)
|
|
1315
|
+
return FORMAT_EXTENSIONS.has(seg.toLowerCase()) ? null : seg
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
// A segment as a selector: a format dropped, a dotted name kept whole.
|
|
1319
|
+
function selectorSegment(seg0: string): string | null {
|
|
1300
1320
|
const seg = seg0.replace(/\$\{/g, '{')
|
|
1301
1321
|
const held = seg.match(/^(\{[^}]+\})\.(?:[A-Za-z0-9]+|\{[^}]+\})$/)
|
|
1302
1322
|
if (null != held) return held[1]
|
|
@@ -1307,13 +1327,24 @@ function guideSegment(seg0: string): string | null {
|
|
|
1307
1327
|
return FORMAT_EXTENSIONS.has(seg.toLowerCase()) ? null : seg
|
|
1308
1328
|
}
|
|
1309
1329
|
|
|
1330
|
+
// What precedes this dot names a segment; what follows does not.
|
|
1331
|
+
function dotOutsidePlaceholder(seg: string): number {
|
|
1332
|
+
let depth = 0
|
|
1333
|
+
for (let i = 0; i < seg.length; i++) {
|
|
1334
|
+
const c = seg[i]
|
|
1335
|
+
depth += '{' === c ? 1 : '}' === c ? -1 : 0
|
|
1336
|
+
if ('.' === c && 0 === depth) return i
|
|
1337
|
+
}
|
|
1338
|
+
return -1
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1310
1341
|
// The guide's parts of a route, query and fragment cut; a route that is all
|
|
1311
1342
|
// fragment, or of formats alone, keeps them.
|
|
1312
|
-
function guideParts(path: string): string[] {
|
|
1343
|
+
function guideParts(path: string, segment = guideSegment): string[] {
|
|
1313
1344
|
const split = (route: string) => route.replace(/\?.*$/, '').split('/').filter((p: string) => '' !== p)
|
|
1314
1345
|
const cut = split(path.replace(/#.*$/, ''))
|
|
1315
1346
|
const parts = 0 === cut.length ? split(path) : cut
|
|
1316
|
-
const kept = parts.map(
|
|
1347
|
+
const kept = parts.map(segment).filter((p): p is string => null != p)
|
|
1317
1348
|
return 0 === kept.length ? parts : kept
|
|
1318
1349
|
}
|
|
1319
1350
|
|
|
@@ -1321,6 +1352,14 @@ function guidePath(path: string): string {
|
|
|
1321
1352
|
return '/' + guideParts(path).join('/')
|
|
1322
1353
|
}
|
|
1323
1354
|
|
|
1355
|
+
function selectorParts(path: string): string[] {
|
|
1356
|
+
return guideParts(path, selectorSegment)
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
function selectorPath(path: string): string {
|
|
1360
|
+
return '/' + selectorParts(path).join('/')
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1324
1363
|
|
|
1325
1364
|
// aontu cannot unify a spread with a key Object.prototype owns, so no entity
|
|
1326
1365
|
// is given one of these names. Listed, not read, so both ports agree.
|
|
@@ -2773,7 +2812,37 @@ function firstSentence(text: string): string {
|
|
|
2773
2812
|
}
|
|
2774
2813
|
|
|
2775
2814
|
|
|
2815
|
+
// The identity OpenAPI gives a parameter: its location and name.
|
|
2816
|
+
function paramKey(param: any): string | undefined {
|
|
2817
|
+
if (null == param || 'object' !== typeof param || 'string' !== typeof param.in || 'string' !== typeof param.name) return undefined
|
|
2818
|
+
return param.in + '\u0000' + ('header' === param.in ? param.name.toLowerCase() : param.name)
|
|
2819
|
+
}
|
|
2820
|
+
|
|
2821
|
+
|
|
2822
|
+
// A route's parameters: the path item's, each replaced by the operation's own
|
|
2823
|
+
// declaration of the same location and name, then the operation's others.
|
|
2824
|
+
function routeParams(pathItem: any, op: any): any[] {
|
|
2825
|
+
const own: any[] = Array.isArray(op?.parameters) ? op.parameters : []
|
|
2826
|
+
const byKey = new Map<string, any>()
|
|
2827
|
+
for (const param of own) {
|
|
2828
|
+
const key = paramKey(param)
|
|
2829
|
+
if (null != key && !byKey.has(key)) byKey.set(key, param)
|
|
2830
|
+
}
|
|
2831
|
+
const used = new Set<any>()
|
|
2832
|
+
const shared = (Array.isArray(pathItem?.parameters) ? pathItem.parameters : []).map((param: any) => {
|
|
2833
|
+
const key = paramKey(param)
|
|
2834
|
+
const mine = null == key ? undefined : byKey.get(key)
|
|
2835
|
+
if (null == mine) return param
|
|
2836
|
+
used.add(mine)
|
|
2837
|
+
return mine
|
|
2838
|
+
})
|
|
2839
|
+
return [...shared, ...own.filter((param) => !used.has(param))]
|
|
2840
|
+
}
|
|
2841
|
+
|
|
2842
|
+
|
|
2776
2843
|
export {
|
|
2844
|
+
paramKey,
|
|
2845
|
+
routeParams,
|
|
2777
2846
|
bodyRequestTransform,
|
|
2778
2847
|
nom,
|
|
2779
2848
|
getdlog,
|
|
@@ -2824,6 +2893,8 @@ export {
|
|
|
2824
2893
|
guardPrototypeName,
|
|
2825
2894
|
guideParts,
|
|
2826
2895
|
guidePath,
|
|
2896
|
+
selectorParts,
|
|
2897
|
+
selectorPath,
|
|
2827
2898
|
stripOperationVerb,
|
|
2828
2899
|
debugpath,
|
|
2829
2900
|
debugpathOn,
|