sheridan-typespec-aspnet 0.2.1 → 0.3.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/README.md +10 -3
- package/dist/src/emitter.js +70 -2
- package/dist/src/lib.d.ts +32 -2
- package/dist/src/lib.js +12 -0
- package/dist/src/model/collect-types.d.ts +3 -1
- package/dist/src/model/collect-types.js +1 -1
- package/dist/src/model/collect.js +39 -3
- package/dist/src/model/types.d.ts +10 -0
- package/dist/src/naming/names.js +11 -1
- package/dist/src/naming/services.d.ts +10 -0
- package/dist/src/naming/services.js +11 -2
- package/dist/src/render/minimal/documentation.d.ts +4 -0
- package/dist/src/render/minimal/documentation.js +107 -0
- package/dist/src/render/minimal/endpoints.d.ts +14 -0
- package/dist/src/render/minimal/endpoints.js +139 -0
- package/dist/src/render/minimal/results.d.ts +11 -0
- package/dist/src/render/minimal/results.js +145 -0
- package/dist/src/render/shared/api-setup.d.ts +6 -1
- package/dist/src/render/shared/api-setup.js +131 -11
- package/dist/src/render/shared/binding.d.ts +33 -0
- package/dist/src/render/shared/binding.js +395 -0
- package/dist/src/render/shared/headers.d.ts +7 -0
- package/dist/src/render/shared/headers.js +52 -0
- package/dist/src/render/shared/models.js +5 -23
- package/dist/src/render/shared/openapi.d.ts +1 -0
- package/dist/src/render/shared/openapi.js +248 -0
- package/dist/src/render/shared/parameters.d.ts +3 -0
- package/dist/src/render/shared/parameters.js +224 -0
- package/dist/src/render/shared/references.d.ts +3 -0
- package/dist/src/render/shared/references.js +18 -0
- package/dist/src/render/shared/request-selector.d.ts +1 -0
- package/dist/src/render/shared/request-selector.js +86 -0
- package/dist/src/render/shared/routes.d.ts +37 -0
- package/dist/src/render/shared/routes.js +241 -0
- package/dist/src/render/shared/validation.d.ts +6 -1
- package/dist/src/render/shared/validation.js +60 -0
- package/dist/src/scaffold/projects.d.ts +1 -1
- package/dist/src/scaffold/projects.js +4 -3
- package/package.json +2 -2
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { reportDiagnostic } from "../../lib.js";
|
|
2
|
+
import { isReference } from "./references.js";
|
|
3
|
+
import { mapScalar, numberLiteral, systemTypes } from "./scalars.js";
|
|
4
|
+
import { holds, uncheckedConstraints, unreached, validationAttributes } from "./validation.js";
|
|
5
|
+
import { stringLiteral } from "./writer.js";
|
|
6
|
+
const nativeTypes = new Set("string bool sbyte short int long byte ushort uint ulong float double decimal".split(" "));
|
|
7
|
+
const nullable = (type) => (type.endsWith("?") ? type : `${type}?`);
|
|
8
|
+
const bare = (ref) => (ref.kind === "nullable" ? bare(ref.type) : ref);
|
|
9
|
+
const isJson = (type) => /^application\/(?:[\w.-]+\+)?json$/i.test(type.split(";")[0]);
|
|
10
|
+
export function bindingWriter(program, model, rootNamespace, typeOf) {
|
|
11
|
+
const parameters = `global::${rootNamespace}.Requests.Parameters`;
|
|
12
|
+
function refuse(what, target) {
|
|
13
|
+
reportDiagnostic(program, { code: "unsupported-type", format: { kind: what }, target });
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
/** A reader that parses text as the JSON of the type would be read. */
|
|
17
|
+
function parsed(type, name, number, converter) {
|
|
18
|
+
const generic = converter ? `${type}, ${converter}` : type;
|
|
19
|
+
return {
|
|
20
|
+
type,
|
|
21
|
+
native: false,
|
|
22
|
+
read: (text) => `${parameters}.Value<${generic}>(${text}, ${number}, ${stringLiteral(name)})`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function scalarReader(builtin, encoding, wireName, target) {
|
|
26
|
+
const mapped = mapScalar(builtin, encoding);
|
|
27
|
+
if (!mapped)
|
|
28
|
+
return refuse(`${builtin} with this encoding`, target);
|
|
29
|
+
const type = systemTypes.has(mapped.type) ? `global::System.${mapped.type}` : mapped.type;
|
|
30
|
+
if (type === "string")
|
|
31
|
+
return { type, native: true, read: (text) => text };
|
|
32
|
+
if (nativeTypes.has(type))
|
|
33
|
+
return { ...parsed(type, wireName, true), native: true };
|
|
34
|
+
const converter = mapped.attributes
|
|
35
|
+
.map((attribute) => /^JsonConverter\(typeof\((\w+)\)\)$/.exec(attribute)?.[1])
|
|
36
|
+
.find(Boolean);
|
|
37
|
+
const number = encoding !== undefined && encoding.wireType !== "string";
|
|
38
|
+
return parsed(type, wireName, number, converter && `global::${rootNamespace}.Serialization.${converter}`);
|
|
39
|
+
}
|
|
40
|
+
function reader(ref, encoding, wireName, target) {
|
|
41
|
+
const value = bare(ref);
|
|
42
|
+
if (value.kind === "scalar")
|
|
43
|
+
return scalarReader(value.name, encoding, wireName, target);
|
|
44
|
+
if (value.kind === "literal") {
|
|
45
|
+
const type = typeOf(value, target);
|
|
46
|
+
return type === "string"
|
|
47
|
+
? { type, native: true, read: (text) => text }
|
|
48
|
+
: { ...parsed(type, wireName, true), native: true };
|
|
49
|
+
}
|
|
50
|
+
if (value.kind !== "named" && value.kind !== "enumMember") {
|
|
51
|
+
return refuse(`${value.kind} in a ${wireName} parameter`, target);
|
|
52
|
+
}
|
|
53
|
+
const declared = model.types.get(value.kind === "named" ? value.id : value.enum);
|
|
54
|
+
if (declared.kind === "scalar") {
|
|
55
|
+
return declared.builtin
|
|
56
|
+
? scalarReader(declared.builtin, encoding, wireName, target)
|
|
57
|
+
: refuse(`scalar "${declared.name}" that derives from no built-in`, target);
|
|
58
|
+
}
|
|
59
|
+
const type = typeOf(value, target);
|
|
60
|
+
if (declared.kind === "enum") {
|
|
61
|
+
return parsed(type, wireName, typeof declared.members[0]?.value === "number");
|
|
62
|
+
}
|
|
63
|
+
// A union of strings is read from its text; other unions have no one way to be written.
|
|
64
|
+
const strings = declared.kind === "union" &&
|
|
65
|
+
declared.variants.every(({ type: variant }) => variant.kind === "literal"
|
|
66
|
+
? typeof variant.value === "string"
|
|
67
|
+
: variant.kind === "scalar" && variant.name === "string");
|
|
68
|
+
return strings
|
|
69
|
+
? parsed(type, wireName, false)
|
|
70
|
+
: refuse(`${declared.kind} in a parameter`, target);
|
|
71
|
+
}
|
|
72
|
+
/** The default of a native value, as a C# literal. */
|
|
73
|
+
function literal(value, type) {
|
|
74
|
+
if (type === "string")
|
|
75
|
+
return typeof value === "string" ? stringLiteral(value) : undefined;
|
|
76
|
+
if (type === "bool")
|
|
77
|
+
return typeof value === "boolean" ? String(value) : undefined;
|
|
78
|
+
const builtin = {
|
|
79
|
+
sbyte: "int8",
|
|
80
|
+
short: "int16",
|
|
81
|
+
int: "int32",
|
|
82
|
+
long: "int64",
|
|
83
|
+
byte: "uint8",
|
|
84
|
+
ushort: "uint16",
|
|
85
|
+
uint: "uint32",
|
|
86
|
+
ulong: "uint64",
|
|
87
|
+
float: "float32",
|
|
88
|
+
double: "float64",
|
|
89
|
+
decimal: "decimal",
|
|
90
|
+
};
|
|
91
|
+
return typeof value === "number" || typeof value === "string"
|
|
92
|
+
? numberLiteral(String(value), builtin[type])
|
|
93
|
+
: undefined;
|
|
94
|
+
}
|
|
95
|
+
/** The default of a value, as the text a request would hold. */
|
|
96
|
+
function defaultText(value, encoding) {
|
|
97
|
+
if (!["string", "number", "boolean"].includes(typeof value))
|
|
98
|
+
return undefined;
|
|
99
|
+
// The default is given as ISO 8601, whatever the date is encoded as in a header.
|
|
100
|
+
if (encoding?.name === "rfc7231" && !Number.isNaN(Date.parse(String(value)))) {
|
|
101
|
+
return new Date(String(value)).toUTCString();
|
|
102
|
+
}
|
|
103
|
+
return String(value);
|
|
104
|
+
}
|
|
105
|
+
function notApplied(input) {
|
|
106
|
+
reportDiagnostic(program, {
|
|
107
|
+
code: "unsupported-type",
|
|
108
|
+
format: { kind: `default value of the parameter "${input.name}"` },
|
|
109
|
+
target: input.source,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function bindList(input, element, name, context) {
|
|
113
|
+
const item = reader(element, input.encoding, input.wireName, input.source);
|
|
114
|
+
if (!item)
|
|
115
|
+
return undefined;
|
|
116
|
+
const itemType = element.kind === "nullable" ? nullable(item.type) : item.type;
|
|
117
|
+
const convert = (list) => item.type === "string"
|
|
118
|
+
? list
|
|
119
|
+
: `${parameters}.List<${itemType}>(${list}, item => ${item.read("item")})`;
|
|
120
|
+
// The wire text of each item of the default.
|
|
121
|
+
const items = Array.isArray(input.default)
|
|
122
|
+
? input.default.map((value) => defaultText(value, input.encoding))
|
|
123
|
+
: undefined;
|
|
124
|
+
if (input.default !== undefined && (!items || items.includes(undefined)))
|
|
125
|
+
notApplied(input);
|
|
126
|
+
const fallback = items && `new string[] { ${items.map((text) => stringLiteral(text ?? "")).join(", ")} }`;
|
|
127
|
+
const optional = input.optional && input.default === undefined;
|
|
128
|
+
// A repeated query value is a list already.
|
|
129
|
+
if (input.in === "query" && input.explode) {
|
|
130
|
+
const bound = item.native ? itemType : "string";
|
|
131
|
+
const parameter = {
|
|
132
|
+
source: "query",
|
|
133
|
+
wireName: input.wireName,
|
|
134
|
+
type: `${bound}[]`,
|
|
135
|
+
name,
|
|
136
|
+
};
|
|
137
|
+
const list = item.native ? name : convert(name);
|
|
138
|
+
const other = fallback && (item.native ? convert(fallback) : convert(fallback));
|
|
139
|
+
// An empty list cannot be written, so none sent is none given.
|
|
140
|
+
const value = other
|
|
141
|
+
? `${name}.Length == 0 ? ${other} : ${list}`
|
|
142
|
+
: optional
|
|
143
|
+
? `${name}.Length == 0 ? null : ${list}`
|
|
144
|
+
: list;
|
|
145
|
+
return { parameter, value };
|
|
146
|
+
}
|
|
147
|
+
const headers = input.in === "header";
|
|
148
|
+
// An HTTP date holds a comma of its own.
|
|
149
|
+
const split = (text) => input.encoding?.name === "rfc7231"
|
|
150
|
+
? `${parameters}.SplitDates(${text})`
|
|
151
|
+
: `${parameters}.Split(${text}, ${headers})`;
|
|
152
|
+
if (input.in === "cookie") {
|
|
153
|
+
const text = `${context}.Request.Cookies[${stringLiteral(input.wireName)}]`;
|
|
154
|
+
const sent = `${parameters}.Required(${text}, ${stringLiteral(input.wireName)})`;
|
|
155
|
+
const value = fallback
|
|
156
|
+
? `${text} is { } ${name} ? ${convert(split(name))} : ${convert(fallback)}`
|
|
157
|
+
: optional
|
|
158
|
+
? `${text} is { } ${name} ? ${convert(split(name))} : null`
|
|
159
|
+
: convert(split(sent));
|
|
160
|
+
return { value };
|
|
161
|
+
}
|
|
162
|
+
const parameter = {
|
|
163
|
+
source: input.in === "path" ? "route" : input.in,
|
|
164
|
+
wireName: input.wireName,
|
|
165
|
+
type: input.optional || input.default !== undefined ? "string?" : "string",
|
|
166
|
+
name,
|
|
167
|
+
};
|
|
168
|
+
const value = fallback
|
|
169
|
+
? `${name} is null ? ${convert(fallback)} : ${convert(split(name))}`
|
|
170
|
+
: optional
|
|
171
|
+
? `${name} is null ? null : ${convert(split(name))}`
|
|
172
|
+
: convert(split(name));
|
|
173
|
+
return { parameter, value };
|
|
174
|
+
}
|
|
175
|
+
function bindParameter(input, name, context) {
|
|
176
|
+
const type = bare(input.type);
|
|
177
|
+
// Such as pipe delimited arrays, which are not read yet.
|
|
178
|
+
if (type.kind === "array") {
|
|
179
|
+
if (input.encoding?.name?.startsWith("ArrayEncoding.")) {
|
|
180
|
+
return refuse(`array parameter encoded as ${input.encoding.name}`, input.source);
|
|
181
|
+
}
|
|
182
|
+
return bindList(input, type.element, name, context);
|
|
183
|
+
}
|
|
184
|
+
const found = reader(input.type, input.encoding, input.wireName, input.source);
|
|
185
|
+
if (!found)
|
|
186
|
+
return undefined;
|
|
187
|
+
const optional = input.optional || input.default !== undefined;
|
|
188
|
+
const given = input.default;
|
|
189
|
+
if (input.in !== "cookie" && found.native) {
|
|
190
|
+
const parameter = {
|
|
191
|
+
source: input.in === "path" ? "route" : input.in,
|
|
192
|
+
wireName: input.wireName,
|
|
193
|
+
// A required value must be sent, even when the service may be given null.
|
|
194
|
+
type: optional ? nullable(found.type) : found.type,
|
|
195
|
+
name,
|
|
196
|
+
};
|
|
197
|
+
if (given === undefined || given === null)
|
|
198
|
+
return { parameter, value: name };
|
|
199
|
+
const other = literal(given, found.type);
|
|
200
|
+
if (other === undefined)
|
|
201
|
+
notApplied(input);
|
|
202
|
+
return { parameter, value: `${name} ?? ${other}` };
|
|
203
|
+
}
|
|
204
|
+
const text = given === undefined || given === null ? undefined : defaultText(given, input.encoding);
|
|
205
|
+
if (given !== undefined && given !== null && text === undefined)
|
|
206
|
+
notApplied(input);
|
|
207
|
+
const missing = text === undefined ? undefined : stringLiteral(text);
|
|
208
|
+
if (input.in === "cookie") {
|
|
209
|
+
const cookie = `${context}.Request.Cookies[${stringLiteral(input.wireName)}]`;
|
|
210
|
+
const value = missing
|
|
211
|
+
? found.read(`${cookie} ?? ${missing}`)
|
|
212
|
+
: optional
|
|
213
|
+
? `${cookie} is { } ${name} ? ${found.read(name)} : null`
|
|
214
|
+
: found.read(`${parameters}.Required(${cookie}, ${stringLiteral(input.wireName)})`);
|
|
215
|
+
return { value };
|
|
216
|
+
}
|
|
217
|
+
const parameter = {
|
|
218
|
+
source: input.in === "path" ? "route" : input.in,
|
|
219
|
+
wireName: input.wireName,
|
|
220
|
+
type: optional ? "string?" : "string",
|
|
221
|
+
name,
|
|
222
|
+
};
|
|
223
|
+
const value = missing
|
|
224
|
+
? found.read(`${name} ?? ${missing}`)
|
|
225
|
+
: optional
|
|
226
|
+
? `${name} is null ? null : ${found.read(name)}`
|
|
227
|
+
: found.read(name);
|
|
228
|
+
return { parameter, value };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Adds the checks of a parameter. ASP.NET Core checks those on a handler's parameter; a list
|
|
232
|
+
* or a cookie is checked once it is read.
|
|
233
|
+
*/
|
|
234
|
+
function constrain(input, bound, given) {
|
|
235
|
+
const kind = holds(model, input.type);
|
|
236
|
+
const unchecked = [
|
|
237
|
+
...uncheckedConstraints(input.constraints ?? {}, kind),
|
|
238
|
+
...(input.heldConstraints ?? []),
|
|
239
|
+
];
|
|
240
|
+
for (const name of unchecked) {
|
|
241
|
+
reportDiagnostic(program, {
|
|
242
|
+
code: "constraint-not-checked",
|
|
243
|
+
format: { name },
|
|
244
|
+
target: input.source,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
const attributes = validationAttributes(input.constraints ?? {}, kind, `global::${rootNamespace}.Validation`);
|
|
248
|
+
if (attributes.length === 0)
|
|
249
|
+
return bound;
|
|
250
|
+
if (bound.parameter && kind !== "array") {
|
|
251
|
+
return { ...bound, parameter: { ...bound.parameter, attributes } };
|
|
252
|
+
}
|
|
253
|
+
return { ...bound, value: check(bound.value, given, attributes) };
|
|
254
|
+
}
|
|
255
|
+
/** Checks a value where it is used, naming it as ASP.NET Core names a handler's parameter. */
|
|
256
|
+
function check(value, given, attributes) {
|
|
257
|
+
const name = stringLiteral(given.name.replace(/^@/, ""));
|
|
258
|
+
// The type is written, as a value that may be left out has none of its own.
|
|
259
|
+
const list = attributes.map(construct).join(", ");
|
|
260
|
+
return `${parameters}.Check<${given.type}>(${value}, ${name}, ${list})`;
|
|
261
|
+
}
|
|
262
|
+
/** The expression making an attribute written as `Name(arguments, Property = value)`. */
|
|
263
|
+
function construct(attribute) {
|
|
264
|
+
const open = attribute.indexOf("(");
|
|
265
|
+
const type = `${attribute.slice(0, open)}Attribute`;
|
|
266
|
+
const list = attribute.slice(open + 1, -1);
|
|
267
|
+
// Text in a string is not an argument.
|
|
268
|
+
const outside = list.replace(/"(?:\\.|[^"\\])*"/g, (text) => "_".repeat(text.length));
|
|
269
|
+
const named = outside.search(/(?:^|, )[A-Z]\w* = /);
|
|
270
|
+
if (named < 0)
|
|
271
|
+
return `new ${type}(${list})`;
|
|
272
|
+
const properties = list.slice(named).replace(/^, /, "");
|
|
273
|
+
return `new ${type}(${list.slice(0, named)}) { ${properties} }`;
|
|
274
|
+
}
|
|
275
|
+
/** Reads every input of an operation, naming the handler's own parameters apart from them. */
|
|
276
|
+
return function bind(named, service, signature, writes) {
|
|
277
|
+
const { operation } = named;
|
|
278
|
+
const taken = new Set(signature.map((parameter) => parameter.name));
|
|
279
|
+
const free = (wanted) => {
|
|
280
|
+
let name = wanted;
|
|
281
|
+
for (let number = 2; taken.has(name); number++)
|
|
282
|
+
name = `${wanted}${number}`;
|
|
283
|
+
taken.add(name);
|
|
284
|
+
return name;
|
|
285
|
+
};
|
|
286
|
+
const serviceName = free("service");
|
|
287
|
+
const context = free("httpContext");
|
|
288
|
+
const found = {
|
|
289
|
+
parameters: [],
|
|
290
|
+
values: [],
|
|
291
|
+
accepts: [],
|
|
292
|
+
checks: new Map(),
|
|
293
|
+
result: free("result"),
|
|
294
|
+
found: free("found"),
|
|
295
|
+
};
|
|
296
|
+
// The response may need the context too.
|
|
297
|
+
let usesContext = writes;
|
|
298
|
+
operation.parameters.forEach((input, index) => {
|
|
299
|
+
const read = bindParameter(input, named.parameters[index], context);
|
|
300
|
+
if (!read)
|
|
301
|
+
return;
|
|
302
|
+
const bound = constrain(input, read, {
|
|
303
|
+
name: named.parameters[index],
|
|
304
|
+
type: signature[index].type,
|
|
305
|
+
});
|
|
306
|
+
if (bound.parameter)
|
|
307
|
+
found.parameters.push(bound.parameter);
|
|
308
|
+
else
|
|
309
|
+
usesContext = true;
|
|
310
|
+
const checks = validationAttributes(input.constraints ?? {}, holds(model, input.type), `global::${rootNamespace}.Validation`);
|
|
311
|
+
if (checks.length)
|
|
312
|
+
found.checks.set(index, checks.map(construct));
|
|
313
|
+
found.values.push(bound.value);
|
|
314
|
+
});
|
|
315
|
+
const { body } = operation;
|
|
316
|
+
if (body) {
|
|
317
|
+
const name = named.body;
|
|
318
|
+
const type = signature[operation.parameters.length].type;
|
|
319
|
+
const reference = body.kind !== "single" || isReference(model, body.type);
|
|
320
|
+
found.bodyType = `typeof(${reference ? type.replace(/\?$/, "") : type})`;
|
|
321
|
+
const kind = body.kind === "single" ? holds(model, body.type) : "other";
|
|
322
|
+
const attributes = validationAttributes(body.constraints ?? {}, kind, `global::${rootNamespace}.Validation`);
|
|
323
|
+
const unchecked = [
|
|
324
|
+
...uncheckedConstraints(body.constraints ?? {}, kind),
|
|
325
|
+
...(body.heldConstraints ?? []),
|
|
326
|
+
...(body.kind === "single" ? [unreached(model, body.type) ?? []].flat() : []),
|
|
327
|
+
];
|
|
328
|
+
for (const constraint of unchecked) {
|
|
329
|
+
reportDiagnostic(program, {
|
|
330
|
+
code: "constraint-not-checked",
|
|
331
|
+
format: { name: constraint },
|
|
332
|
+
target: operation.source,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
if (body.kind === "single" && body.contentTypes.every(isJson)) {
|
|
336
|
+
found.parameters.push({
|
|
337
|
+
source: "body",
|
|
338
|
+
type,
|
|
339
|
+
name,
|
|
340
|
+
...(attributes.length && { attributes }),
|
|
341
|
+
});
|
|
342
|
+
found.values.push(name);
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
// ASP.NET Core reads only JSON, so text and bytes are read by hand.
|
|
346
|
+
const held = type.replace(/\?$/, "");
|
|
347
|
+
const text = body.contentTypes.every((t) => t.toLowerCase().startsWith("text/"));
|
|
348
|
+
const method = held === "byte[]" ? "Bytes" : text || held === "string" ? "Text" : undefined;
|
|
349
|
+
if (body.kind !== "single" || !method || body.contentTypes.some(isJson)) {
|
|
350
|
+
refuse(`body sent as ${body.contentTypes.join(", ")}`, operation.source);
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
usesContext = true;
|
|
354
|
+
const cancellation = named.cancellation;
|
|
355
|
+
const sent = `await ${parameters}.${method}(${context}.Request, ${body.optional}, ${cancellation})`;
|
|
356
|
+
const checked = (value) => attributes.length ? check(value, { name, type }, attributes) : value;
|
|
357
|
+
if (method === "Bytes" || held === "string") {
|
|
358
|
+
found.values.push(checked(sent));
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
// Other values sent as text are read as parameters are.
|
|
362
|
+
const parsed = reader(body.type, undefined, name, operation.source);
|
|
363
|
+
if (parsed) {
|
|
364
|
+
const text = free("text");
|
|
365
|
+
const read = parsed.read(text);
|
|
366
|
+
found.values.push(checked(body.optional
|
|
367
|
+
? `${sent} is { } ${text} ? ${read} : null`
|
|
368
|
+
: `${parameters}.With(${sent}, ${text} => ${read})`));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
found.accepts = body.contentTypes.filter((type) => !type.includes("*"));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
found.parameters.push({
|
|
376
|
+
source: "service",
|
|
377
|
+
type: service,
|
|
378
|
+
name: serviceName,
|
|
379
|
+
});
|
|
380
|
+
if (usesContext) {
|
|
381
|
+
found.parameters.push({
|
|
382
|
+
source: "context",
|
|
383
|
+
type: "global::Microsoft.AspNetCore.Http.HttpContext",
|
|
384
|
+
name: context,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
found.parameters.push({
|
|
388
|
+
source: "cancellation",
|
|
389
|
+
type: "global::System.Threading.CancellationToken",
|
|
390
|
+
name: named.cancellation,
|
|
391
|
+
});
|
|
392
|
+
found.values.push(named.cancellation);
|
|
393
|
+
return found;
|
|
394
|
+
};
|
|
395
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ApiModel, ParameterModel } from "../../model/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* An expression giving the text of a header, or null when there is nothing to send. Undefined when
|
|
4
|
+
* the header cannot be written, which is reported when the result is named.
|
|
5
|
+
*/
|
|
6
|
+
export type HeaderWriter = (header: Pick<ParameterModel, "type" | "encoding" | "wireName" | "source">, value: string, type: string) => string | undefined;
|
|
7
|
+
export declare function headerWriter(model: ApiModel, rootNamespace: string, refuse: (what: string, header: Pick<ParameterModel, "source">) => void): HeaderWriter;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { mapScalar } from "./scalars.js";
|
|
2
|
+
const bare = (ref) => (ref.kind === "nullable" ? bare(ref.type) : ref);
|
|
3
|
+
export function headerWriter(model, rootNamespace, refuse) {
|
|
4
|
+
const parameters = `global::${rootNamespace}.Requests.Parameters`;
|
|
5
|
+
/** The converter of an encoded value, when its type has none of its own. */
|
|
6
|
+
function converter(ref, header) {
|
|
7
|
+
const value = bare(ref);
|
|
8
|
+
let builtin = value.kind === "scalar" ? value.name : undefined;
|
|
9
|
+
if (value.kind === "named") {
|
|
10
|
+
const declared = model.types.get(value.id);
|
|
11
|
+
if (declared.kind === "scalar")
|
|
12
|
+
builtin = declared.builtin;
|
|
13
|
+
}
|
|
14
|
+
const name = builtin &&
|
|
15
|
+
mapScalar(builtin, header.encoding)
|
|
16
|
+
?.attributes.map((a) => /^JsonConverter\(typeof\((\w+)\)\)$/.exec(a)?.[1])
|
|
17
|
+
.find(Boolean);
|
|
18
|
+
return name ? `global::${rootNamespace}.Serialization.${name}` : undefined;
|
|
19
|
+
}
|
|
20
|
+
function holdsRecordsOrLists(ref) {
|
|
21
|
+
const value = bare(ref);
|
|
22
|
+
if (value.kind === "named") {
|
|
23
|
+
const kind = model.types.get(value.id).kind;
|
|
24
|
+
return kind === "model";
|
|
25
|
+
}
|
|
26
|
+
return ["array", "record", "tuple", "unknown"].includes(value.kind);
|
|
27
|
+
}
|
|
28
|
+
return (header, value, type) => {
|
|
29
|
+
const ref = bare(header.type);
|
|
30
|
+
const item = ref.kind === "array" ? bare(ref.element) : ref;
|
|
31
|
+
if (holdsRecordsOrLists(item)) {
|
|
32
|
+
refuse(`${item.kind} written as text in "${header.wireName}"`, header);
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
const used = converter(item, header);
|
|
36
|
+
const itemType = ref.kind === "array" ? undefined : type;
|
|
37
|
+
const write = (text, held) => used
|
|
38
|
+
? `${parameters}.Write<${held}, ${used}>(${text})`
|
|
39
|
+
: `${parameters}.Write<${held}>(${text})`;
|
|
40
|
+
if (ref.kind !== "array") {
|
|
41
|
+
return itemType.replace(/\?$/, "") === "string" ? value : write(value, itemType);
|
|
42
|
+
}
|
|
43
|
+
// The item type is that of the member, without its brackets.
|
|
44
|
+
const element = type
|
|
45
|
+
.replace(/\?$/, "")
|
|
46
|
+
.replace(/^global::System\.Collections\.Generic\.IEnumerable<(.*)>$/, "$1[]")
|
|
47
|
+
.replace(/\[\]$/, "");
|
|
48
|
+
return element === "string"
|
|
49
|
+
? `${parameters}.Join(${value})`
|
|
50
|
+
: `${parameters}.Join(${value}, item => ${write("item", element)})`;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -4,7 +4,7 @@ import { pascalCase } from "../../naming/identifiers.js";
|
|
|
4
4
|
import { memberFor, renderChoice, shapeOf } from "./choices.js";
|
|
5
5
|
import { discriminatorValue, lineage } from "./hierarchy.js";
|
|
6
6
|
import { mapScalar, numberLiteral, systemTypes } from "./scalars.js";
|
|
7
|
-
import { uncheckedConstraints, validationAttributes } from "./validation.js";
|
|
7
|
+
import { uncheckedConstraints, validationAttributes, holds, unreached } from "./validation.js";
|
|
8
8
|
import { csharpFile, docComment, stringLiteral } from "./writer.js";
|
|
9
9
|
const json = "global::System.Text.Json";
|
|
10
10
|
const collections = "global::System.Collections.Generic";
|
|
@@ -172,25 +172,6 @@ export function renderModels(program, model, names, options) {
|
|
|
172
172
|
required: false,
|
|
173
173
|
};
|
|
174
174
|
}
|
|
175
|
-
/** What a property holds, for deciding which constraints apply. */
|
|
176
|
-
function holds(ref) {
|
|
177
|
-
if (ref.kind === "nullable")
|
|
178
|
-
return holds(ref.type);
|
|
179
|
-
if (ref.kind === "array")
|
|
180
|
-
return "array";
|
|
181
|
-
let name = ref.kind === "scalar" ? ref.name : undefined;
|
|
182
|
-
if (ref.kind === "named") {
|
|
183
|
-
const type = model.types.get(ref.id);
|
|
184
|
-
if (type.kind === "scalar")
|
|
185
|
-
name = type.builtin;
|
|
186
|
-
}
|
|
187
|
-
const mapped = name === undefined ? undefined : mapScalar(name)?.type;
|
|
188
|
-
if (mapped === "string")
|
|
189
|
-
return "string";
|
|
190
|
-
return mapped && /^(s?byte|u?short|u?int|u?long|float|double|decimal)$/.test(mapped)
|
|
191
|
-
? "number"
|
|
192
|
-
: "other";
|
|
193
|
-
}
|
|
194
175
|
const reported = new Map();
|
|
195
176
|
/** True once for each place in the spec, however many body shapes repeat it. */
|
|
196
177
|
function firstReport(what, target) {
|
|
@@ -212,13 +193,14 @@ export function renderModels(program, model, names, options) {
|
|
|
212
193
|
}
|
|
213
194
|
function checks(property, required) {
|
|
214
195
|
const namespace = `global::${options.rootNamespace}.Validation`;
|
|
215
|
-
const found = validationAttributes(property.constraints ?? {}, holds(property.type), namespace);
|
|
196
|
+
const found = validationAttributes(property.constraints ?? {}, holds(model, property.type), namespace);
|
|
216
197
|
reportUnchecked([
|
|
217
|
-
...uncheckedConstraints(property.constraints ?? {}, holds(property.type)),
|
|
198
|
+
...uncheckedConstraints(property.constraints ?? {}, holds(model, property.type)),
|
|
218
199
|
...(property.heldConstraints ?? []),
|
|
200
|
+
...[unreached(model, property.type) ?? []].flat(),
|
|
219
201
|
], property.source);
|
|
220
202
|
// JSON can still send null for a reference that C# says is never null.
|
|
221
|
-
const kind = holds(property.type);
|
|
203
|
+
const kind = holds(model, property.type);
|
|
222
204
|
const target = property.type.kind === "named" ? model.types.get(property.type.id) : undefined;
|
|
223
205
|
const reference = kind === "string" ||
|
|
224
206
|
kind === "array" ||
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function renderOpenApi(rootNamespace: string): string;
|