diff --git a/.chronus/changes/deprecate-get-asset-emitter-2024-5-4-16-18-29.md b/.chronus/changes/deprecate-get-asset-emitter-2024-5-4-16-18-29.md new file mode 100644 index 000000000..e782b0126 --- /dev/null +++ b/.chronus/changes/deprecate-get-asset-emitter-2024-5-4-16-18-29.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Deprecate getAssetEmitter and recommend calling `createAssetEmitter` directly diff --git a/.chronus/changes/deprecate-get-asset-emitter-2024-5-4-16-38-47.md b/.chronus/changes/deprecate-get-asset-emitter-2024-5-4-16-38-47.md new file mode 100644 index 000000000..1ac7a664c --- /dev/null +++ b/.chronus/changes/deprecate-get-asset-emitter-2024-5-4-16-38-47.md @@ -0,0 +1,9 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/json-schema" + - "@typespec/openapi3" +--- + +Fix issue that could result in invalid document generation when running `tsp compile` from another directory diff --git a/packages/compiler/src/core/types.ts b/packages/compiler/src/core/types.ts index 52f6876ab..193ce64e5 100644 --- a/packages/compiler/src/core/types.ts +++ b/packages/compiler/src/core/types.ts @@ -2639,6 +2639,8 @@ export interface EmitContext> { /** * Get an asset emitter to write emitted output to disk using a TypeEmitter * + * @deprecated call {@link createAssetEmitter} directly instead. + * * @param TypeEmitterClass The TypeEmitter to construct your emitted output */ getAssetEmitter(TypeEmitterClass: typeof TypeEmitter): AssetEmitter; diff --git a/packages/json-schema/src/index.ts b/packages/json-schema/src/index.ts index 27a23b8de..4839373ba 100644 --- a/packages/json-schema/src/index.ts +++ b/packages/json-schema/src/index.ts @@ -14,6 +14,7 @@ import { setTypeSpecNamespace, typespecTypeToJson, } from "@typespec/compiler"; +import { createAssetEmitter } from "@typespec/compiler/emitter-framework"; import { ValidatesRawJsonDecorator } from "../generated-defs/TypeSpec.JsonSchema.Private.js"; import { BaseUriDecorator, @@ -45,7 +46,7 @@ export type JsonSchemaDeclaration = Model | Union | Enum | Scalar; const jsonSchemaKey = createStateSymbol("JsonSchema"); export async function $onEmit(context: EmitContext) { - const emitter = context.getAssetEmitter(JsonSchemaEmitter); + const emitter = createAssetEmitter(context.program, JsonSchemaEmitter as any, context); if (emitter.getOptions().emitAllModels) { emitter.emitProgram({ emitTypeSpecNamespace: false }); diff --git a/packages/openapi3/src/openapi.ts b/packages/openapi3/src/openapi.ts index 6922ab3ae..01538af7c 100644 --- a/packages/openapi3/src/openapi.ts +++ b/packages/openapi3/src/openapi.ts @@ -301,12 +301,14 @@ function createOAPIEmitter( service.type, options.omitUnreachableTypes ); - schemaEmitter = context.getAssetEmitter( + schemaEmitter = createAssetEmitter( + program, class extends OpenAPI3SchemaEmitter { constructor(emitter: AssetEmitter, OpenAPI3EmitterOptions>) { super(emitter, metadataInfo, visibilityUsage, options); } - } as any + } as any, + context ); const securitySchemes = getOpenAPISecuritySchemes(allHttpAuthentications);