@voxgig/apidef 8.10.0 → 8.12.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 +4 -1
- package/dist/apidef.js +12 -34
- package/dist/apidef.js.map +1 -1
- package/dist/builder/entity/entity.js +0 -33
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/builder/entity/info.js +0 -4
- package/dist/builder/entity/info.js.map +1 -1
- package/dist/builder/flow.d.ts +4 -1
- package/dist/builder/flow.js +36 -27
- package/dist/builder/flow.js.map +1 -1
- package/dist/guide/graphql01.js +0 -55
- package/dist/guide/graphql01.js.map +1 -1
- package/dist/guide/guide.js +3 -126
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.js +40 -181
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/parse/graphql.js +0 -25
- package/dist/parse/graphql.js.map +1 -1
- package/dist/parse.js +51 -56
- package/dist/parse.js.map +1 -1
- package/dist/resolved.d.ts +23 -0
- package/dist/resolved.js +92 -0
- package/dist/resolved.js.map +1 -0
- package/dist/resolver.js +0 -2
- package/dist/resolver.js.map +1 -1
- package/dist/transform/args.js +2 -25
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/casecollide.d.ts +3 -0
- package/dist/transform/casecollide.js +67 -0
- package/dist/transform/casecollide.js.map +1 -0
- package/dist/transform/contract.js +43 -38
- package/dist/transform/contract.js.map +1 -1
- package/dist/transform/entity.js +0 -56
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.js +19 -352
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/flowstep.js +0 -49
- package/dist/transform/flowstep.js.map +1 -1
- package/dist/transform/graphql.js +0 -36
- package/dist/transform/graphql.js.map +1 -1
- package/dist/transform/operation.js +0 -23
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/top.js +1 -128
- package/dist/transform/top.js.map +1 -1
- package/dist/transform.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.js.map +1 -1
- package/dist/utility.d.ts +2 -1
- package/dist/utility.js +17 -316
- package/dist/utility.js.map +1 -1
- package/model/apidef.aon +20 -113
- package/model/guide.aon +1 -32
- package/package.json +4 -4
- package/src/apidef.ts +22 -33
- package/src/builder/entity/entity.ts +0 -33
- package/src/builder/entity/info.ts +0 -4
- package/src/builder/flow.ts +39 -24
- package/src/desc.ts +0 -22
- package/src/guide/graphql01.ts +0 -75
- package/src/guide/guide.ts +5 -127
- package/src/guide/heuristic01.ts +47 -182
- package/src/model.ts +6 -85
- package/src/parse/graphql.ts +0 -25
- package/src/parse.ts +61 -56
- package/src/resolved.ts +136 -0
- package/src/resolver.ts +0 -2
- package/src/transform/args.ts +2 -25
- package/src/transform/casecollide.ts +80 -0
- package/src/transform/contract.ts +44 -34
- package/src/transform/entity.ts +0 -56
- package/src/transform/field.ts +28 -353
- package/src/transform/flowstep.ts +0 -49
- package/src/transform/graphql.ts +0 -36
- package/src/transform/operation.ts +0 -23
- package/src/transform/top.ts +1 -131
- package/src/transform.ts +0 -1
- package/src/types.ts +4 -13
- package/src/utility.ts +18 -314
package/dist/transform/field.js
CHANGED
|
@@ -19,23 +19,6 @@ const fieldTransform = async function (ctx) {
|
|
|
19
19
|
if (mop) {
|
|
20
20
|
const mpoints = mop.points;
|
|
21
21
|
for (let mpoint of mpoints) {
|
|
22
|
-
// ACTION POINTS CONTRIBUTE NO FIELDS, as `identityParams` and
|
|
23
|
-
// `responseCandidates` below already assume: an action is a VERB
|
|
24
|
-
// dispatched by `$action`, so its request body is that verb's
|
|
25
|
-
// arguments and its response is that verb's result. Neither says
|
|
26
|
-
// anything about what a record of this entity carries.
|
|
27
|
-
//
|
|
28
|
-
// A custom action lands under `create`, so every point of it was
|
|
29
|
-
// harvested along with the plain create's. solar's planet — four
|
|
30
|
-
// properties in the spec — came out with ten fields, the extra six
|
|
31
|
-
// being `{start, stop}` and `{forbid, why}` from the two action
|
|
32
|
-
// bodies and `{ok, state}` from their shared response envelope.
|
|
33
|
-
// Those reached the generated `Planet` type, its create and update
|
|
34
|
-
// data types, and the per-entity field table in the generated
|
|
35
|
-
// reference, none of which a planet has ever carried.
|
|
36
|
-
if (null != mpoint?.select?.['$action']) {
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
22
|
const opfields = resolveOpFields(ment, mop, mpoint, def);
|
|
40
23
|
for (let opfield of opfields) {
|
|
41
24
|
if (!seen[opfield.name]) {
|
|
@@ -52,40 +35,9 @@ const fieldTransform = async function (ctx) {
|
|
|
52
35
|
fields.sort((a, b) => {
|
|
53
36
|
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
|
54
37
|
});
|
|
55
|
-
// Mark the entity as having an id only when the spec actually declares one.
|
|
56
|
-
// Downstream (test generators, fixture builders) gate id-specific code on
|
|
57
|
-
// this presence so that public read-only APIs without ids don't get
|
|
58
|
-
// bogus id assertions.
|
|
59
|
-
// COMPOSITE FIRST, because a compound key need not come with an `id`.
|
|
60
|
-
//
|
|
61
|
-
// An entity addressed by `{owner}/{repo}` whose response carries only
|
|
62
|
-
// `owner` and `name` has no field literally named `id`, and its adjacent
|
|
63
|
-
// placeholders are left unrenamed so `addressedById` is false too.
|
|
64
|
-
// Neither branch below then ran, so the entity got NO id descriptor and
|
|
65
|
-
// even an explicit `guide.entity.<name>.id.parts` was silently ignored —
|
|
66
|
-
// while the Go port, which initialises a descriptor unconditionally,
|
|
67
|
-
// emitted the composite. The ports disagreed on exactly the shape this
|
|
68
|
-
// feature exists for.
|
|
69
38
|
const gent = guide?.entity?.[ment.name];
|
|
70
39
|
const composite = compositeId(ment, gent, def);
|
|
71
40
|
const idField = fields.find((f) => 'id' === f.name);
|
|
72
|
-
// A COMPOSITE ID IS A STRING, whatever the API's own `id` field is —
|
|
73
|
-
// AND THE API'S OWN id IS KEPT.
|
|
74
|
-
//
|
|
75
|
-
// github's repo declares `id` as an integer, its global database id,
|
|
76
|
-
// while the composite identity is `owner/repo`. Two facts have to
|
|
77
|
-
// survive: `id` must hold a string, because that is what the joined
|
|
78
|
-
// value is and what every generated type has to store; and the spec's
|
|
79
|
-
// numeric property must not be silently reinterpreted, because a
|
|
80
|
-
// consumer that wants the database id is entitled to it with its own
|
|
81
|
-
// type and format intact.
|
|
82
|
-
//
|
|
83
|
-
// So the API's field MOVES to `<api>_id` rather than being rewritten in
|
|
84
|
-
// place, carrying its type, format and per-op overrides with it, and the
|
|
85
|
-
// entity's `alias.field` map records where it went. Retyping in place
|
|
86
|
-
// (the first attempt) claimed the server's numeric id was a string;
|
|
87
|
-
// leaving it alone made `id.field` name a declaration the runtime value
|
|
88
|
-
// cannot satisfy. Moving it is the only option that lies about neither.
|
|
89
41
|
if (null != composite.parts && null != idField && !scalarStringField(idField)) {
|
|
90
42
|
const idf = idField;
|
|
91
43
|
const apiname = String(model?.name || 'api');
|
|
@@ -140,38 +92,12 @@ const fieldTransform = async function (ctx) {
|
|
|
140
92
|
ment.id = { name: 'id', field: 'id', ...composite };
|
|
141
93
|
}
|
|
142
94
|
else if (addressedById(ment)) {
|
|
143
|
-
// The FIELD as well as the descriptor. An entity addressed by id has an
|
|
144
|
-
// id at runtime — the test fixture seeds one, and the SDK sends it — so
|
|
145
|
-
// a model that declares the descriptor without the field makes the
|
|
146
|
-
// generated TYPE disagree with the generated TEST: trello's Option,
|
|
147
|
-
// Reaction and Sticker compiled to `TS2339: Property 'id' does not
|
|
148
|
-
// exist` the moment the test started assigning data.id.
|
|
149
95
|
fields.push({
|
|
150
96
|
name: 'id',
|
|
151
97
|
type: '`$STRING`',
|
|
152
98
|
req: false,
|
|
153
99
|
});
|
|
154
100
|
fields.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
155
|
-
// ADDRESSABLE BY ID WITHOUT DECLARING ONE AS A FIELD.
|
|
156
|
-
//
|
|
157
|
-
// The rule above reads the RESPONSE schema, and plenty of real entities
|
|
158
|
-
// are addressed by an id their response never repeats. github's
|
|
159
|
-
// private_registry is one: PATCH /orgs/{org}/private-registries/{secret_name}
|
|
160
|
-
// renames secret_name to id, so the entity is addressed by id on every
|
|
161
|
-
// one of its own routes, while its schema declares only created_at, key,
|
|
162
|
-
// name, url and friends.
|
|
163
|
-
//
|
|
164
|
-
// Downstream that absence is not cosmetic. TestEntity gates
|
|
165
|
-
// `data.id = <created>.id` on THIS descriptor, so the generated update
|
|
166
|
-
// carried no id at all, the test mock's selector fell back to whatever
|
|
167
|
-
// else was in reqdata (org_id), matched no single record, and the flow
|
|
168
|
-
// failed with a 404 that named nothing to do with ids.
|
|
169
|
-
//
|
|
170
|
-
// An entity whose own points take an `id` param IS addressable by id;
|
|
171
|
-
// that is the property the downstream generators actually want. Entities
|
|
172
|
-
// with neither a field nor an id param — the read-only public APIs the
|
|
173
|
-
// rule above was written for — still get no descriptor, so they still
|
|
174
|
-
// get no id assertions.
|
|
175
101
|
ment.id = { name: 'id', field: 'id', ...composite };
|
|
176
102
|
}
|
|
177
103
|
msg += ment.name + ' ';
|
|
@@ -179,14 +105,6 @@ const fieldTransform = async function (ctx) {
|
|
|
179
105
|
return { ok: true, msg };
|
|
180
106
|
};
|
|
181
107
|
exports.fieldTransform = fieldTransform;
|
|
182
|
-
// The separator that joins a composite id into one string.
|
|
183
|
-
//
|
|
184
|
-
// A forward slash cannot occur inside a single path segment — a raw `/`
|
|
185
|
-
// would end the segment, and a value that legitimately contains one arrives
|
|
186
|
-
// percent-encoded as `%2F` — so joining on it can never be ambiguous, and
|
|
187
|
-
// splitting on it can never over-split. That is what makes the composite id
|
|
188
|
-
// safe to carry as a single opaque string, which is the property the SDK and
|
|
189
|
-
// Seneca entities are built on.
|
|
190
108
|
const ID_SEP = '/';
|
|
191
109
|
// Subfields that conventionally carry the identifying value of a nested
|
|
192
110
|
// object, in preference order. github's repo `owner` is a user object whose
|
|
@@ -197,36 +115,6 @@ const NESTED_ID_KEYS = ['login', 'slug', 'name', 'key', 'id'];
|
|
|
197
115
|
// The ops that address ONE record, most authoritative first. Only a
|
|
198
116
|
// tie-break: identityParams compares candidates from all of them.
|
|
199
117
|
const ID_OPS = ['load', 'update', 'patch', 'remove'];
|
|
200
|
-
// The parameters that TOGETHER name one record: the trailing run of
|
|
201
|
-
// ADJACENT variable segments on the addressing route.
|
|
202
|
-
//
|
|
203
|
-
// ADJACENCY IS THE WHOLE TEST, and it is what separates a compound key from
|
|
204
|
-
// ordinary parent/child nesting:
|
|
205
|
-
//
|
|
206
|
-
// /repos/{owner}/{repo} -> owner, repo COMPOSITE
|
|
207
|
-
// /api/planet/{planet_id}/moon/{moon_id} -> moon_id single
|
|
208
|
-
// /repos/{owner}/{repo}/pulls/{pull_number} -> pull_number single
|
|
209
|
-
//
|
|
210
|
-
// A literal segment between two variables names a SUB-COLLECTION, so the
|
|
211
|
-
// earlier variable scopes the later one — `planet_id` says which planet's
|
|
212
|
-
// moons, and `moon_id` alone identifies the moon. Two variables with nothing
|
|
213
|
-
// between them address no sub-collection: neither value names anything on
|
|
214
|
-
// its own, and only the pair identifies a repository.
|
|
215
|
-
//
|
|
216
|
-
// Taking every variable on the path instead was tried first and is wrong on
|
|
217
|
-
// most real specs — it made `moon` (planet_id + moon_id), petstore's `order`,
|
|
218
|
-
// `pet` and `user`, and taxonomy's `domain` and `kingdom` all falsely
|
|
219
|
-
// composite, which the apidef-validate goldens caught immediately. Nested
|
|
220
|
-
// resources are the common shape; compound keys are the exception, and
|
|
221
|
-
// adjacency is the thing that actually distinguishes them.
|
|
222
|
-
//
|
|
223
|
-
// Read from the op that names a single record, never from `list`: a
|
|
224
|
-
// collection route's path params are the entity's parents. A point ending in
|
|
225
|
-
// a literal is a verb ON the record (`.../{number}/merge`) and carries the
|
|
226
|
-
// same variables, so it is a fallback rather than a different answer.
|
|
227
|
-
// Walk back from a point's end, collecting variables until a literal stops
|
|
228
|
-
// the run. That literal is the sub-collection boundary; anything before it
|
|
229
|
-
// scopes this record rather than naming it.
|
|
230
118
|
function trailingVars(point) {
|
|
231
119
|
const segs = (point?.segments || []).filter((s) => null != s);
|
|
232
120
|
const run = [];
|
|
@@ -239,15 +127,6 @@ function trailingVars(point) {
|
|
|
239
127
|
return run;
|
|
240
128
|
}
|
|
241
129
|
function identityParams(ment) {
|
|
242
|
-
// EVERY ID-BEARING OP AT ONCE, not the first one that offers a candidate.
|
|
243
|
-
//
|
|
244
|
-
// These four ops all address a single record, so all four describe the
|
|
245
|
-
// same identity — but they do not all carry the same routes. gitlab's
|
|
246
|
-
// `project` has `/api/v4/projects/{id}` under `remove` alone, while its
|
|
247
|
-
// `load` carries only sub-resources like
|
|
248
|
-
// `/api/v4/projects/{id}/uploads/{secret}/{filename}`. Returning on the
|
|
249
|
-
// first op with any candidate therefore made a PROJECT identified by
|
|
250
|
-
// `secret/filename`. The op order is now only a tie-break.
|
|
251
130
|
const cands = [];
|
|
252
131
|
for (let o = 0; o < ID_OPS.length; o++) {
|
|
253
132
|
const mop = ment.op?.[ID_OPS[o]];
|
|
@@ -267,56 +146,12 @@ function identityParams(ment) {
|
|
|
267
146
|
run,
|
|
268
147
|
// Segments BEFORE the run: how much parent scope the route needs.
|
|
269
148
|
scope: ((pt.segments || []).length - run.length),
|
|
270
|
-
// DOES THE RUN END IN THE RECORD'S OWN KEY? Then it is the
|
|
271
|
-
// record's address and nothing further is needed.
|
|
272
|
-
//
|
|
273
|
-
// This transform RENAMES that parameter to `id`, so a run ending in
|
|
274
|
-
// it is this port's own statement of what identifies the record —
|
|
275
|
-
// and the composite inference must not contradict it.
|
|
276
|
-
// `/gists/{gist_id}` becomes `/gists/{id}` and is a gist;
|
|
277
|
-
// `/gists/{gist_id}/{sha}` is a REVISION of one, and won on key
|
|
278
|
-
// length alone, so a gist came out keyed `gist_id/sha` while the
|
|
279
|
-
// generated SDK's own load match takes the single parameter. The
|
|
280
|
-
// same contradiction gave cloudsmith's repo and vulnerability
|
|
281
|
-
// compound keys their SDKs never address them by.
|
|
282
|
-
//
|
|
283
|
-
// Deliberately narrow: exactly `id` or an unrenamed `<entity>_id`,
|
|
284
|
-
// never any `*_id`. `actor_type/actor_id` IS a compound key, and a
|
|
285
|
-
// looser test breaks it.
|
|
286
149
|
own: 'id' === run[run.length - 1] ||
|
|
287
150
|
ment.name + '_id' === run[run.length - 1],
|
|
288
151
|
order: o,
|
|
289
152
|
});
|
|
290
153
|
}
|
|
291
154
|
}
|
|
292
|
-
// WHICH ROUTE IS THE RECORD'S OWN ADDRESS.
|
|
293
|
-
//
|
|
294
|
-
// An entity gathers every route that reads it, and in a large
|
|
295
|
-
// specification most of those are sub-resources. Three earlier rules were
|
|
296
|
-
// measured against the validation corpus, and each is wrong:
|
|
297
|
-
//
|
|
298
|
-
// The FIRST route listed gave github's `repo` the single part
|
|
299
|
-
// `subject_digest`, from
|
|
300
|
-
// `/repos/{owner}/{repo}/attestations/{subject_digest}` — no compound
|
|
301
|
-
// key at all, for the entity this feature exists for. Invisible on a
|
|
302
|
-
// small spec, where the first item route IS the record's own.
|
|
303
|
-
//
|
|
304
|
-
// The SHORTEST route ending in a variable took cloudsmith's
|
|
305
|
-
// `/vulnerabilities/{owner}/` — a LIST of an owner's vulnerabilities —
|
|
306
|
-
// and cut a four-part key down to `owner`, dropping three more
|
|
307
|
-
// composites. Ending in a variable does not make a route an address.
|
|
308
|
-
//
|
|
309
|
-
// The LONGEST trailing run took
|
|
310
|
-
// `/orgs/{org}/teams/{team_slug}/repos/{owner}/{repo}` and made a TEAM
|
|
311
|
-
// identified by `owner/repo`. A deep sub-resource can carry more
|
|
312
|
-
// adjacent variables than the record's own route does.
|
|
313
|
-
//
|
|
314
|
-
// What separates them is PARENT SCOPE: the record's own route is the
|
|
315
|
-
// least-qualified one that names it, and among equally-qualified routes
|
|
316
|
-
// the one carrying the fullest key. `/repos/{owner}/{repo}` is qualified
|
|
317
|
-
// by one segment and the attestations route by four; `/teams/{team_id}`
|
|
318
|
-
// by one and the org-team-repo route by five; cloudsmith's vulnerability
|
|
319
|
-
// routes are all qualified by one, so the fullest of them wins.
|
|
320
155
|
const best = cands.reduce((b, c) => {
|
|
321
156
|
if (null == b) {
|
|
322
157
|
return c;
|
|
@@ -334,34 +169,6 @@ function identityParams(ment) {
|
|
|
334
169
|
}, null);
|
|
335
170
|
return null == best ? [] : best.run;
|
|
336
171
|
}
|
|
337
|
-
// THE PROPERTY MAPS A RESPONSE COULD BE DESCRIBING, best first.
|
|
338
|
-
//
|
|
339
|
-
// BOTH SPEC DIALECTS. An OpenAPI 3 response carries its schema under
|
|
340
|
-
// `content['application/json']`; a SWAGGER 2 response carries it directly as
|
|
341
|
-
// `schema`. Reading only the first resolved nothing for every Swagger 2 spec
|
|
342
|
-
// in the validation corpus.
|
|
343
|
-
//
|
|
344
|
-
// JSON ONLY, where there is a choice. An operation may declare several media
|
|
345
|
-
// types with different schemas, and field extraction uses the JSON one — so
|
|
346
|
-
// picking whichever came first in source order could infer a path from an XML
|
|
347
|
-
// or binary schema that the actual JSON record does not have.
|
|
348
|
-
//
|
|
349
|
-
// `allOf` IS EXPANDED, because a response that composes its entity that way
|
|
350
|
-
// has neither `properties` nor `items` of its own. field extraction expands
|
|
351
|
-
// it; not doing so here meant the fields were present while the id could not
|
|
352
|
-
// be reconstructed.
|
|
353
|
-
//
|
|
354
|
-
// ONLY THE ENVELOPE IS DESCENDED, via the same `envelopeProp` rule field
|
|
355
|
-
// extraction uses. Descending every object-valued property instead treats an
|
|
356
|
-
// ordinary nested object as a whole record: for `{ slug, metadata: { tenant } }`
|
|
357
|
-
// addressed by `{tenant}/{slug}`, `tenant` resolved to `tenant` rather than
|
|
358
|
-
// `metadata.tenant` — a confidently wrong path, which is worse than no
|
|
359
|
-
// mapping at all.
|
|
360
|
-
//
|
|
361
|
-
// ACTION POINTS ARE SKIPPED, as `identityParams` skips them: an action's
|
|
362
|
-
// response is a verb's result, not a representation of the entity, so a field
|
|
363
|
-
// that happens to appear there says nothing about what a returned record
|
|
364
|
-
// carries.
|
|
365
172
|
function responseCandidates(ment, def) {
|
|
366
173
|
const out = [];
|
|
367
174
|
const seen = new Set();
|
|
@@ -429,13 +236,20 @@ function responseCandidates(ment, def) {
|
|
|
429
236
|
add(content[ctype]?.schema, opname);
|
|
430
237
|
}
|
|
431
238
|
}
|
|
432
|
-
// Swagger 2 puts it here.
|
|
433
239
|
add(resdef.schema, opname);
|
|
434
240
|
}
|
|
435
241
|
}
|
|
436
242
|
}
|
|
437
243
|
return out;
|
|
438
244
|
}
|
|
245
|
+
function namesEntity(schema, ment) {
|
|
246
|
+
const xref = schema?.['x-ref'];
|
|
247
|
+
if ('string' !== typeof xref) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
const cmp = xref.slice(xref.lastIndexOf('/') + 1);
|
|
251
|
+
return (0, utility_1.canonizeCmpName)(cmp) === ment.name;
|
|
252
|
+
}
|
|
439
253
|
// A `$ref` followed one hop, or the schema itself. apidef resolves most refs
|
|
440
254
|
// before this stage; this covers the ones that survive on a nested property.
|
|
441
255
|
function resolveRef(schema, def) {
|
|
@@ -490,12 +304,6 @@ function partAliases(ment, part) {
|
|
|
490
304
|
});
|
|
491
305
|
return [...names];
|
|
492
306
|
}
|
|
493
|
-
// Where one part is carried in a given property map, or null.
|
|
494
|
-
//
|
|
495
|
-
// The four rules, in order, each a fact the spec states: a scalar property of
|
|
496
|
-
// that name; the part naming this entity, resolved to `name`; a scalar
|
|
497
|
-
// `<part>_name` / `_login` / `_slug`; or an object property's conventional
|
|
498
|
-
// identifying subfield.
|
|
499
307
|
function resolvePart(ment, part, aliases, props, def) {
|
|
500
308
|
if (null == props) {
|
|
501
309
|
return null;
|
|
@@ -529,47 +337,10 @@ function resolvePart(ment, part, aliases, props, def) {
|
|
|
529
337
|
}
|
|
530
338
|
return null;
|
|
531
339
|
}
|
|
532
|
-
// WHERE EACH COMPOSITE PART'S VALUE LIVES IN A RESPONSE.
|
|
533
|
-
//
|
|
534
|
-
// The parts are PATH PARAMETER names; a response names its fields whatever it
|
|
535
|
-
// likes. Resolving one to the other is what lets an SDK put an id on a record
|
|
536
|
-
// the API returned, rather than only address a record whose id it was given.
|
|
537
|
-
//
|
|
538
|
-
// The rules, in order, and each of them is a fact about the spec rather than
|
|
539
|
-
// a guess:
|
|
540
|
-
//
|
|
541
|
-
// 1. a scalar field of exactly that name -> itself
|
|
542
|
-
// 2. the part names this entity, and there is a `name` -> `name`
|
|
543
|
-
// (`/repos/{owner}/{repo}` on entity `repo`, whose response calls the
|
|
544
|
-
// repository `name`)
|
|
545
|
-
// 3. a scalar `<part>_name` / `<part>_login` / `<part>_slug`
|
|
546
|
-
// 4. an OBJECT field of that name -> `<part>.<conventional key>`
|
|
547
|
-
// (`owner` is a user object; the value is `owner.login`)
|
|
548
|
-
//
|
|
549
|
-
// A part none of these resolve is left OUT. Downstream then knows the id
|
|
550
|
-
// cannot be rebuilt for that entity and can say so, which is better than a
|
|
551
|
-
// confidently wrong id on a real record. guide.aon can state it instead.
|
|
552
340
|
function identityFrom(ment, parts, def) {
|
|
553
|
-
// THE RESPONSE SCHEMA IS THE AUTHORITY, not `ment.fields`.
|
|
554
|
-
//
|
|
555
|
-
// `ment.fields` is merged across load, create, update and list, so a part
|
|
556
|
-
// that exists only in a REQUEST BODY appears there too. Resolving against
|
|
557
|
-
// it recorded such a part in `from` as though a returned record carried it,
|
|
558
|
-
// and a consumer then rebuilt an id from a property the response never
|
|
559
|
-
// sends — worse than leaving the part unresolved, which at least says so.
|
|
560
|
-
//
|
|
561
|
-
// Candidate property maps, in order: the response's own properties, then
|
|
562
|
-
// one level into an envelope. A response that wraps the record
|
|
563
|
-
// (`{ item: {...} }`, `{ data: [ {...} ] }`) states the record's fields one
|
|
564
|
-
// level in, and searching only the wrapper found nothing.
|
|
565
341
|
const candidates = responseCandidates(ment, def);
|
|
566
342
|
const out = {};
|
|
567
343
|
for (const part of parts) {
|
|
568
|
-
// THE WIRE NAME AS WELL AS THE MODEL NAME. `identityParams` reads the
|
|
569
|
-
// RENAMED parameter off the path segments, while a response keeps its own
|
|
570
|
-
// casing — so a `tenantKey` renamed to `tenant_key` was looked up under a
|
|
571
|
-
// name the response does not use, and the mapping was dropped for every
|
|
572
|
-
// camel-cased or depluralized parameter.
|
|
573
344
|
const aliases = partAliases(ment, part);
|
|
574
345
|
let found = null;
|
|
575
346
|
for (const props of candidates) {
|
|
@@ -589,15 +360,6 @@ function identityFrom(ment, parts, def) {
|
|
|
589
360
|
function scalarStringField(f) {
|
|
590
361
|
return String(f?.type || '').toUpperCase().includes('STRING');
|
|
591
362
|
}
|
|
592
|
-
// WHICH PARAMETER IS THE RECORD'S OWN KEY, among several that looked
|
|
593
|
-
// adjacent. The same shape apidef's id handling recognises everywhere else:
|
|
594
|
-
//
|
|
595
|
-
// 1. one named exactly `id`
|
|
596
|
-
// 2. `<entity>_id` — the entity's own id, however the path spells it
|
|
597
|
-
// 3. any `*_id` — an id by name
|
|
598
|
-
// 4. failing all that, the terminal parameter
|
|
599
|
-
//
|
|
600
|
-
// Position is the LAST resort, not the first.
|
|
601
363
|
function singleKeyOf(ment, parts) {
|
|
602
364
|
if (0 === parts.length) {
|
|
603
365
|
return undefined;
|
|
@@ -607,12 +369,6 @@ function singleKeyOf(ment, parts) {
|
|
|
607
369
|
?? parts.find((p) => p.endsWith('_id'))
|
|
608
370
|
?? parts[parts.length - 1];
|
|
609
371
|
}
|
|
610
|
-
// The composite half of the id descriptor, or `{}` for the ordinary case.
|
|
611
|
-
//
|
|
612
|
-
// Emitted ONLY for a genuinely composite id (two or more addressing
|
|
613
|
-
// parameters). A single-parameter entity already round-trips through one
|
|
614
|
-
// `id` and gains nothing from carrying a one-element `parts`, so its
|
|
615
|
-
// descriptor is left exactly as it was — no existing model output moves.
|
|
616
372
|
function compositeId(ment, gent, def) {
|
|
617
373
|
const gid = gent?.id;
|
|
618
374
|
const sep = null != gid?.sep && '' !== String(gid.sep) ? String(gid.sep) : ID_SEP;
|
|
@@ -620,17 +376,6 @@ function compositeId(ment, gent, def) {
|
|
|
620
376
|
// empty `parts`, because aontu resolves an empty list to nothing and the
|
|
621
377
|
// key would arrive absent — indistinguishable from never having been set.
|
|
622
378
|
if (null != gid && false === gid.composite) {
|
|
623
|
-
// DISABLING COMPOSITE MUST NOT DISABLE THE ID. The correction says these
|
|
624
|
-
// adjacent parameters are not a compound key; it does not say the record
|
|
625
|
-
// has no key. Returning a bare `{}` left an entity whose response has no
|
|
626
|
-
// literal `id` with no descriptor at all — the false positive removed and
|
|
627
|
-
// nothing identifying the real key.
|
|
628
|
-
//
|
|
629
|
-
// WHICH of the adjacent parameters is that key is decided by the same
|
|
630
|
-
// id-finding rules apidef uses elsewhere, not by position. Taking the
|
|
631
|
-
// terminal one picked `archive_format` for
|
|
632
|
-
// `/artifacts/{artifact_id}/{archive_format}` — the modifier, precisely
|
|
633
|
-
// the false positive the correction exists to undo.
|
|
634
379
|
return { single: singleKeyOf(ment, identityParams(ment)) };
|
|
635
380
|
}
|
|
636
381
|
// `from` STATED IN guide.aon WINS PER PART, so a spec can correct one
|
|
@@ -690,19 +435,6 @@ function resolveOpFields(ment, mop, mpoint, def) {
|
|
|
690
435
|
req: !!fielddef.required,
|
|
691
436
|
op: {},
|
|
692
437
|
};
|
|
693
|
-
// Carry the spec's own words for the field, when it has any.
|
|
694
|
-
//
|
|
695
|
-
// Every generated per-entity table has a Description column and every cell
|
|
696
|
-
// was blank, because nothing ever read the property `description` the spec
|
|
697
|
-
// supplies. Trimmed, and only when it is a non-empty string: a whitespace
|
|
698
|
-
// or non-string value would put a meaningless cell where an empty one is
|
|
699
|
-
// honest.
|
|
700
|
-
// ONE LINE, not the whole description. Every generated Readme drops this
|
|
701
|
-
// straight into a markdown table cell, where a raw newline ends the row
|
|
702
|
-
// and orphans the rest of the table — and specs put bullet lists, fenced
|
|
703
|
-
// examples and multi-paragraph notes in `description`. firstSentence is
|
|
704
|
-
// the same reduction the API summary uses, so `short` means the same
|
|
705
|
-
// thing wherever it appears.
|
|
706
438
|
const fdesc = fielddef.description;
|
|
707
439
|
if ('string' === typeof fdesc && '' !== fdesc.trim()) {
|
|
708
440
|
const short = (0, utility_1.firstSentence)(fdesc);
|
|
@@ -710,20 +442,6 @@ function resolveOpFields(ment, mop, mpoint, def) {
|
|
|
710
442
|
mfield.short = short;
|
|
711
443
|
}
|
|
712
444
|
}
|
|
713
|
-
// SPEC FACTS ABOUT THE FIELD, carried through verbatim.
|
|
714
|
-
//
|
|
715
|
-
// These four are declared by OpenAPI on the property and were being
|
|
716
|
-
// dropped on the floor. `readOnly` is the one that matters most: it is
|
|
717
|
-
// the difference between a field a client MAY send and one it may not,
|
|
718
|
-
// and nothing else in the model says which — so every generator has been
|
|
719
|
-
// putting server-assigned fields into the type a caller fills in.
|
|
720
|
-
//
|
|
721
|
-
// ONLY WHEN THE SPEC SAYS SO, and for the booleans only when TRUE. Each
|
|
722
|
-
// defaults to false in OpenAPI, so an absent key and an explicit `false`
|
|
723
|
-
// carry the same information; emitting the false ones would add a key to
|
|
724
|
-
// every field of every model and say nothing. Same discipline as
|
|
725
|
-
// `short`: absent means "the spec did not say", never "apidef dropped
|
|
726
|
-
// it".
|
|
727
445
|
for (const flag of ['readOnly', 'writeOnly', 'deprecated']) {
|
|
728
446
|
if (true === fielddef[flag]) {
|
|
729
447
|
mfield[flag] = true;
|
|
@@ -761,7 +479,6 @@ function findGraphqlFieldDefs(ment, mpoint, def) {
|
|
|
761
479
|
return [];
|
|
762
480
|
}
|
|
763
481
|
const out = [];
|
|
764
|
-
// Sorted by construction in parse/graphql.ts, so output stays byte-stable.
|
|
765
482
|
for (const fname of Object.keys(gtype.fields)) {
|
|
766
483
|
const f = gtype.fields[fname];
|
|
767
484
|
if (f.deprecated) {
|
|
@@ -780,17 +497,10 @@ function findGraphqlFieldDefs(ment, mpoint, def) {
|
|
|
780
497
|
// custom scalars left unconstrained.
|
|
781
498
|
type: 'ENUM' === kind ? 'string' : gqlFieldType(f.type),
|
|
782
499
|
required: f.reqd,
|
|
783
|
-
// GraphQL puts the field's own words on GqlField.desc (see
|
|
784
|
-
// parse/graphql.ts). resolveOpFields reads `description`, the OpenAPI
|
|
785
|
-
// spelling, so name it that here rather than teaching the reader two.
|
|
786
500
|
description: f.desc,
|
|
787
501
|
});
|
|
788
502
|
}
|
|
789
503
|
else if (('OBJECT' === kind || 'INTERFACE' === kind) && !f.list) {
|
|
790
|
-
// To-one relation. The default fragment selects `team { id }`, so the
|
|
791
|
-
// response carries a nested stub object — declare it as such. Naming a
|
|
792
|
-
// flat `team_id` here would advertise a field the wire never returns,
|
|
793
|
-
// since nothing flattens the response.
|
|
794
504
|
const idField = ftype.fields?.id;
|
|
795
505
|
if (null != idField) {
|
|
796
506
|
out.push({
|
|
@@ -804,12 +514,6 @@ function findGraphqlFieldDefs(ment, mpoint, def) {
|
|
|
804
514
|
}
|
|
805
515
|
return out;
|
|
806
516
|
}
|
|
807
|
-
// GraphQL named type -> the type names the field typing understands.
|
|
808
|
-
//
|
|
809
|
-
// Built-ins only: a custom scalar (JSON, JSONObject, Upload, ...) can hold
|
|
810
|
-
// any JSON value, so advertising it as a string would misdescribe the data
|
|
811
|
-
// and make generated validation reject values the schema accepts. Enums are
|
|
812
|
-
// mapped by the caller, which knows they are strings.
|
|
813
517
|
function gqlFieldType(typeName) {
|
|
814
518
|
return 'Int' === typeName ? 'integer' :
|
|
815
519
|
'Float' === typeName ? 'number' :
|
|
@@ -817,14 +521,16 @@ function gqlFieldType(typeName) {
|
|
|
817
521
|
('String' === typeName || 'ID' === typeName) ? 'string' :
|
|
818
522
|
undefined;
|
|
819
523
|
}
|
|
820
|
-
function findFieldDefs(
|
|
524
|
+
function findFieldDefs(ment, mop, mpoint, def) {
|
|
821
525
|
if ('graphql' === mpoint.kind) {
|
|
822
|
-
return findGraphqlFieldDefs(
|
|
526
|
+
return findGraphqlFieldDefs(ment, mpoint, def);
|
|
823
527
|
}
|
|
528
|
+
// A verb, rather than an address: see the call site in the transform.
|
|
529
|
+
const isAction = null != mpoint?.select?.['$action'];
|
|
824
530
|
const fielddefs = [];
|
|
825
531
|
const pathdef = def.paths[mpoint.orig];
|
|
826
532
|
const method = mpoint.method.toLowerCase();
|
|
827
|
-
const opdef = pathdef[method];
|
|
533
|
+
const opdef = pathdef?.[method];
|
|
828
534
|
if (opdef) {
|
|
829
535
|
const responses = opdef.responses;
|
|
830
536
|
const requestBody = opdef.requestBody;
|
|
@@ -833,13 +539,6 @@ function findFieldDefs(_ment, mop, mpoint, def) {
|
|
|
833
539
|
fieldSets = (0, jostraca_1.getx)(responses, '200 content "application/json" schema') ??
|
|
834
540
|
(0, jostraca_1.getx)(responses, '200 schema');
|
|
835
541
|
if ('list' == mop.name) {
|
|
836
|
-
// List responses commonly come in three shapes:
|
|
837
|
-
// 1. direct array — { type: array, items: { ...item } }
|
|
838
|
-
// 2. wrapper object — { properties: { items: [Item], page, ... } }
|
|
839
|
-
// (a single array-of-object property inside an object schema)
|
|
840
|
-
// 3. legacy "list of created items" under 201
|
|
841
|
-
// Resolve to the inner item schema when we can identify one
|
|
842
|
-
// unambiguously; otherwise fall through to the 200 schema as-is.
|
|
843
542
|
const unwrapped = unwrapArrayWrapper(fieldSets);
|
|
844
543
|
if (unwrapped) {
|
|
845
544
|
fieldSets = unwrapped;
|
|
@@ -855,14 +554,6 @@ function findFieldDefs(_ment, mop, mpoint, def) {
|
|
|
855
554
|
fieldSets = (0, jostraca_1.getx)(responses, '201 content "application/json" schema') ??
|
|
856
555
|
(0, jostraca_1.getx)(responses, '201 schema');
|
|
857
556
|
}
|
|
858
|
-
// Single-entity responses get the same treatment the list branch above
|
|
859
|
-
// already gives collections: a body that is only an envelope around the
|
|
860
|
-
// entity — `{item: {...}}` — describes the WRAPPER, not the entity, so
|
|
861
|
-
// its sole property would otherwise be harvested as a field. That is
|
|
862
|
-
// how an entity `todoitem` ended up with a required `item` field of
|
|
863
|
-
// type object, which then appeared in the generated create/update data
|
|
864
|
-
// types. envelopeProp applies the same two rules used to pick the
|
|
865
|
-
// response transform, so the field list and the transform agree.
|
|
866
557
|
if ('list' != mop.name) {
|
|
867
558
|
const envelope = (0, utility_1.envelopeProp)(fieldSets?.properties, mop.name);
|
|
868
559
|
if (null != envelope) {
|
|
@@ -870,11 +561,15 @@ function findFieldDefs(_ment, mop, mpoint, def) {
|
|
|
870
561
|
}
|
|
871
562
|
}
|
|
872
563
|
}
|
|
564
|
+
if (isAction && !namesEntity(fieldSets, ment)) {
|
|
565
|
+
return fielddefs;
|
|
566
|
+
}
|
|
873
567
|
// A QUERY (RFC 10008) request body is a filter/query schema, not the
|
|
874
568
|
// entity shape, so it must not contribute entity fields. Fields for a
|
|
875
569
|
// QUERY op come from its response only. Other methods (POST/PUT/PATCH)
|
|
876
|
-
// carry the entity in the body, so merge as usual
|
|
877
|
-
|
|
570
|
+
// carry the entity in the body, so merge as usual -- except for an
|
|
571
|
+
// action, whose body is the verb's arguments and never the record.
|
|
572
|
+
if (requestBody && 'query' !== method && !isAction) {
|
|
878
573
|
fieldSets = [
|
|
879
574
|
fieldSets,
|
|
880
575
|
(0, jostraca_1.getx)(requestBody, 'content "application/json" schema') ??
|
|
@@ -962,7 +657,6 @@ function findExampleObject(opdef) {
|
|
|
962
657
|
example = (0, jostraca_1.getx)(resdef, 'examples "application/json"');
|
|
963
658
|
if (null != example && 'object' === typeof example)
|
|
964
659
|
return unwrapExample(example);
|
|
965
|
-
// Swagger 2.0: schema.example
|
|
966
660
|
example = (0, jostraca_1.getx)(resdef, 'schema example');
|
|
967
661
|
if (null != example && 'object' === typeof example)
|
|
968
662
|
return unwrapExample(example);
|
|
@@ -975,16 +669,6 @@ function unwrapExample(example) {
|
|
|
975
669
|
}
|
|
976
670
|
return example;
|
|
977
671
|
}
|
|
978
|
-
// unwrapArrayWrapper inspects a list-response schema and, when it is an
|
|
979
|
-
// object with a single array-of-object-schema property (e.g.
|
|
980
|
-
// { boards: [Board] }, { items: [Foo], page, total, ... }), returns the
|
|
981
|
-
// inner item schema so that field resolution sees the actual entity
|
|
982
|
-
// properties rather than the wrapper's bookkeeping.
|
|
983
|
-
//
|
|
984
|
-
// Returns null if the input is not unambiguously such a wrapper:
|
|
985
|
-
// - schema is already an array → return null (let caller use it directly)
|
|
986
|
-
// - no array-of-object-schema property → return null
|
|
987
|
-
// - more than one array-of-object-schema property → ambiguous, return null
|
|
988
672
|
function unwrapArrayWrapper(schema) {
|
|
989
673
|
if (null == schema || 'object' !== typeof schema)
|
|
990
674
|
return null;
|
|
@@ -1039,26 +723,9 @@ function mergeField(mop, existingField, newField) {
|
|
|
1039
723
|
type: newField.type,
|
|
1040
724
|
};
|
|
1041
725
|
}
|
|
1042
|
-
// Field identity is first-writer-wins, but a DESCRIPTION is not part of
|
|
1043
|
-
// identity: the op that first names a field is often not the one that
|
|
1044
|
-
// documents it (a load response referencing a bare component, a create body
|
|
1045
|
-
// referencing the annotated one). Take the first non-empty description in
|
|
1046
|
-
// opFieldPrecedence order and keep it — dropping it left a blank cell in
|
|
1047
|
-
// every generated table while the spec had the words all along.
|
|
1048
726
|
if (null == existingField.short && null != newField.short) {
|
|
1049
727
|
existingField.short = newField.short;
|
|
1050
728
|
}
|
|
1051
|
-
// The spec facts merge the same way, and for the same reason: one schema
|
|
1052
|
-
// annotates the field and another references it bare, so taking the first
|
|
1053
|
-
// declaration in opFieldPrecedence order is what finds the annotation.
|
|
1054
|
-
//
|
|
1055
|
-
// THE PRECEDENCE ORDER PUTS `load` FIRST, WHICH IS THE SAFE DIRECTION HERE.
|
|
1056
|
-
// A field the response schema marks readOnly and a request body also lists
|
|
1057
|
-
// is a self-contradictory spec — OpenAPI says a client must not send a
|
|
1058
|
-
// readOnly property at all — and this resolves it by believing the
|
|
1059
|
-
// restriction rather than the omission. Marking a writable field readOnly
|
|
1060
|
-
// costs a caller one field; the other way round sends a value the server
|
|
1061
|
-
// rejects.
|
|
1062
729
|
for (const flag of ['readOnly', 'writeOnly', 'deprecated', 'format']) {
|
|
1063
730
|
if (null == existingField[flag] && null != newField[flag]) {
|
|
1064
731
|
existingField[flag] = newField[flag];
|