-
Notifications
You must be signed in to change notification settings - Fork 214
/
parameters.ts
162 lines (148 loc) · 5.08 KB
/
parameters.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
import {
createDiagnosticCollector,
Diagnostic,
filterModelProperties,
ModelProperty,
Operation,
Program,
} from "@cadl-lang/compiler";
import { createDiagnostic } from "../lib.js";
import { getAction, getCollectionAction, getResourceOperation } from "../rest.js";
import {
getHeaderFieldName,
getOperationVerb,
getPathParamName,
getQueryParamName,
isBody,
} from "./decorators.js";
import { gatherMetadata, getRequestVisibility, isMetadata } from "./metadata.js";
import { HttpOperationParameters, HttpVerb } from "./types.js";
export function getOperationParameters(
program: Program,
operation: Operation,
knownPathParamNames: string[] = []
): [HttpOperationParameters, readonly Diagnostic[]] {
const verb = getExplicitVerbForOperation(program, operation);
if (verb) {
return getOperationParametersForVerb(program, operation, verb, knownPathParamNames);
}
// If no verb is explicitly specified, it is POST if there is a body and
// GET otherwise. Theoretically, it is possible to use @visibility
// strangely such that there is no body if the verb is POST and there is a
// body if the verb is GET. In that rare case, GET is chosen arbitrarily.
const post = getOperationParametersForVerb(program, operation, "post", knownPathParamNames);
return post[0].bodyType
? post
: getOperationParametersForVerb(program, operation, "get", knownPathParamNames);
}
function getOperationParametersForVerb(
program: Program,
operation: Operation,
verb: HttpVerb,
knownPathParamNames: string[]
): [HttpOperationParameters, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const visibility = getRequestVisibility(verb);
const metadata = gatherMetadata(
program,
diagnostics,
operation.parameters,
visibility,
(_, param) => isMetadata(program, param) || isImplicitPathParam(param)
);
function isImplicitPathParam(param: ModelProperty) {
// Only top-level parameters can be implicit path parameters.
//
// This check should be simpler: `param.model === operation.parameters`,
// but that is blocked by https://github.com/microsoft/cadl/issues/1069
const isTopLevel = param === operation.parameters.properties.get(param.name);
return isTopLevel && knownPathParamNames.includes(param.name);
}
const result: HttpOperationParameters = {
parameters: [],
verb,
};
for (const param of metadata) {
const queryParam = getQueryParamName(program, param);
const pathParam =
getPathParamName(program, param) ?? (isImplicitPathParam(param) && param.name);
const headerParam = getHeaderFieldName(program, param);
const bodyParam = isBody(program, param);
const defined = [
["query", queryParam],
["path", pathParam],
["header", headerParam],
["body", bodyParam],
].filter((x) => !!x[1]);
if (defined.length >= 2) {
diagnostics.add(
createDiagnostic({
code: "operation-param-duplicate-type",
format: { paramName: param.name, types: defined.map((x) => x[0]).join(", ") },
target: param,
})
);
}
if (queryParam) {
result.parameters.push({ type: "query", name: queryParam, param });
} else if (pathParam) {
if (param.optional && param.default === undefined) {
diagnostics.add(
createDiagnostic({
code: "optional-path-param",
format: { paramName: param.name },
target: operation,
})
);
}
result.parameters.push({ type: "path", name: pathParam, param });
} else if (headerParam) {
result.parameters.push({ type: "header", name: headerParam, param });
} else if (bodyParam) {
if (result.bodyType === undefined) {
result.bodyParameter = param;
result.bodyType = param.type;
} else {
diagnostics.add(createDiagnostic({ code: "duplicate-body", target: param }));
}
}
}
const unannotatedProperties = filterModelProperties(
program,
operation.parameters,
(p) => !metadata.has(p)
);
if (unannotatedProperties.properties.size > 0) {
if (result.bodyType === undefined) {
result.bodyType = unannotatedProperties;
} else {
diagnostics.add(
createDiagnostic({
code: "duplicate-body",
messageId: "bodyAndUnannotated",
target: operation,
})
);
}
}
return diagnostics.wrap(result);
}
function getExplicitVerbForOperation(program: Program, operation: Operation): HttpVerb | undefined {
const resourceOperation = getResourceOperation(program, operation);
const verb =
(resourceOperation && resourceOperationToVerb[resourceOperation.operation]) ??
getOperationVerb(program, operation) ??
// TODO: Enable this verb choice to be customized!
(getAction(program, operation) || getCollectionAction(program, operation) ? "post" : undefined);
return verb;
}
// TODO: Make this overridable by libraries
const resourceOperationToVerb: any = {
read: "get",
create: "post",
createOrUpdate: "patch",
createOrReplace: "put",
update: "patch",
delete: "delete",
list: "get",
};