@typespec/openapi3 0.51.0-dev.1 → 0.51.0-dev.10
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/src/openapi.d.ts.map +1 -1
- package/dist/src/openapi.js +86 -536
- package/dist/src/openapi.js.map +1 -1
- package/dist/src/schema-emitter.d.ts +49 -0
- package/dist/src/schema-emitter.d.ts.map +1 -0
- package/dist/src/schema-emitter.js +714 -0
- package/dist/src/schema-emitter.js.map +1 -0
- package/dist/src/visibility-usage.d.ts +9 -0
- package/dist/src/visibility-usage.d.ts.map +1 -0
- package/dist/src/visibility-usage.js +127 -0
- package/dist/src/visibility-usage.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _OpenAPI3SchemaEmitter_instances, _OpenAPI3SchemaEmitter_metadataInfo, _OpenAPI3SchemaEmitter_visibilityUsage, _OpenAPI3SchemaEmitter_options, _OpenAPI3SchemaEmitter_applyExternalDocs, _OpenAPI3SchemaEmitter_typeNameOptions, _OpenAPI3SchemaEmitter_getVisibilityContext, _OpenAPI3SchemaEmitter_getContentType, _OpenAPI3SchemaEmitter_requiredModelProperties, _OpenAPI3SchemaEmitter_enumSchema, _OpenAPI3SchemaEmitter_unionSchema, _OpenAPI3SchemaEmitter_getDiscriminatorMapping, _OpenAPI3SchemaEmitter_attachExtensions, _OpenAPI3SchemaEmitter_getDefaultValue, _OpenAPI3SchemaEmitter_getSchemaForScalar, _OpenAPI3SchemaEmitter_getSchemaForStdScalars, _OpenAPI3SchemaEmitter_applyConstraints, _OpenAPI3SchemaEmitter_inlineType, _OpenAPI3SchemaEmitter_createDeclaration, _OpenAPI3SchemaEmitter_isStdType, _OpenAPI3SchemaEmitter_applyEncoding, _OpenAPI3SchemaEmitter_mergeFormatAndEncoding;
|
|
13
|
+
import { compilerAssert, getDeprecated, getDiscriminatedUnion, getDiscriminator, getDoc, getEncode, getFormat, getKnownValues, getMaxItems, getMaxLength, getMaxValue, getMaxValueExclusive, getMinItems, getMinLength, getMinValue, getMinValueExclusive, getNamespaceFullName, getPattern, getSummary, getTypeName, ignoreDiagnostics, isArrayModelType, isNeverType, isNullType, isSecret, isTemplateDeclaration, stringTemplateToString, } from "@typespec/compiler";
|
|
14
|
+
import { ArrayBuilder, ObjectBuilder, Placeholder, TypeEmitter, } from "@typespec/compiler/emitter-framework";
|
|
15
|
+
import { getVisibilitySuffix, Visibility } from "@typespec/http";
|
|
16
|
+
import { checkDuplicateTypeName, getExtensions, getExternalDocs, getOpenAPITypeName, isReadonlyProperty, shouldInline, } from "@typespec/openapi";
|
|
17
|
+
import { getOneOf, getRef } from "./decorators.js";
|
|
18
|
+
import { reportDiagnostic } from "./lib.js";
|
|
19
|
+
/**
|
|
20
|
+
* OpenAPI3 schema emitter. Deals with emitting content of `components/schemas` section.
|
|
21
|
+
*/
|
|
22
|
+
export class OpenAPI3SchemaEmitter extends TypeEmitter {
|
|
23
|
+
constructor(emitter, metadataInfo, visibilityUsage, options) {
|
|
24
|
+
super(emitter);
|
|
25
|
+
_OpenAPI3SchemaEmitter_instances.add(this);
|
|
26
|
+
_OpenAPI3SchemaEmitter_metadataInfo.set(this, void 0);
|
|
27
|
+
_OpenAPI3SchemaEmitter_visibilityUsage.set(this, void 0);
|
|
28
|
+
_OpenAPI3SchemaEmitter_options.set(this, void 0);
|
|
29
|
+
__classPrivateFieldSet(this, _OpenAPI3SchemaEmitter_metadataInfo, metadataInfo, "f");
|
|
30
|
+
__classPrivateFieldSet(this, _OpenAPI3SchemaEmitter_visibilityUsage, visibilityUsage, "f");
|
|
31
|
+
__classPrivateFieldSet(this, _OpenAPI3SchemaEmitter_options, options, "f");
|
|
32
|
+
}
|
|
33
|
+
modelDeclarationReferenceContext(model, name) {
|
|
34
|
+
return this.reduceContext(model);
|
|
35
|
+
}
|
|
36
|
+
modelLiteralReferenceContext(model) {
|
|
37
|
+
return this.reduceContext(model);
|
|
38
|
+
}
|
|
39
|
+
scalarDeclarationReferenceContext(scalar, name) {
|
|
40
|
+
return this.reduceContext(scalar);
|
|
41
|
+
}
|
|
42
|
+
enumDeclarationReferenceContext(en, name) {
|
|
43
|
+
return this.reduceContext(en);
|
|
44
|
+
}
|
|
45
|
+
unionDeclarationReferenceContext(union) {
|
|
46
|
+
return this.reduceContext(union);
|
|
47
|
+
}
|
|
48
|
+
reduceContext(type) {
|
|
49
|
+
const visibility = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getVisibilityContext).call(this);
|
|
50
|
+
const patch = {};
|
|
51
|
+
if (visibility !== Visibility.Read && !__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_metadataInfo, "f").isTransformed(type, visibility)) {
|
|
52
|
+
patch.visibility = Visibility.Read;
|
|
53
|
+
}
|
|
54
|
+
const contentType = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getContentType).call(this);
|
|
55
|
+
if (contentType === "application/json") {
|
|
56
|
+
delete patch.contentType;
|
|
57
|
+
}
|
|
58
|
+
return patch;
|
|
59
|
+
}
|
|
60
|
+
modelDeclaration(model, _) {
|
|
61
|
+
const program = this.emitter.getProgram();
|
|
62
|
+
const visibility = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getVisibilityContext).call(this);
|
|
63
|
+
const schema = new ObjectBuilder({
|
|
64
|
+
type: "object",
|
|
65
|
+
required: __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_requiredModelProperties).call(this, model, visibility),
|
|
66
|
+
properties: this.emitter.emitModelProperties(model),
|
|
67
|
+
});
|
|
68
|
+
if (model.indexer) {
|
|
69
|
+
schema.set("additionalProperties", this.emitter.emitTypeReference(model.indexer.value));
|
|
70
|
+
}
|
|
71
|
+
const derivedModels = model.derivedModels.filter(includeDerivedModel);
|
|
72
|
+
// getSchemaOrRef on all children to push them into components.schemas
|
|
73
|
+
for (const child of derivedModels) {
|
|
74
|
+
this.emitter.emitTypeReference(child);
|
|
75
|
+
}
|
|
76
|
+
const discriminator = getDiscriminator(program, model);
|
|
77
|
+
if (discriminator) {
|
|
78
|
+
const [union] = getDiscriminatedUnion(model, discriminator);
|
|
79
|
+
const openApiDiscriminator = { ...discriminator };
|
|
80
|
+
if (union.variants.size > 0) {
|
|
81
|
+
openApiDiscriminator.mapping = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDiscriminatorMapping).call(this, union);
|
|
82
|
+
}
|
|
83
|
+
schema.discriminator = openApiDiscriminator;
|
|
84
|
+
}
|
|
85
|
+
__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyExternalDocs).call(this, model, schema);
|
|
86
|
+
if (model.baseModel) {
|
|
87
|
+
schema.set("allOf", B.array([this.emitter.emitTypeReference(model.baseModel)]));
|
|
88
|
+
}
|
|
89
|
+
const baseName = getOpenAPITypeName(program, model, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_typeNameOptions).call(this));
|
|
90
|
+
const isMultipart = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getContentType).call(this).startsWith("multipart/");
|
|
91
|
+
const name = isMultipart ? baseName + "MultiPart" : baseName;
|
|
92
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_createDeclaration).call(this, model, name, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, model, schema));
|
|
93
|
+
}
|
|
94
|
+
modelLiteral(model) {
|
|
95
|
+
const schema = new ObjectBuilder({
|
|
96
|
+
type: "object",
|
|
97
|
+
properties: this.emitter.emitModelProperties(model),
|
|
98
|
+
required: __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_requiredModelProperties).call(this, model, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getVisibilityContext).call(this)),
|
|
99
|
+
});
|
|
100
|
+
if (model.indexer) {
|
|
101
|
+
schema.set("additionalProperties", this.emitter.emitTypeReference(model.indexer.value));
|
|
102
|
+
}
|
|
103
|
+
return schema;
|
|
104
|
+
}
|
|
105
|
+
modelInstantiation(model, name) {
|
|
106
|
+
name = name !== null && name !== void 0 ? name : getOpenAPITypeName(this.emitter.getProgram(), model, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_typeNameOptions).call(this));
|
|
107
|
+
if (!name) {
|
|
108
|
+
return this.modelLiteral(model);
|
|
109
|
+
}
|
|
110
|
+
return this.modelDeclaration(model, name);
|
|
111
|
+
}
|
|
112
|
+
arrayDeclaration(array, name, elementType) {
|
|
113
|
+
const schema = new ObjectBuilder({
|
|
114
|
+
type: "array",
|
|
115
|
+
items: this.emitter.emitTypeReference(elementType),
|
|
116
|
+
});
|
|
117
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_createDeclaration).call(this, array, name, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, array, schema));
|
|
118
|
+
}
|
|
119
|
+
arrayDeclarationReferenceContext(array, name, elementType) {
|
|
120
|
+
return {
|
|
121
|
+
visibility: __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getVisibilityContext).call(this) | Visibility.Item,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
arrayLiteral(array, elementType) {
|
|
125
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_inlineType).call(this, array, new ObjectBuilder({
|
|
126
|
+
type: "array",
|
|
127
|
+
items: this.emitter.emitTypeReference(elementType),
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
arrayLiteralReferenceContext(array, elementType) {
|
|
131
|
+
return {
|
|
132
|
+
visibility: __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getVisibilityContext).call(this) | Visibility.Item,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
modelProperties(model) {
|
|
136
|
+
const props = new ObjectBuilder();
|
|
137
|
+
const visibility = this.emitter.getContext().visibility;
|
|
138
|
+
for (const [name, prop] of model.properties) {
|
|
139
|
+
if (isNeverType(prop.type)) {
|
|
140
|
+
// If the property has a type of 'never', don't include it in the schema
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (!__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_metadataInfo, "f").isPayloadProperty(prop, visibility)) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const result = this.emitter.emitModelProperty(prop);
|
|
147
|
+
props.set(name, result);
|
|
148
|
+
}
|
|
149
|
+
const discriminator = getDiscriminator(this.emitter.getProgram(), model);
|
|
150
|
+
if (discriminator && !(discriminator.propertyName in props)) {
|
|
151
|
+
props.set(discriminator.propertyName, {
|
|
152
|
+
type: "string",
|
|
153
|
+
description: `Discriminator property for ${model.name}.`,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
if (Object.keys(props).length === 0) {
|
|
157
|
+
return this.emitter.result.none();
|
|
158
|
+
}
|
|
159
|
+
return props;
|
|
160
|
+
}
|
|
161
|
+
modelPropertyLiteral(prop) {
|
|
162
|
+
const program = this.emitter.getProgram();
|
|
163
|
+
const isMultipart = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getContentType).call(this).startsWith("multipart/");
|
|
164
|
+
if (isMultipart &&
|
|
165
|
+
prop.type.kind === "Scalar" &&
|
|
166
|
+
prop.type.name === "bytes" &&
|
|
167
|
+
getEncode(program, prop.type) === undefined &&
|
|
168
|
+
getEncode(program, prop) === undefined) {
|
|
169
|
+
return { type: "string", format: "binary" };
|
|
170
|
+
}
|
|
171
|
+
const refSchema = this.emitter.emitTypeReference(prop.type, {
|
|
172
|
+
referenceContext: isMultipart ? { contentType: "application/json" } : {},
|
|
173
|
+
});
|
|
174
|
+
if (refSchema.kind !== "code") {
|
|
175
|
+
throw new Error("Unexpected non-code result from emit reference");
|
|
176
|
+
}
|
|
177
|
+
const isRef = refSchema.value instanceof Placeholder || "$ref" in refSchema.value;
|
|
178
|
+
const schema = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyEncoding).call(this, prop, refSchema.value);
|
|
179
|
+
// Apply decorators on the property to the type's schema
|
|
180
|
+
const additionalProps = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, prop, {});
|
|
181
|
+
if (prop.default) {
|
|
182
|
+
additionalProps.default = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDefaultValue).call(this, prop.type, prop.default);
|
|
183
|
+
}
|
|
184
|
+
if (isReadonlyProperty(program, prop)) {
|
|
185
|
+
additionalProps.readOnly = true;
|
|
186
|
+
}
|
|
187
|
+
// Attach any additional OpenAPI extensions
|
|
188
|
+
__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_attachExtensions).call(this, program, prop, additionalProps);
|
|
189
|
+
if (schema && isRef) {
|
|
190
|
+
if (Object.keys(additionalProps).length === 0) {
|
|
191
|
+
return schema;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
return {
|
|
195
|
+
allOf: [schema],
|
|
196
|
+
...additionalProps,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
if (getOneOf(program, prop) && schema.anyOf) {
|
|
202
|
+
schema.oneOf = schema.anyOf;
|
|
203
|
+
delete schema.anyOf;
|
|
204
|
+
}
|
|
205
|
+
const merged = new ObjectBuilder(schema);
|
|
206
|
+
for (const [key, value] of Object.entries(additionalProps)) {
|
|
207
|
+
merged.set(key, value);
|
|
208
|
+
}
|
|
209
|
+
return merged;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
booleanLiteral(boolean) {
|
|
213
|
+
return { type: "boolean", enum: [boolean.value] };
|
|
214
|
+
}
|
|
215
|
+
stringLiteral(string) {
|
|
216
|
+
return { type: "string", enum: [string.value] };
|
|
217
|
+
}
|
|
218
|
+
stringTemplate(string) {
|
|
219
|
+
const [value, diagnostics] = stringTemplateToString(string);
|
|
220
|
+
if (diagnostics.length > 0) {
|
|
221
|
+
this.emitter
|
|
222
|
+
.getProgram()
|
|
223
|
+
.reportDiagnostics(diagnostics.map((x) => ({ ...x, severity: "warning" })));
|
|
224
|
+
return { type: "string" };
|
|
225
|
+
}
|
|
226
|
+
return { type: "string", enum: [value] };
|
|
227
|
+
}
|
|
228
|
+
numericLiteral(number) {
|
|
229
|
+
return { type: "number", enum: [number.value] };
|
|
230
|
+
}
|
|
231
|
+
enumDeclaration(en, name) {
|
|
232
|
+
const baseName = getOpenAPITypeName(this.emitter.getProgram(), en, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_typeNameOptions).call(this));
|
|
233
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_createDeclaration).call(this, en, baseName, new ObjectBuilder(__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_enumSchema).call(this, en)));
|
|
234
|
+
}
|
|
235
|
+
enumMember(member) {
|
|
236
|
+
return this.enumMemberReference(member);
|
|
237
|
+
}
|
|
238
|
+
enumMemberReference(member) {
|
|
239
|
+
// would like to dispatch to the same `literal` codepaths but enum members aren't literal types
|
|
240
|
+
switch (typeof member.value) {
|
|
241
|
+
case "undefined":
|
|
242
|
+
return { type: "string", enum: [member.name] };
|
|
243
|
+
case "string":
|
|
244
|
+
return { type: "string", enum: [member.value] };
|
|
245
|
+
case "number":
|
|
246
|
+
return { type: "number", enum: [member.value] };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
unionDeclaration(union, name) {
|
|
250
|
+
const schema = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_unionSchema).call(this, union);
|
|
251
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_createDeclaration).call(this, union, name, schema);
|
|
252
|
+
}
|
|
253
|
+
unionLiteral(union) {
|
|
254
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_unionSchema).call(this, union);
|
|
255
|
+
}
|
|
256
|
+
unionVariants(union) {
|
|
257
|
+
const variants = new ArrayBuilder();
|
|
258
|
+
for (const variant of union.variants.values()) {
|
|
259
|
+
variants.push(this.emitter.emitType(variant));
|
|
260
|
+
}
|
|
261
|
+
return variants;
|
|
262
|
+
}
|
|
263
|
+
unionVariant(variant) {
|
|
264
|
+
return this.emitter.emitTypeReference(variant.type);
|
|
265
|
+
}
|
|
266
|
+
modelPropertyReference(prop) {
|
|
267
|
+
return this.modelPropertyLiteral(prop);
|
|
268
|
+
}
|
|
269
|
+
reference(targetDeclaration, pathUp, pathDown, commonScope) {
|
|
270
|
+
if (targetDeclaration.value instanceof Placeholder) {
|
|
271
|
+
// I don't think this is possible, confirm.
|
|
272
|
+
throw new Error("Can't form reference to declaration that hasn't been created yet");
|
|
273
|
+
}
|
|
274
|
+
// these will be undefined when creating a self-reference
|
|
275
|
+
const currentSfScope = pathUp[pathUp.length - 1];
|
|
276
|
+
const targetSfScope = pathDown[0];
|
|
277
|
+
if (targetSfScope && currentSfScope && !targetSfScope.sourceFile.meta.shouldEmit) {
|
|
278
|
+
currentSfScope.sourceFile.meta.bundledRefs.push(targetDeclaration);
|
|
279
|
+
}
|
|
280
|
+
return { $ref: `#/components/schemas/${targetDeclaration.name}` };
|
|
281
|
+
}
|
|
282
|
+
circularReference(target, scope, cycle) {
|
|
283
|
+
if (!cycle.containsDeclaration) {
|
|
284
|
+
reportDiagnostic(this.emitter.getProgram(), {
|
|
285
|
+
code: "inline-cycle",
|
|
286
|
+
format: {
|
|
287
|
+
type: cycle.toString(),
|
|
288
|
+
},
|
|
289
|
+
target: cycle.first.type,
|
|
290
|
+
});
|
|
291
|
+
return {};
|
|
292
|
+
}
|
|
293
|
+
return super.circularReference(target, scope, cycle);
|
|
294
|
+
}
|
|
295
|
+
scalarDeclaration(scalar, name) {
|
|
296
|
+
const isStd = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_isStdType).call(this, scalar);
|
|
297
|
+
const schema = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getSchemaForScalar).call(this, scalar);
|
|
298
|
+
const baseName = getOpenAPITypeName(this.emitter.getProgram(), scalar, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_typeNameOptions).call(this));
|
|
299
|
+
// Don't create a declaration for std types
|
|
300
|
+
return isStd ? schema : __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_createDeclaration).call(this, scalar, baseName, new ObjectBuilder(schema));
|
|
301
|
+
}
|
|
302
|
+
scalarInstantiation(scalar, name) {
|
|
303
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getSchemaForScalar).call(this, scalar);
|
|
304
|
+
}
|
|
305
|
+
tupleLiteral(tuple) {
|
|
306
|
+
return { type: "array", items: {} };
|
|
307
|
+
}
|
|
308
|
+
intrinsic(intrinsic, name) {
|
|
309
|
+
switch (name) {
|
|
310
|
+
case "unknown":
|
|
311
|
+
return {};
|
|
312
|
+
}
|
|
313
|
+
throw new Error("Unknown intrinsic type " + name);
|
|
314
|
+
}
|
|
315
|
+
programContext(program) {
|
|
316
|
+
const sourceFile = this.emitter.createSourceFile("openapi");
|
|
317
|
+
return { scope: sourceFile.globalScope };
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
_OpenAPI3SchemaEmitter_metadataInfo = new WeakMap(), _OpenAPI3SchemaEmitter_visibilityUsage = new WeakMap(), _OpenAPI3SchemaEmitter_options = new WeakMap(), _OpenAPI3SchemaEmitter_instances = new WeakSet(), _OpenAPI3SchemaEmitter_applyExternalDocs = function _OpenAPI3SchemaEmitter_applyExternalDocs(typespecType, target) {
|
|
321
|
+
const externalDocs = getExternalDocs(this.emitter.getProgram(), typespecType);
|
|
322
|
+
if (externalDocs) {
|
|
323
|
+
target.externalDocs = externalDocs;
|
|
324
|
+
}
|
|
325
|
+
}, _OpenAPI3SchemaEmitter_typeNameOptions = function _OpenAPI3SchemaEmitter_typeNameOptions() {
|
|
326
|
+
const serviceNamespaceName = this.emitter.getContext().serviceNamespaceName;
|
|
327
|
+
return {
|
|
328
|
+
// shorten type names by removing TypeSpec and service namespace
|
|
329
|
+
namespaceFilter(ns) {
|
|
330
|
+
const name = getNamespaceFullName(ns);
|
|
331
|
+
return name !== serviceNamespaceName;
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}, _OpenAPI3SchemaEmitter_getVisibilityContext = function _OpenAPI3SchemaEmitter_getVisibilityContext() {
|
|
335
|
+
var _a;
|
|
336
|
+
return (_a = this.emitter.getContext().visibility) !== null && _a !== void 0 ? _a : Visibility.Read;
|
|
337
|
+
}, _OpenAPI3SchemaEmitter_getContentType = function _OpenAPI3SchemaEmitter_getContentType() {
|
|
338
|
+
var _a;
|
|
339
|
+
return (_a = this.emitter.getContext().contentType) !== null && _a !== void 0 ? _a : "application/json";
|
|
340
|
+
}, _OpenAPI3SchemaEmitter_requiredModelProperties = function _OpenAPI3SchemaEmitter_requiredModelProperties(model, visibility) {
|
|
341
|
+
const requiredProps = [];
|
|
342
|
+
for (const prop of model.properties.values()) {
|
|
343
|
+
if (isNeverType(prop.type)) {
|
|
344
|
+
// If the property has a type of 'never', don't include it in the schema
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (!__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_metadataInfo, "f").isPayloadProperty(prop, visibility)) {
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (!__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_metadataInfo, "f").isOptional(prop, visibility)) {
|
|
351
|
+
requiredProps.push(prop.name);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return requiredProps.length > 0 ? requiredProps : undefined;
|
|
355
|
+
}, _OpenAPI3SchemaEmitter_enumSchema = function _OpenAPI3SchemaEmitter_enumSchema(en) {
|
|
356
|
+
var _a;
|
|
357
|
+
const program = this.emitter.getProgram();
|
|
358
|
+
if (en.members.size === 0) {
|
|
359
|
+
reportDiagnostic(program, { code: "empty-enum", target: en });
|
|
360
|
+
return {};
|
|
361
|
+
}
|
|
362
|
+
const enumTypes = new Set();
|
|
363
|
+
const enumValues = new Set();
|
|
364
|
+
for (const member of en.members.values()) {
|
|
365
|
+
enumTypes.add(typeof member.value === "number" ? "number" : "string");
|
|
366
|
+
enumValues.add((_a = member.value) !== null && _a !== void 0 ? _a : member.name);
|
|
367
|
+
}
|
|
368
|
+
if (enumTypes.size > 1) {
|
|
369
|
+
reportDiagnostic(program, { code: "enum-unique-type", target: en });
|
|
370
|
+
}
|
|
371
|
+
const schema = { type: enumTypes.values().next().value, enum: [...enumValues] };
|
|
372
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, en, schema);
|
|
373
|
+
}, _OpenAPI3SchemaEmitter_unionSchema = function _OpenAPI3SchemaEmitter_unionSchema(union) {
|
|
374
|
+
const program = this.emitter.getProgram();
|
|
375
|
+
if (union.variants.size === 0) {
|
|
376
|
+
reportDiagnostic(program, { code: "empty-union", target: union });
|
|
377
|
+
return {};
|
|
378
|
+
}
|
|
379
|
+
const variants = Array.from(union.variants.values());
|
|
380
|
+
const literalVariantEnumByType = {};
|
|
381
|
+
const ofType = getOneOf(program, union) ? "oneOf" : "anyOf";
|
|
382
|
+
const schemaMembers = [];
|
|
383
|
+
let nullable = false;
|
|
384
|
+
const discriminator = getDiscriminator(program, union);
|
|
385
|
+
for (const variant of variants) {
|
|
386
|
+
if (isNullType(variant.type)) {
|
|
387
|
+
nullable = true;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
if (isLiteralType(variant.type)) {
|
|
391
|
+
if (!literalVariantEnumByType[variant.type.kind]) {
|
|
392
|
+
const enumSchema = this.emitter.emitTypeReference(variant.type);
|
|
393
|
+
compilerAssert(enumSchema.kind === "code", "Unexpected enum schema. Should be kind: code");
|
|
394
|
+
literalVariantEnumByType[variant.type.kind] = enumSchema.value;
|
|
395
|
+
schemaMembers.push({ schema: enumSchema.value, type: null });
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
literalVariantEnumByType[variant.type.kind].enum.push(variant.type.value);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
const enumSchema = this.emitter.emitTypeReference(variant.type);
|
|
403
|
+
compilerAssert(enumSchema.kind === "code", "Unexpected enum schema. Should be kind: code");
|
|
404
|
+
schemaMembers.push({ schema: enumSchema.value, type: variant.type });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (schemaMembers.length === 0) {
|
|
408
|
+
if (nullable) {
|
|
409
|
+
// This union is equivalent to just `null` but OA3 has no way to specify
|
|
410
|
+
// null as a value, so we throw an error.
|
|
411
|
+
reportDiagnostic(program, { code: "union-null", target: union });
|
|
412
|
+
return {};
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
// completely empty union can maybe only happen with bugs?
|
|
416
|
+
compilerAssert(false, "Attempting to emit an empty union");
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (schemaMembers.length === 1) {
|
|
420
|
+
// we can just return the single schema member after applying nullable
|
|
421
|
+
let schema = schemaMembers[0].schema;
|
|
422
|
+
const type = schemaMembers[0].type;
|
|
423
|
+
if (nullable) {
|
|
424
|
+
if (schema instanceof Placeholder || "$ref" in schema) {
|
|
425
|
+
// but we can't make a ref "nullable", so wrap in an allOf (for models)
|
|
426
|
+
// or oneOf (for all other types)
|
|
427
|
+
if (type && type.kind === "Model") {
|
|
428
|
+
return B.object({ type: "object", allOf: B.array([schema]), nullable: true });
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
return B.object({ oneOf: B.array([schema]), nullable: true });
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
schema = { ...schema, nullable: true };
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return schema;
|
|
439
|
+
}
|
|
440
|
+
const schema = {
|
|
441
|
+
[ofType]: schemaMembers.map((m) => m.schema),
|
|
442
|
+
};
|
|
443
|
+
if (nullable) {
|
|
444
|
+
schema.nullable = true;
|
|
445
|
+
}
|
|
446
|
+
if (discriminator) {
|
|
447
|
+
// the decorator validates that all the variants will be a model type
|
|
448
|
+
// with the discriminator field present.
|
|
449
|
+
schema.discriminator = { ...discriminator };
|
|
450
|
+
// Diagnostic already reported in compiler for unions
|
|
451
|
+
const discriminatedUnion = ignoreDiagnostics(getDiscriminatedUnion(union, discriminator));
|
|
452
|
+
if (discriminatedUnion.variants.size > 0) {
|
|
453
|
+
schema.discriminator.mapping = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDiscriminatorMapping).call(this, discriminatedUnion);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, union, schema);
|
|
457
|
+
}, _OpenAPI3SchemaEmitter_getDiscriminatorMapping = function _OpenAPI3SchemaEmitter_getDiscriminatorMapping(union) {
|
|
458
|
+
const mapping = {};
|
|
459
|
+
for (const [key, model] of union.variants.entries()) {
|
|
460
|
+
const ref = this.emitter.emitTypeReference(model);
|
|
461
|
+
compilerAssert(ref.kind === "code", "Unexpected ref schema. Should be kind: code");
|
|
462
|
+
mapping[key] = ref.value.$ref;
|
|
463
|
+
}
|
|
464
|
+
return mapping;
|
|
465
|
+
}, _OpenAPI3SchemaEmitter_attachExtensions = function _OpenAPI3SchemaEmitter_attachExtensions(program, type, emitObject) {
|
|
466
|
+
// Attach any OpenAPI extensions
|
|
467
|
+
const extensions = getExtensions(program, type);
|
|
468
|
+
if (extensions) {
|
|
469
|
+
for (const key of extensions.keys()) {
|
|
470
|
+
emitObject[key] = extensions.get(key);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}, _OpenAPI3SchemaEmitter_getDefaultValue = function _OpenAPI3SchemaEmitter_getDefaultValue(type, defaultType) {
|
|
474
|
+
var _a;
|
|
475
|
+
const program = this.emitter.getProgram();
|
|
476
|
+
switch (defaultType.kind) {
|
|
477
|
+
case "String":
|
|
478
|
+
return defaultType.value;
|
|
479
|
+
case "Number":
|
|
480
|
+
return defaultType.value;
|
|
481
|
+
case "Boolean":
|
|
482
|
+
return defaultType.value;
|
|
483
|
+
case "Tuple":
|
|
484
|
+
compilerAssert(type.kind === "Tuple" || (type.kind === "Model" && isArrayModelType(program, type)), "setting tuple default to non-tuple value");
|
|
485
|
+
if (type.kind === "Tuple") {
|
|
486
|
+
return defaultType.values.map((defaultTupleValue, index) => __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDefaultValue).call(this, type.values[index], defaultTupleValue));
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
return defaultType.values.map((defaultTuplevalue) => __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDefaultValue).call(this, type.indexer.value, defaultTuplevalue));
|
|
490
|
+
}
|
|
491
|
+
case "Intrinsic":
|
|
492
|
+
return isNullType(defaultType)
|
|
493
|
+
? null
|
|
494
|
+
: reportDiagnostic(program, {
|
|
495
|
+
code: "invalid-default",
|
|
496
|
+
format: { type: defaultType.kind },
|
|
497
|
+
target: defaultType,
|
|
498
|
+
});
|
|
499
|
+
case "EnumMember":
|
|
500
|
+
return (_a = defaultType.value) !== null && _a !== void 0 ? _a : defaultType.name;
|
|
501
|
+
default:
|
|
502
|
+
reportDiagnostic(program, {
|
|
503
|
+
code: "invalid-default",
|
|
504
|
+
format: { type: defaultType.kind },
|
|
505
|
+
target: defaultType,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
}, _OpenAPI3SchemaEmitter_getSchemaForScalar = function _OpenAPI3SchemaEmitter_getSchemaForScalar(scalar) {
|
|
509
|
+
let result = {};
|
|
510
|
+
const isStd = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_isStdType).call(this, scalar);
|
|
511
|
+
if (isStd) {
|
|
512
|
+
result = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getSchemaForStdScalars).call(this, scalar);
|
|
513
|
+
}
|
|
514
|
+
else if (scalar.baseScalar) {
|
|
515
|
+
result = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getSchemaForScalar).call(this, scalar.baseScalar);
|
|
516
|
+
}
|
|
517
|
+
const withDecorators = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyEncoding).call(this, scalar, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, scalar, result));
|
|
518
|
+
if (isStd) {
|
|
519
|
+
// Standard types are going to be inlined in the spec and we don't want the description of the scalar to show up
|
|
520
|
+
delete withDecorators.description;
|
|
521
|
+
}
|
|
522
|
+
return withDecorators;
|
|
523
|
+
}, _OpenAPI3SchemaEmitter_getSchemaForStdScalars = function _OpenAPI3SchemaEmitter_getSchemaForStdScalars(scalar) {
|
|
524
|
+
switch (scalar.name) {
|
|
525
|
+
case "bytes":
|
|
526
|
+
return { type: "string", format: "byte" };
|
|
527
|
+
case "numeric":
|
|
528
|
+
return { type: "number" };
|
|
529
|
+
case "integer":
|
|
530
|
+
return { type: "integer" };
|
|
531
|
+
case "int8":
|
|
532
|
+
return { type: "integer", format: "int8" };
|
|
533
|
+
case "int16":
|
|
534
|
+
return { type: "integer", format: "int16" };
|
|
535
|
+
case "int32":
|
|
536
|
+
return { type: "integer", format: "int32" };
|
|
537
|
+
case "int64":
|
|
538
|
+
return { type: "integer", format: "int64" };
|
|
539
|
+
case "safeint":
|
|
540
|
+
return { type: "integer", format: "int64" };
|
|
541
|
+
case "uint8":
|
|
542
|
+
return { type: "integer", format: "uint8" };
|
|
543
|
+
case "uint16":
|
|
544
|
+
return { type: "integer", format: "uint16" };
|
|
545
|
+
case "uint32":
|
|
546
|
+
return { type: "integer", format: "uint32" };
|
|
547
|
+
case "uint64":
|
|
548
|
+
return { type: "integer", format: "uint64" };
|
|
549
|
+
case "float":
|
|
550
|
+
return { type: "number" };
|
|
551
|
+
case "float64":
|
|
552
|
+
return { type: "number", format: "double" };
|
|
553
|
+
case "float32":
|
|
554
|
+
return { type: "number", format: "float" };
|
|
555
|
+
case "decimal":
|
|
556
|
+
return { type: "number", format: "decimal" };
|
|
557
|
+
case "decimal128":
|
|
558
|
+
return { type: "number", format: "decimal128" };
|
|
559
|
+
case "string":
|
|
560
|
+
return { type: "string" };
|
|
561
|
+
case "boolean":
|
|
562
|
+
return { type: "boolean" };
|
|
563
|
+
case "plainDate":
|
|
564
|
+
return { type: "string", format: "date" };
|
|
565
|
+
case "utcDateTime":
|
|
566
|
+
case "offsetDateTime":
|
|
567
|
+
return { type: "string", format: "date-time" };
|
|
568
|
+
case "plainTime":
|
|
569
|
+
return { type: "string", format: "time" };
|
|
570
|
+
case "duration":
|
|
571
|
+
return { type: "string", format: "duration" };
|
|
572
|
+
case "url":
|
|
573
|
+
return { type: "string", format: "uri" };
|
|
574
|
+
default:
|
|
575
|
+
const _assertNever = scalar.name;
|
|
576
|
+
return {};
|
|
577
|
+
}
|
|
578
|
+
}, _OpenAPI3SchemaEmitter_applyConstraints = function _OpenAPI3SchemaEmitter_applyConstraints(type, original) {
|
|
579
|
+
const schema = new ObjectBuilder(original);
|
|
580
|
+
const program = this.emitter.getProgram();
|
|
581
|
+
const applyConstraint = (fn, key) => {
|
|
582
|
+
const value = fn(program, type);
|
|
583
|
+
if (value !== undefined) {
|
|
584
|
+
schema[key] = value;
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
applyConstraint(getMinLength, "minLength");
|
|
588
|
+
applyConstraint(getMaxLength, "maxLength");
|
|
589
|
+
applyConstraint(getMinValue, "minimum");
|
|
590
|
+
applyConstraint(getMaxValue, "maximum");
|
|
591
|
+
applyConstraint(getPattern, "pattern");
|
|
592
|
+
applyConstraint(getMinItems, "minItems");
|
|
593
|
+
applyConstraint(getMaxItems, "maxItems");
|
|
594
|
+
const minValueExclusive = getMinValueExclusive(program, type);
|
|
595
|
+
if (minValueExclusive !== undefined) {
|
|
596
|
+
schema.minimum = minValueExclusive;
|
|
597
|
+
schema.exclusiveMinimum = true;
|
|
598
|
+
}
|
|
599
|
+
const maxValueExclusive = getMaxValueExclusive(program, type);
|
|
600
|
+
if (maxValueExclusive !== undefined) {
|
|
601
|
+
schema.maximum = maxValueExclusive;
|
|
602
|
+
schema.exclusiveMaximum = true;
|
|
603
|
+
}
|
|
604
|
+
if (isSecret(program, type)) {
|
|
605
|
+
schema.format = "password";
|
|
606
|
+
}
|
|
607
|
+
// the stdlib applies a format of "url" but json schema wants "uri",
|
|
608
|
+
// so ignore this format if it's the built-in type.
|
|
609
|
+
if (!__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_isStdType).call(this, type) || type.name !== "url") {
|
|
610
|
+
applyConstraint(getFormat, "format");
|
|
611
|
+
}
|
|
612
|
+
applyConstraint(getDoc, "description");
|
|
613
|
+
applyConstraint(getSummary, "title");
|
|
614
|
+
applyConstraint((p, t) => (getDeprecated(p, t) !== undefined ? true : undefined), "deprecated");
|
|
615
|
+
__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_attachExtensions).call(this, program, type, schema);
|
|
616
|
+
const values = getKnownValues(program, type);
|
|
617
|
+
if (values) {
|
|
618
|
+
return new ObjectBuilder({
|
|
619
|
+
oneOf: [schema, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_enumSchema).call(this, values)],
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
return new ObjectBuilder(schema);
|
|
623
|
+
}, _OpenAPI3SchemaEmitter_inlineType = function _OpenAPI3SchemaEmitter_inlineType(type, schema) {
|
|
624
|
+
if (__classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_options, "f").includeXTypeSpecName !== "never") {
|
|
625
|
+
schema.set("x-typespec-name", getTypeName(type, __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_typeNameOptions).call(this)));
|
|
626
|
+
}
|
|
627
|
+
return schema;
|
|
628
|
+
}, _OpenAPI3SchemaEmitter_createDeclaration = function _OpenAPI3SchemaEmitter_createDeclaration(type, name, schema) {
|
|
629
|
+
const refUrl = getRef(this.emitter.getProgram(), type);
|
|
630
|
+
if (refUrl) {
|
|
631
|
+
return {
|
|
632
|
+
$ref: refUrl,
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
if (shouldInline(this.emitter.getProgram(), type)) {
|
|
636
|
+
return __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_inlineType).call(this, type, schema);
|
|
637
|
+
}
|
|
638
|
+
const title = getSummary(this.emitter.getProgram(), type);
|
|
639
|
+
if (title) {
|
|
640
|
+
schema.set("title", title);
|
|
641
|
+
}
|
|
642
|
+
const usage = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_visibilityUsage, "f").getUsage(type);
|
|
643
|
+
const shouldAddSuffix = usage !== undefined && usage.size > 1;
|
|
644
|
+
const visibility = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getVisibilityContext).call(this);
|
|
645
|
+
const fullName = name + (shouldAddSuffix ? getVisibilitySuffix(visibility, Visibility.Read) : "");
|
|
646
|
+
const decl = this.emitter.result.declaration(fullName, schema);
|
|
647
|
+
checkDuplicateTypeName(this.emitter.getProgram(), type, fullName, Object.fromEntries(decl.scope.declarations.map((x) => [x.name, true])));
|
|
648
|
+
return decl;
|
|
649
|
+
}, _OpenAPI3SchemaEmitter_isStdType = function _OpenAPI3SchemaEmitter_isStdType(type) {
|
|
650
|
+
return this.emitter.getProgram().checker.isStdType(type);
|
|
651
|
+
}, _OpenAPI3SchemaEmitter_applyEncoding = function _OpenAPI3SchemaEmitter_applyEncoding(typespecType, target) {
|
|
652
|
+
const encodeData = getEncode(this.emitter.getProgram(), typespecType);
|
|
653
|
+
if (encodeData) {
|
|
654
|
+
const newTarget = new ObjectBuilder(target);
|
|
655
|
+
const newType = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getSchemaForStdScalars).call(this, encodeData.type);
|
|
656
|
+
newTarget.type = newType.type;
|
|
657
|
+
// If the target already has a format it takes priority. (e.g. int32)
|
|
658
|
+
newTarget.format = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_mergeFormatAndEncoding).call(this, newTarget.format, encodeData.encoding, newType.format);
|
|
659
|
+
return newTarget;
|
|
660
|
+
}
|
|
661
|
+
return new ObjectBuilder(target);
|
|
662
|
+
}, _OpenAPI3SchemaEmitter_mergeFormatAndEncoding = function _OpenAPI3SchemaEmitter_mergeFormatAndEncoding(format, encoding, encodeAsFormat) {
|
|
663
|
+
switch (format) {
|
|
664
|
+
case undefined:
|
|
665
|
+
return encodeAsFormat !== null && encodeAsFormat !== void 0 ? encodeAsFormat : encoding;
|
|
666
|
+
case "date-time":
|
|
667
|
+
switch (encoding) {
|
|
668
|
+
case "rfc3339":
|
|
669
|
+
return "date-time";
|
|
670
|
+
case "unixTimestamp":
|
|
671
|
+
return "unixtime";
|
|
672
|
+
case "rfc7231":
|
|
673
|
+
return "http-date";
|
|
674
|
+
default:
|
|
675
|
+
return encoding;
|
|
676
|
+
}
|
|
677
|
+
case "duration":
|
|
678
|
+
switch (encoding) {
|
|
679
|
+
case "ISO8601":
|
|
680
|
+
return "duration";
|
|
681
|
+
default:
|
|
682
|
+
return encodeAsFormat !== null && encodeAsFormat !== void 0 ? encodeAsFormat : encoding;
|
|
683
|
+
}
|
|
684
|
+
default:
|
|
685
|
+
return encodeAsFormat !== null && encodeAsFormat !== void 0 ? encodeAsFormat : encoding;
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
function isLiteralType(type) {
|
|
689
|
+
return type.kind === "Boolean" || type.kind === "String" || type.kind === "Number";
|
|
690
|
+
}
|
|
691
|
+
function includeDerivedModel(model) {
|
|
692
|
+
var _a, _b;
|
|
693
|
+
return (!isTemplateDeclaration(model) &&
|
|
694
|
+
(((_a = model.templateMapper) === null || _a === void 0 ? void 0 : _a.args) === undefined ||
|
|
695
|
+
((_b = model.templateMapper.args) === null || _b === void 0 ? void 0 : _b.length) === 0 ||
|
|
696
|
+
model.derivedModels.length > 0));
|
|
697
|
+
}
|
|
698
|
+
const B = {
|
|
699
|
+
array: (items) => {
|
|
700
|
+
const builder = new ArrayBuilder();
|
|
701
|
+
for (const item of items) {
|
|
702
|
+
builder.push(item);
|
|
703
|
+
}
|
|
704
|
+
return builder;
|
|
705
|
+
},
|
|
706
|
+
object: (obj) => {
|
|
707
|
+
const builder = new ObjectBuilder();
|
|
708
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
709
|
+
builder.set(key, value);
|
|
710
|
+
}
|
|
711
|
+
return builder;
|
|
712
|
+
},
|
|
713
|
+
};
|
|
714
|
+
//# sourceMappingURL=schema-emitter.js.map
|