-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathlib.ts
More file actions
453 lines (435 loc) · 16 KB
/
lib.ts
File metadata and controls
453 lines (435 loc) · 16 KB
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
import { createTypeSpecLibrary, JSONSchemaType, paramMessage } from "@typespec/compiler";
export type FileType = "yaml" | "json";
export type OpenAPIVersion = "3.0.0" | "3.1.0" | "3.2.0";
export type ExperimentalParameterExamplesStrategy = "data" | "serialized";
export interface OpenAPI3EmitterOptions {
/**
* If the content should be serialized as YAML or JSON. Can be a single value or an array to emit multiple file types.
* When an array is provided, the `{file-type}` variable can be used in `output-file` to produce distinct filenames.
* @default yaml, it not specified infer from the `output-file` extension
*/
"file-type"?: FileType | FileType[];
/**
* Name of the output file.
* Output file will interpolate the following values:
* - service-name: Name of the service
* - service-name-if-multiple: Name of the service if multiple
* - version: Version of the service if multiple
*
* @default `{service-name-if-multiple}.{version}.openapi.yaml` or `.json` if {@link OpenAPI3EmitterOptions["file-type"]} is `"json"`. When `file-type` is an array, uses `{file-type}` variable.
*
* @example Single service no versioning
* - `openapi.yaml`
*
* @example Multiple services no versioning
* - `openapi.Org1.Service1.yaml`
* - `openapi.Org1.Service2.yaml`
*
* @example Single service with versioning
* - `openapi.v1.yaml`
* - `openapi.v2.yaml`
*
* @example Multiple service with versioning
* - `openapi.Org1.Service1.v1.yaml`
* - `openapi.Org1.Service1.v2.yaml`
* - `openapi.Org1.Service2.v1.0.yaml`
* - `openapi.Org1.Service2.v1.1.yaml`
*/
"output-file"?: string;
/**
* The Open API specification versions to emit.
* If more than one version is specified, then the output file
* will be created inside a directory matching each specification version.
*
* @default ["3.0.0"]
*/
"openapi-versions"?: OpenAPIVersion[];
/**
* Set the newline character for emitting files.
* @default lf
*/
"new-line"?: "crlf" | "lf";
/**
* Omit unreachable types.
* By default all types declared under the service namespace will be included. With this flag on only types references in an operation will be emitted.
*/
"omit-unreachable-types"?: boolean;
/**
* If the generated openapi types should have the `x-typespec-name` extension set with the name of the TypeSpec type that created it.
* This extension is meant for debugging and should not be depended on.
* @default "never"
*/
"include-x-typespec-name"?: "inline-only" | "never";
/**
* If the generated openapi enums should have the `x-enum-varnames` extension filled.
* This maintains the key of any enum value that defines it in the form `key: value`.
* The default behavior is to use the value as both the key and value.
* @default false
*/
"include-x-enum-varnames"?: boolean;
/**
* How to handle safeint type. Options are:
* - `double-int`: Will produce `type: integer, format: double-int`
* - `int64`: Will produce `type: integer, format: int64`
* @default "int64"
*/
"safeint-strategy"?: "double-int" | "int64";
/**
* If true, then for models emitted as object schemas we default `additionalProperties` to false for
* OpenAPI 3.0, and `unevaluatedProperties` to false for OpenAPI 3.1, if not explicitly specified elsewhere.
* @default false
*/
"seal-object-schemas"?: boolean;
/**
* Determines how to emit examples on parameters.
*
* Note: This is an experimental feature and may change in future versions.
* @see https://spec.openapis.org/oas/v3.0.4.html#style-examples for parameter example serialization rules.
* @see https://github.com/OAI/OpenAPI-Specification/discussions/4622 for discussion on handling parameter examples.
*/
"experimental-parameter-examples"?: ExperimentalParameterExamplesStrategy;
/**
* How should operation ID be generated when `@operationId` is not used.
* Available options are
* - `parent-container`: Uses the parent namespace/interface and operation name to generate the ID.
* - `fqn`: Uses the fully qualified name(from service root) of the operation to generate the ID.
* - `explicit-only`: Only use explicitly defined operation IDs.
* @default parent-container
*/
"operation-id-strategy"?:
| OperationIdStrategy
| {
/** Strategy used to generate the operation ID. */
kind: OperationIdStrategy;
/** Separator used to join segment in the operation name. */
separator?: string;
};
}
export type OperationIdStrategy = "parent-container" | "fqn" | "explicit-only";
const operationIdStrategySchema = {
type: "string",
enum: ["parent-container", "fqn", "explicit-only"],
default: "parent-container",
description: [
"Determines how to generate operation IDs when `@operationId` is not used.",
"Avaliable options are:",
" - `parent-container`: Uses the parent namespace and operation name to generate the ID.",
" - `fqn`: Uses the fully qualified name of the operation to generate the ID.",
" - `explicit-only`: Only use explicitly defined operation IDs.",
].join("\n"),
} as const;
const EmitterOptionsSchema: JSONSchemaType<OpenAPI3EmitterOptions> = {
type: "object",
additionalProperties: false,
properties: {
"file-type": {
type: ["string", "array"],
nullable: true,
oneOf: [
{
type: "string",
enum: ["yaml", "json"],
},
{
type: "array",
items: {
type: "string",
enum: ["yaml", "json"],
},
uniqueItems: true,
minItems: 1,
},
],
description:
"If the content should be serialized as YAML or JSON. Can be a single value or an array to emit multiple formats. Default 'yaml', if not specified infer from the `output-file` extension",
},
"output-file": {
type: "string",
nullable: true,
description: [
"Name of the output file.",
" Output file will interpolate the following values:",
" - service-name: Name of the service",
" - service-name-if-multiple: Name of the service if multiple",
" - version: Version of the service if multiple",
" - file-type: The file type being emitted (json or yaml). Useful when `file-type` is an array.",
"",
' Default: `{service-name-if-multiple}.{version}.openapi.yaml` or `.json` if `file-type` is `"json"`',
" When `file-type` is an array: `{service-name-if-multiple}.{version}.openapi.{file-type}`",
"",
" Example Single service no versioning",
" - `openapi.yaml`",
"",
" Example Multiple services no versioning",
" - `openapi.Org1.Service1.yaml`",
" - `openapi.Org1.Service2.yaml`",
"",
" Example Single service with versioning",
" - `openapi.v1.yaml`",
" - `openapi.v2.yaml`",
"",
" Example Multiple service with versioning",
" - `openapi.Org1.Service1.v1.yaml`",
" - `openapi.Org1.Service1.v2.yaml`",
" - `openapi.Org1.Service2.v1.0.yaml`",
" - `openapi.Org1.Service2.v1.1.yaml` ",
].join("\n"),
},
"openapi-versions": {
title: "OpenAPI Versions",
type: "array",
items: {
type: "string",
enum: ["3.0.0", "3.1.0", "3.2.0"],
nullable: true,
description: "The versions of OpenAPI to emit. Defaults to `[3.0.0]`",
},
nullable: true,
uniqueItems: true,
minItems: 1,
default: ["3.0.0"],
},
"new-line": {
type: "string",
enum: ["crlf", "lf"],
default: "lf",
nullable: true,
description: "Set the newline character for emitting files.",
},
"omit-unreachable-types": {
type: "boolean",
nullable: true,
description:
"Omit unreachable types.\nBy default all types declared under the service namespace will be included. With this flag on only types references in an operation will be emitted.",
},
"include-x-typespec-name": {
type: "string",
enum: ["inline-only", "never"],
nullable: true,
default: "never",
description:
"If the generated openapi types should have the `x-typespec-name` extension set with the name of the TypeSpec type that created it.\nThis extension is meant for debugging and should not be depended on.",
},
"include-x-enum-varnames": {
type: "boolean",
nullable: true,
default: false,
description:
"If the generated openapi enums should have the `x-enum-varnames` extension filled.\nThis maintains the key of any enum value that defines it in the form `key: value`.\nThe default behavior is to use the value as both the key and value.",
},
"safeint-strategy": {
type: "string",
enum: ["double-int", "int64"],
nullable: true,
default: "int64",
description: [
"How to handle safeint type. Options are:",
" - `double-int`: Will produce `type: integer, format: double-int`",
" - `int64`: Will produce `type: integer, format: int64`",
"",
"Default: `int64`",
].join("\n"),
},
"seal-object-schemas": {
type: "boolean",
nullable: true,
default: false,
description: [
"If true, then for models emitted as object schemas we default `additionalProperties` to false for",
"OpenAPI 3.0, and `unevaluatedProperties` to false for OpenAPI 3.1, if not explicitly specified elsewhere.",
"Default: `false`",
].join("\n"),
},
"experimental-parameter-examples": {
type: "string",
enum: ["data", "serialized"],
nullable: true,
description: [
"Determines how to emit examples on parameters.",
"Note: This is an experimental feature and may change in future versions.",
"See https://spec.openapis.org/oas/v3.0.4.html#style-examples for parameter example serialization rules",
"See https://github.com/OAI/OpenAPI-Specification/discussions/4622 for discussion on handling parameter examples.",
].join("\n"),
},
"operation-id-strategy": {
oneOf: [
operationIdStrategySchema,
{
type: "object",
properties: {
kind: operationIdStrategySchema,
separator: {
type: "string",
nullable: true,
description: "Separator used to join segment in the operation name.",
},
},
required: ["kind"],
},
],
} as any,
},
required: [],
};
export const $lib = createTypeSpecLibrary({
name: "@typespec/openapi3",
capabilities: {
dryRun: true,
},
diagnostics: {
"oneof-union": {
severity: "error",
messages: {
default:
"@oneOf decorator can only be used on a union or a model property which type is a union.",
},
},
"inconsistent-shared-route-request-visibility": {
severity: "error",
messages: {
default: "All operations with `@sharedRoutes` must have the same `@requestVisibility`.",
},
},
"invalid-server-variable": {
severity: "error",
messages: {
default: paramMessage`Server variable '${"propName"}' must be assignable to 'string'. It must either be a string, enum of string or union of strings.`,
},
},
"invalid-format": {
severity: "warning",
messages: {
default: paramMessage`Collection format '${"value"}' is not supported in OpenAPI3 ${"paramType"} parameters. Defaulting to type 'string'.`,
},
},
"invalid-style": {
severity: "warning",
messages: {
default: paramMessage`Style '${"style"}' is not supported in OpenAPI3 ${"paramType"} parameters. Defaulting to style 'simple'.`,
optionalPath: paramMessage`Style '${"style"}' is not supported in OpenAPI3 ${"paramType"} parameters. The style ${"style"} could be introduced by an optional parameter. Defaulting to style 'simple'.`,
},
},
"path-reserved-expansion": {
severity: "warning",
messages: {
default: `Reserved expansion of path parameter with '+' operator #{allowReserved: true} is not supported in OpenAPI3.`,
},
},
"resource-namespace": {
severity: "error",
messages: {
default: "Resource goes on namespace",
},
},
"path-query": {
severity: "error",
messages: {
default: `OpenAPI does not allow paths containing a query string.`,
},
},
"duplicate-header": {
severity: "error",
messages: {
default: paramMessage`The header ${"header"} is defined across multiple content types`,
},
},
"status-code-in-default-response": {
severity: "error",
messages: {
default: "a default response should not have an explicit status code",
},
},
"invalid-schema": {
severity: "error",
messages: {
default: paramMessage`Couldn't get schema for type ${"type"}`,
},
},
"union-null": {
severity: "error",
messages: {
default: "Cannot have a union containing only null types.",
},
},
"empty-union": {
severity: "error",
messages: {
default:
"Empty unions are not supported for OpenAPI v3 - enums must have at least one value.",
},
},
"empty-enum": {
severity: "error",
messages: {
default:
"Empty enums are not supported for OpenAPI v3 - enums must have at least one value.",
},
},
"enum-unique-type": {
severity: "error",
messages: {
default: "Enums are not supported unless all options are literals of the same type.",
},
},
"inline-cycle": {
severity: "error",
messages: {
default: paramMessage`Cycle detected in '${"type"}'. Use @friendlyName decorator to assign an OpenAPI definition name and make it non-inline.`,
},
},
"unsupported-status-code-range": {
severity: "error",
messages: {
default: paramMessage`Status code range '${"start"} to '${"end"}' is not supported. OpenAPI 3.0 can only represent range 1XX, 2XX, 3XX, 4XX and 5XX. Example: \`@minValue(400) @maxValue(499)\` for 4XX.`,
},
},
"invalid-model-property": {
severity: "error",
messages: {
default: paramMessage`'${"type"}' cannot be specified as a model property.`,
},
},
"unsupported-auth": {
severity: "warning",
messages: {
default: paramMessage`Authentication "${"authType"}" is not a known authentication by the openapi3 emitter, it will be ignored.`,
},
},
"xml-attribute-invalid-property-type": {
severity: "warning",
messages: {
default: paramMessage`XML \`@attribute\` can only be primitive types in the OpenAPI 3 emitter, Property '${"name"}' type will be changed to type: string.`,
},
},
"xml-unwrapped-invalid-property-type": {
severity: "warning",
messages: {
default: paramMessage`XML \`@unwrapped\` can only used on array properties or primitive ones in the OpenAPI 3 emitter, Property '${"name"}' will be ignored.`,
},
},
"invalid-component-fixed-field-key": {
severity: "warning",
messages: {
default: paramMessage`Invalid key '${"value"}' used in a fixed field of the Component object. Only alphanumerics, dot (.), hyphen (-), and underscore (_) characters are allowed in keys.`,
},
},
"streams-not-supported": {
severity: "warning",
messages: {
default:
"Streams with itemSchema are only fully supported in OpenAPI 3.2.0 or above. The response will be emitted without itemSchema. Consider using OpenAPI 3.2.0 for full stream support.",
},
},
"default-not-supported": {
severity: "warning",
messages: {
default: paramMessage`Default value is not supported in OpenAPI 3.0 ${"message"}`,
},
},
},
emitter: {
options: EmitterOptionsSchema as JSONSchemaType<OpenAPI3EmitterOptions>,
},
});
export const { createDiagnostic, reportDiagnostic, createStateSymbol } = $lib;
export type OpenAPILibrary = typeof $lib;