@typespec/openapi3
Advanced tools
Comparing version 0.54.0-dev.0 to 0.54.0-dev.2
@@ -1,2 +0,2 @@ | ||
import { compilerAssert, emitFile, getAllTags, getAnyExtensionFromPath, getDoc, getEncode, getFormat, getKnownValues, getMaxItems, getMaxLength, getMaxValue, getMaxValueExclusive, getMinItems, getMinLength, getMinValue, getMinValueExclusive, getNamespaceFullName, getPattern, getService, getSummary, ignoreDiagnostics, interpolatePath, isArrayModelType, isDeprecated, isGlobalNamespace, isNeverType, isNullType, isSecret, isVoidType, listServices, navigateTypesInNamespace, projectProgram, resolvePath, } from "@typespec/compiler"; | ||
import { compilerAssert, emitFile, getAllTags, getAnyExtensionFromPath, getDoc, getEncode, getFormat, getKnownValues, getMaxItems, getMaxLength, getMaxValue, getMaxValueExclusive, getMinItems, getMinLength, getMinValue, getMinValueExclusive, getNamespaceFullName, getPattern, getService, getSummary, ignoreDiagnostics, interpolatePath, isDeprecated, isGlobalNamespace, isNeverType, isSecret, isVoidType, listServices, navigateTypesInNamespace, projectProgram, resolvePath, } from "@typespec/compiler"; | ||
import { createMetadataInfo, getAuthentication, getHttpService, getServers, getStatusCodeDescription, isContentTypeHeader, isOverloadSameEndpoint, reportIfNoRoutes, resolveRequestVisibility, Visibility, } from "@typespec/http"; | ||
@@ -8,3 +8,3 @@ import { getExtensions, getExternalDocs, getInfo, getOpenAPITypeName, getParameterKey, isDefaultResponse, isReadonlyProperty, resolveOperationId, shouldInline, } from "@typespec/openapi"; | ||
import { reportDiagnostic } from "./lib.js"; | ||
import { OpenAPI3SchemaEmitter } from "./schema-emitter.js"; | ||
import { getDefaultValue, OpenAPI3SchemaEmitter } from "./schema-emitter.js"; | ||
import { deepEquals } from "./util.js"; | ||
@@ -160,3 +160,3 @@ import { resolveVisibilityUsage } from "./visibility-usage.js"; | ||
const variable = { | ||
default: prop.default ? getDefaultValue(prop.type, prop.default) : "", | ||
default: prop.default ? getDefaultValue(program, prop.type, prop.default) : "", | ||
description: getDoc(program, prop), | ||
@@ -874,3 +874,3 @@ }; | ||
if (param.default) { | ||
schema.default = getDefaultValue(param.type, param.default); | ||
schema.default = getDefaultValue(program, param.type, param.default); | ||
} | ||
@@ -1020,37 +1020,2 @@ // Description is already provided in the parameter itself. | ||
} | ||
function getDefaultValue(type, defaultType) { | ||
var _a; | ||
switch (defaultType.kind) { | ||
case "String": | ||
return defaultType.value; | ||
case "Number": | ||
return defaultType.value; | ||
case "Boolean": | ||
return defaultType.value; | ||
case "Tuple": | ||
compilerAssert(type.kind === "Tuple" || (type.kind === "Model" && isArrayModelType(program, type)), "setting tuple default to non-tuple value"); | ||
if (type.kind === "Tuple") { | ||
return defaultType.values.map((defaultTupleValue, index) => getDefaultValue(type.values[index], defaultTupleValue)); | ||
} | ||
else { | ||
return defaultType.values.map((defaultTuplevalue) => getDefaultValue(type.indexer.value, defaultTuplevalue)); | ||
} | ||
case "Intrinsic": | ||
return isNullType(defaultType) | ||
? null | ||
: reportDiagnostic(program, { | ||
code: "invalid-default", | ||
format: { type: defaultType.kind }, | ||
target: defaultType, | ||
}); | ||
case "EnumMember": | ||
return (_a = defaultType.value) !== null && _a !== void 0 ? _a : defaultType.name; | ||
default: | ||
reportDiagnostic(program, { | ||
code: "invalid-default", | ||
format: { type: defaultType.kind }, | ||
target: defaultType, | ||
}); | ||
} | ||
} | ||
function attachExtensions(program, type, emitObject) { | ||
@@ -1057,0 +1022,0 @@ // Attach any OpenAPI extensions |
@@ -49,2 +49,3 @@ import { BooleanLiteral, Enum, EnumMember, IntrinsicType, Model, ModelProperty, NumericLiteral, Program, Scalar, StringLiteral, StringTemplate, Tuple, Type, Union, UnionVariant } from "@typespec/compiler"; | ||
} | ||
export declare function getDefaultValue(program: Program, type: Type, defaultType: Type): any; | ||
//# sourceMappingURL=schema-emitter.d.ts.map |
@@ -12,3 +12,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
}; | ||
var _OpenAPI3SchemaEmitter_instances, _OpenAPI3SchemaEmitter_metadataInfo, _OpenAPI3SchemaEmitter_visibilityUsage, _OpenAPI3SchemaEmitter_options, _OpenAPI3SchemaEmitter_applyExternalDocs, _OpenAPI3SchemaEmitter_typeNameOptions, _OpenAPI3SchemaEmitter_getVisibilityContext, _OpenAPI3SchemaEmitter_getContentType, _OpenAPI3SchemaEmitter_requiredModelProperties, _OpenAPI3SchemaEmitter_isBytesKeptRaw, _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; | ||
var _OpenAPI3SchemaEmitter_instances, _OpenAPI3SchemaEmitter_metadataInfo, _OpenAPI3SchemaEmitter_visibilityUsage, _OpenAPI3SchemaEmitter_options, _OpenAPI3SchemaEmitter_applyExternalDocs, _OpenAPI3SchemaEmitter_typeNameOptions, _OpenAPI3SchemaEmitter_getVisibilityContext, _OpenAPI3SchemaEmitter_getContentType, _OpenAPI3SchemaEmitter_requiredModelProperties, _OpenAPI3SchemaEmitter_isBytesKeptRaw, _OpenAPI3SchemaEmitter_enumSchema, _OpenAPI3SchemaEmitter_unionSchema, _OpenAPI3SchemaEmitter_getDiscriminatorMapping, _OpenAPI3SchemaEmitter_attachExtensions, _OpenAPI3SchemaEmitter_getSchemaForScalar, _OpenAPI3SchemaEmitter_getSchemaForStdScalars, _OpenAPI3SchemaEmitter_applyConstraints, _OpenAPI3SchemaEmitter_inlineType, _OpenAPI3SchemaEmitter_createDeclaration, _OpenAPI3SchemaEmitter_isStdType, _OpenAPI3SchemaEmitter_applyEncoding, _OpenAPI3SchemaEmitter_mergeFormatAndEncoding; | ||
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, resolveEncodedName, stringTemplateToString, } from "@typespec/compiler"; | ||
@@ -189,3 +189,3 @@ import { ArrayBuilder, ObjectBuilder, Placeholder, TypeEmitter, } from "@typespec/compiler/emitter-framework"; | ||
if (prop.default) { | ||
additionalProps.default = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDefaultValue).call(this, prop.type, prop.default); | ||
additionalProps.default = getDefaultValue(program, prop.type, prop.default); | ||
} | ||
@@ -388,3 +388,3 @@ if (isReadonlyProperty(program, prop)) { | ||
reportDiagnostic(program, { code: "empty-union", target: union }); | ||
return {}; | ||
return new ObjectBuilder({}); | ||
} | ||
@@ -424,3 +424,3 @@ const variants = Array.from(union.variants.values()); | ||
reportDiagnostic(program, { code: "union-null", target: union }); | ||
return {}; | ||
return new ObjectBuilder({}); | ||
} | ||
@@ -434,27 +434,33 @@ else { | ||
// we can just return the single schema member after applying nullable | ||
let schema = schemaMembers[0].schema; | ||
const schema = schemaMembers[0].schema; | ||
const type = schemaMembers[0].type; | ||
const additionalProps = __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_applyConstraints).call(this, union, {}); | ||
if (nullable) { | ||
if (schema instanceof Placeholder || "$ref" in schema) { | ||
// but we can't make a ref "nullable", so wrap in an allOf (for models) | ||
// or oneOf (for all other types) | ||
additionalProps.nullable = true; | ||
} | ||
if (Object.keys(additionalProps).length === 0) { | ||
return new ObjectBuilder(schema); | ||
} | ||
else { | ||
if ((schema instanceof Placeholder || "$ref" in schema) && | ||
!(type && shouldInline(program, type))) { | ||
if (type && type.kind === "Model") { | ||
if (shouldInline(program, type)) { | ||
const merged = new ObjectBuilder(schema); | ||
merged.set("nullable", true); | ||
return merged; | ||
} | ||
else { | ||
return B.object({ type: "object", allOf: B.array([schema]), nullable: true }); | ||
} | ||
return new ObjectBuilder({ | ||
type: "object", | ||
allOf: B.array([schema]), | ||
...additionalProps, | ||
}); | ||
} | ||
else { | ||
return B.object({ oneOf: B.array([schema]), nullable: true }); | ||
return new ObjectBuilder({ oneOf: B.array([schema]), ...additionalProps }); | ||
} | ||
} | ||
else { | ||
schema = { ...schema, nullable: true }; | ||
const merged = new ObjectBuilder(schema); | ||
for (const [key, value] of Object.entries(additionalProps)) { | ||
merged.set(key, value); | ||
} | ||
return merged; | ||
} | ||
} | ||
return schema; | ||
} | ||
@@ -494,37 +500,2 @@ const schema = { | ||
} | ||
}, _OpenAPI3SchemaEmitter_getDefaultValue = function _OpenAPI3SchemaEmitter_getDefaultValue(type, defaultType) { | ||
var _a; | ||
const program = this.emitter.getProgram(); | ||
switch (defaultType.kind) { | ||
case "String": | ||
return defaultType.value; | ||
case "Number": | ||
return defaultType.value; | ||
case "Boolean": | ||
return defaultType.value; | ||
case "Tuple": | ||
compilerAssert(type.kind === "Tuple" || (type.kind === "Model" && isArrayModelType(program, type)), "setting tuple default to non-tuple value"); | ||
if (type.kind === "Tuple") { | ||
return defaultType.values.map((defaultTupleValue, index) => __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDefaultValue).call(this, type.values[index], defaultTupleValue)); | ||
} | ||
else { | ||
return defaultType.values.map((defaultTuplevalue) => __classPrivateFieldGet(this, _OpenAPI3SchemaEmitter_instances, "m", _OpenAPI3SchemaEmitter_getDefaultValue).call(this, type.indexer.value, defaultTuplevalue)); | ||
} | ||
case "Intrinsic": | ||
return isNullType(defaultType) | ||
? null | ||
: reportDiagnostic(program, { | ||
code: "invalid-default", | ||
format: { type: defaultType.kind }, | ||
target: defaultType, | ||
}); | ||
case "EnumMember": | ||
return (_a = defaultType.value) !== null && _a !== void 0 ? _a : defaultType.name; | ||
default: | ||
reportDiagnostic(program, { | ||
code: "invalid-default", | ||
format: { type: defaultType.kind }, | ||
target: defaultType, | ||
}); | ||
} | ||
}, _OpenAPI3SchemaEmitter_getSchemaForScalar = function _OpenAPI3SchemaEmitter_getSchemaForScalar(scalar) { | ||
@@ -737,2 +708,39 @@ let result = {}; | ||
}; | ||
export function getDefaultValue(program, type, defaultType) { | ||
var _a; | ||
switch (defaultType.kind) { | ||
case "String": | ||
return defaultType.value; | ||
case "Number": | ||
return defaultType.value; | ||
case "Boolean": | ||
return defaultType.value; | ||
case "Tuple": | ||
compilerAssert(type.kind === "Tuple" || (type.kind === "Model" && isArrayModelType(program, type)), "setting tuple default to non-tuple value"); | ||
if (type.kind === "Tuple") { | ||
return defaultType.values.map((defaultTupleValue, index) => getDefaultValue(program, type.values[index], defaultTupleValue)); | ||
} | ||
else { | ||
return defaultType.values.map((defaultTuplevalue) => getDefaultValue(program, type.indexer.value, defaultTuplevalue)); | ||
} | ||
case "Intrinsic": | ||
return isNullType(defaultType) | ||
? null | ||
: reportDiagnostic(program, { | ||
code: "invalid-default", | ||
format: { type: defaultType.kind }, | ||
target: defaultType, | ||
}); | ||
case "EnumMember": | ||
return (_a = defaultType.value) !== null && _a !== void 0 ? _a : defaultType.name; | ||
case "UnionVariant": | ||
return getDefaultValue(program, type, defaultType.type); | ||
default: | ||
reportDiagnostic(program, { | ||
code: "invalid-default", | ||
format: { type: defaultType.kind }, | ||
target: defaultType, | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=schema-emitter.js.map |
{ | ||
"name": "@typespec/openapi3", | ||
"version": "0.54.0-dev.0", | ||
"version": "0.54.0-dev.2", | ||
"author": "Microsoft Corporation", | ||
@@ -5,0 +5,0 @@ "description": "TypeSpec library for emitting OpenAPI 3.0 from the TypeSpec REST protocol binding", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
259478
3333