forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.ts
618 lines (559 loc) · 18.6 KB
/
metadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import {
compilerAssert,
DiagnosticCollector,
getEffectiveModelType,
getParameterVisibility,
isVisible as isVisibleCore,
Model,
ModelProperty,
Operation,
Program,
Queue,
TwoLevelMap,
Type,
Union,
walkPropertiesInherited,
} from "@typespec/compiler";
import {
includeInapplicableMetadataInPayload,
isBody,
isBodyIgnore,
isBodyRoot,
isHeader,
isPathParam,
isQueryParam,
isStatusCode,
} from "./decorators.js";
import { HttpVerb } from "./types.js";
/**
* Flags enum representation of well-known visibilities that are used in
* REST API.
*/
export enum Visibility {
Read = 1 << 0,
Create = 1 << 1,
Update = 1 << 2,
Delete = 1 << 3,
Query = 1 << 4,
None = 0,
All = Read | Create | Update | Delete | Query,
/**
* Additional flag to indicate when something is nested in a collection
* and therefore no metadata is applicable.
*/
Item = 1 << 20,
/**
* Additional flag to indicate when the verb is path and therefore
* will have fields made optional if request visibility includes update.
*/
Patch = 1 << 21,
}
const visibilityToArrayMap: Map<Visibility, string[]> = new Map();
function visibilityToArray(visibility: Visibility): readonly string[] {
// Item and Patch flags are not real visibilities.
visibility &= ~Visibility.Item;
visibility &= ~Visibility.Patch;
let result = visibilityToArrayMap.get(visibility);
if (!result) {
result = [];
if (visibility & Visibility.Read) {
result.push("read");
}
if (visibility & Visibility.Create) {
result.push("create");
}
if (visibility & Visibility.Update) {
result.push("update");
}
if (visibility & Visibility.Delete) {
result.push("delete");
}
if (visibility & Visibility.Query) {
result.push("query");
}
compilerAssert(result.length > 0 || visibility === Visibility.None, "invalid visibility");
visibilityToArrayMap.set(visibility, result);
}
return result;
}
function arrayToVisibility(array: readonly string[] | undefined): Visibility | undefined {
if (!array) {
return undefined;
}
let value = Visibility.None;
for (const item of array) {
switch (item) {
case "read":
value |= Visibility.Read;
break;
case "create":
value |= Visibility.Create;
break;
case "update":
value |= Visibility.Update;
break;
case "delete":
value |= Visibility.Delete;
break;
case "query":
value |= Visibility.Query;
break;
default:
return undefined;
}
}
return value;
}
/**
* Provides a naming suffix to create a unique name for a type with this
* visibility.
*
* The canonical visibility (default Visibility.Read) gets empty suffix,
* otherwise visibilities are joined in pascal-case with `Or`. And `Item` is
* if `Visibility.Item` is produced.
*
* Examples (with canonicalVisibility = Visibility.Read):
* - Visibility.Read => ""
* - Visibility.Update => "Update"
* - Visibility.Create | Visibility.Update => "CreateOrUpdate"
* - Visibility.Create | Visibility.Item => "CreateItem"
* - Visibility.Create | Visibility.Update | Visibility.Item => "CreateOrUpdateItem"
* */
export function getVisibilitySuffix(
visibility: Visibility,
canonicalVisibility: Visibility | undefined = Visibility.None
) {
let suffix = "";
if ((visibility & ~Visibility.Item & ~Visibility.Patch) !== canonicalVisibility) {
const visibilities = visibilityToArray(visibility);
suffix += visibilities.map((v) => v[0].toUpperCase() + v.slice(1)).join("Or");
}
if (visibility & Visibility.Item) {
suffix += "Item";
}
return suffix;
}
/**
* Determines the visibility to use for a request with the given verb.
*
* - GET | HEAD => Visibility.Query
* - POST => Visibility.Update
* - PUT => Visibility.Create | Update
* - DELETE => Visibility.Delete
*/
function getDefaultVisibilityForVerb(verb: HttpVerb): Visibility {
switch (verb) {
case "get":
case "head":
return Visibility.Query;
case "post":
return Visibility.Create;
case "put":
return Visibility.Create | Visibility.Update;
case "patch":
return Visibility.Update;
case "delete":
return Visibility.Delete;
default:
const _assertNever: never = verb;
compilerAssert(false, "unreachable");
}
}
/**
* Determines the visibility to use for a request with the given verb.
*
* - GET | HEAD => Visibility.Query
* - POST => Visibility.Create
* - PATCH => Visibility.Update
* - PUT => Visibility.Create | Update
* - DELETE => Visibility.Delete
* @param verb The HTTP verb for the operation.
* @deprecated Use `resolveRequestVisibility` instead, or if you only want the default visibility for a verb, `getDefaultVisibilityForVerb`.
* @returns The applicable parameter visibility or visibilities for the request.
*/
export function getRequestVisibility(verb: HttpVerb): Visibility {
let visibility = getDefaultVisibilityForVerb(verb);
// If the verb is PATCH, then we need to add the patch flag to the visibility in order for
// later processes to properly apply it
if (verb === "patch") {
visibility |= Visibility.Patch;
}
return visibility;
}
/**
* Returns the applicable parameter visibility or visibilities for the request if `@requestVisibility` was used.
* Otherwise, returns the default visibility based on the HTTP verb for the operation.
* @param operation The TypeSpec Operation for the request.
* @param verb The HTTP verb for the operation.
* @returns The applicable parameter visibility or visibilities for the request.
*/
export function resolveRequestVisibility(
program: Program,
operation: Operation,
verb: HttpVerb
): Visibility {
const parameterVisibility = arrayToVisibility(getParameterVisibility(program, operation));
const defaultVisibility = getDefaultVisibilityForVerb(verb);
let visibility = parameterVisibility ?? defaultVisibility;
// If the verb is PATCH, then we need to add the patch flag to the visibility in order for
// later processes to properly apply it
if (verb === "patch") {
visibility |= Visibility.Patch;
}
return visibility;
}
/**
* Walks the given type and collects all applicable metadata and `@body`
* properties recursively.
*
* @param rootMapOut If provided, the map will be populated to link
* nested metadata properties to their root properties.
*/
export function gatherMetadata(
program: Program,
diagnostics: DiagnosticCollector, // currently unused, but reserved for future diagnostics
type: Type,
visibility: Visibility,
isMetadataCallback = isMetadata,
rootMapOut?: Map<ModelProperty, ModelProperty>
): Set<ModelProperty> {
const metadata = new Map<string, ModelProperty>();
if (type.kind !== "Model" || type.properties.size === 0) {
return new Set();
}
const visited = new Set();
const queue = new Queue<[Model, ModelProperty | undefined]>([[type, undefined]]);
while (!queue.isEmpty()) {
const [model, rootOpt] = queue.dequeue();
visited.add(model);
for (const property of walkPropertiesInherited(model)) {
const root = rootOpt ?? property;
if (!isVisible(program, property, visibility)) {
continue;
}
// ISSUE: This should probably be an error, but that's a breaking
// change that currently breaks some samples and tests.
//
// The traversal here is level-order so that the preferred metadata in
// the case of duplicates, which is the most compatible with prior
// behavior where nested metadata was always dropped.
if (metadata.has(property.name)) {
continue;
}
if (isApplicableMetadataOrBody(program, property, visibility, isMetadataCallback)) {
metadata.set(property.name, property);
rootMapOut?.set(property, root);
if (isBody(program, property)) {
continue; // We ignore any properties under `@body`
}
}
if (
property.type.kind === "Model" &&
!type.indexer &&
type.properties.size > 0 &&
!visited.has(property.type)
) {
queue.enqueue([property.type, root]);
}
}
}
return new Set(metadata.values());
}
/**
* Determines if a property is metadata. A property is defined to be
* metadata if it is marked `@header`, `@query`, `@path`, or `@statusCode`.
*/
export function isMetadata(program: Program, property: ModelProperty) {
return (
isHeader(program, property) ||
isQueryParam(program, property) ||
isPathParam(program, property) ||
isStatusCode(program, property)
);
}
/**
* Determines if the given property is visible with the given visibility.
*/
export function isVisible(program: Program, property: ModelProperty, visibility: Visibility) {
return isVisibleCore(program, property, visibilityToArray(visibility));
}
/**
* Determines if the given property is metadata that is applicable with the
* given visibility.
*
* - No metadata is applicable with Visibility.Item present.
* - If only Visibility.Read is present, then only `@header` and `@status`
* properties are applicable.
* - If Visibility.Read is not present, all metadata properties other than
* `@statusCode` are applicable.
*/
export function isApplicableMetadata(
program: Program,
property: ModelProperty,
visibility: Visibility,
isMetadataCallback = isMetadata
) {
return isApplicableMetadataCore(program, property, visibility, false, isMetadataCallback);
}
/**
* Determines if the given property is metadata or marked `@body` and
* applicable with the given visibility.
*/
export function isApplicableMetadataOrBody(
program: Program,
property: ModelProperty,
visibility: Visibility,
isMetadataCallback = isMetadata
) {
return isApplicableMetadataCore(program, property, visibility, true, isMetadataCallback);
}
function isApplicableMetadataCore(
program: Program,
property: ModelProperty,
visibility: Visibility,
treatBodyAsMetadata: boolean,
isMetadataCallback: (program: Program, property: ModelProperty) => boolean
) {
if (visibility & Visibility.Item) {
return false; // no metadata is applicable to collection items
}
if (treatBodyAsMetadata && (isBody(program, property) || isBodyRoot(program, property))) {
return true;
}
if (!isMetadataCallback(program, property)) {
return false;
}
if (visibility & Visibility.Read) {
return isHeader(program, property) || isStatusCode(program, property);
}
if (!(visibility & Visibility.Read)) {
return !isStatusCode(program, property);
}
return true;
}
/**
* Provides information about changes that happen to a data type's payload
* when inapplicable metadata is added or invisible properties are removed.
*
* Results are computed on demand and expensive computations are memoized.
*/
export interface MetadataInfo {
/**
* Determines if the given type is a model that becomes empty once
* applicable metadata is removed and visibility is applied.
*
* Note that a model is not considered emptied if it was already empty in
* the first place, or has a base model or indexer.
*
* When the type of a property is emptied by visibility, the property
* itself is also removed.
* @deprecated This produces inconsistent behaviors and should be avoided.
*/
isEmptied(type: Type | undefined, visibility: Visibility): boolean;
/**
* Determines if the given type is transformed by applying the given
* visibility and removing invisible properties or adding inapplicable
* metadata properties.
*/
isTransformed(type: Type | undefined, visibility: Visibility): boolean;
/**
* Determines if the given property is part of the request or response
* payload and not applicable metadata {@link isApplicableMetadata} or
* filtered out by the given visibility.
*/
isPayloadProperty(
property: ModelProperty,
visibility: Visibility,
inExplicitBody?: boolean
): boolean;
/**
* Determines if the given property is optional in the request or
* response payload for the given visibility.
*/
isOptional(property: ModelProperty, visibility: Visibility): boolean;
/**
* If type is an anonymous model, tries to find a named model that has the
* same set of properties when non-payload properties are excluded.
*/
getEffectivePayloadType(type: Type, visibility: Visibility): Type;
}
export interface MetadataInfoOptions {
/**
* The visibility to be used as the baseline against which
* {@link MetadataInfo.isEmptied} and {@link MetadataInfo.isTransformed}
* are computed. If not specified, {@link Visibility.None} is used, which
* will consider that any model that has fields that are only visible to
* some visibilities as transformed.
*/
canonicalVisibility?: Visibility;
/**
* Optional callback to indicate that a property can be shared with the
* canonical representation even for visibilities where it is not visible.
*
* This is used, for example, in OpenAPI emit where a property can be
* marked `readOnly: true` to represent @visibility("read") without
* creating a separate schema schema for {@link Visibility.Read}.
*/
canShareProperty?(property: ModelProperty): boolean;
}
export function createMetadataInfo(program: Program, options?: MetadataInfoOptions): MetadataInfo {
const canonicalVisibility = options?.canonicalVisibility ?? Visibility.None;
const enum State {
NotTransformed,
Transformed,
Emptied,
ComputationInProgress,
}
const stateMap = new TwoLevelMap<Type, Visibility, State>();
return {
isEmptied,
isTransformed,
isPayloadProperty,
isOptional,
getEffectivePayloadType,
};
function isEmptied(type: Type | undefined, visibility: Visibility): boolean {
if (!type) {
return false;
}
const state = getState(type, visibility);
return state === State.Emptied;
}
function isTransformed(type: Type | undefined, visibility: Visibility): boolean {
if (!type) {
return false;
}
const state = getState(type, visibility);
switch (state) {
case State.Transformed:
return true;
case State.Emptied:
return visibility === canonicalVisibility || !isEmptied(type, canonicalVisibility);
default:
return false;
}
}
function getState(type: Type, visibility: Visibility): State {
return stateMap.getOrAdd(
type,
visibility,
() => computeState(type, visibility),
State.ComputationInProgress
);
}
function computeState(type: Type, visibility: Visibility): State {
switch (type.kind) {
case "Model":
return computeStateForModel(type, visibility);
case "Union":
return computeStateForUnion(type, visibility);
default:
return State.NotTransformed;
}
}
function computeStateForModel(model: Model, visibility: Visibility) {
if (computeIsEmptied(model, visibility)) {
return State.Emptied;
}
if (
isTransformed(model.indexer?.value, visibility | Visibility.Item) ||
isTransformed(model.baseModel, visibility)
) {
return State.Transformed;
}
for (const property of model.properties.values()) {
if (
isAddedRemovedOrMadeOptional(property, visibility) ||
isTransformed(property.type, visibility)
) {
return State.Transformed;
}
}
return State.NotTransformed;
}
function computeStateForUnion(union: Union, visibility: Visibility) {
for (const variant of union.variants.values()) {
if (isTransformed(variant.type, visibility)) {
return State.Transformed;
}
}
return State.NotTransformed;
}
function isAddedRemovedOrMadeOptional(property: ModelProperty, visibility: Visibility) {
if (visibility === canonicalVisibility) {
return false;
}
if (isOptional(property, canonicalVisibility) !== isOptional(property, visibility)) {
return true;
}
return (
isPayloadProperty(property, visibility, undefined, /* keep shared */ true) !==
isPayloadProperty(property, canonicalVisibility, undefined, /*keep shared*/ true)
);
}
function computeIsEmptied(model: Model, visibility: Visibility) {
if (model.baseModel || model.indexer || model.properties.size === 0) {
return false;
}
for (const property of model.properties.values()) {
if (isPayloadProperty(property, visibility, undefined, /* keep shared */ true)) {
return false;
}
}
return true;
}
function isOptional(property: ModelProperty, visibility: Visibility): boolean {
// Properties are made optional for patch requests if the visibility includes
// update, but not for array elements with the item flag since you must provide
// all array elements with required properties, even in a patch.
const hasUpdate = (visibility & Visibility.Update) !== 0;
const isPatch = (visibility & Visibility.Patch) !== 0;
const isItem = (visibility & Visibility.Item) !== 0;
return property.optional || (hasUpdate && isPatch && !isItem);
}
function isPayloadProperty(
property: ModelProperty,
visibility: Visibility,
inExplicitBody?: boolean,
keepShareableProperties?: boolean
): boolean {
if (
!inExplicitBody &&
(isBodyIgnore(program, property) ||
isApplicableMetadata(program, property, visibility) ||
(isMetadata(program, property) && !includeInapplicableMetadataInPayload(program, property)))
) {
return false;
}
if (!isVisible(program, property, visibility)) {
// NOTE: When we check if a model is transformed for a given
// visibility, we retain shared properties. It is not considered
// transformed if the only removed properties are shareable. However,
// if we do create a unique schema for a visibility, then we still
// drop invisible shareable properties from other uses of
// isPayloadProperty.
//
// For OpenAPI emit, for example, this means that we won't put a
// readOnly: true property into a specialized schema for a non-read
// visibility.
keepShareableProperties ??= visibility === canonicalVisibility;
return !!(keepShareableProperties && options?.canShareProperty?.(property));
}
return true;
}
/**
* If the type is an anonymous model, tries to find a named model that has the same
* set of properties when non-payload properties are excluded.
*/
function getEffectivePayloadType(type: Type, visibility: Visibility): Type {
if (type.kind === "Model" && !type.name) {
const effective = getEffectiveModelType(program, type, (p) =>
isPayloadProperty(p, visibility, undefined, /* keep shared */ false)
);
if (effective.name) {
return effective;
}
}
return type;
}
}