Skip to content

Commit

Permalink
Deprecate getAssetEmitter and recommend calling createAssetEmitter
Browse files Browse the repository at this point in the history
…directly (#3516)

fix #3397

Problem with calling `getAssetEmitter` is it create an asset emitter
with the instance of the compiler used in the compilation and not the
instance of the compiler defined in the type emitter necessarly. This
cause issue with `instanceof` checks which are then not the exact same
class as its loaded form different instance of the compiler
Calling `createAssetEmitter` solve the issue because it is imported in
teh context of the emitter package and will use the emitter package
version
  • Loading branch information
timotheeguerin authored Jul 1, 2024
1 parent 781f2af commit 4cfb2c2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions packages/compiler/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,8 @@ export interface EmitContext<TOptions extends object = Record<string, never>> {
/**
* 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<T>(TypeEmitterClass: typeof TypeEmitter<T, TOptions>): AssetEmitter<T, TOptions>;
Expand Down
3 changes: 2 additions & 1 deletion packages/json-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,7 +46,7 @@ export type JsonSchemaDeclaration = Model | Union | Enum | Scalar;
const jsonSchemaKey = createStateSymbol("JsonSchema");

export async function $onEmit(context: EmitContext<JSONSchemaEmitterOptions>) {
const emitter = context.getAssetEmitter(JsonSchemaEmitter);
const emitter = createAssetEmitter(context.program, JsonSchemaEmitter as any, context);

if (emitter.getOptions().emitAllModels) {
emitter.emitProgram({ emitTypeSpecNamespace: false });
Expand Down
6 changes: 4 additions & 2 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,14 @@ function createOAPIEmitter(
service.type,
options.omitUnreachableTypes
);
schemaEmitter = context.getAssetEmitter(
schemaEmitter = createAssetEmitter(
program,
class extends OpenAPI3SchemaEmitter {
constructor(emitter: AssetEmitter<Record<string, any>, OpenAPI3EmitterOptions>) {
super(emitter, metadataInfo, visibilityUsage, options);
}
} as any
} as any,
context
);

const securitySchemes = getOpenAPISecuritySchemes(allHttpAuthentications);
Expand Down

0 comments on commit 4cfb2c2

Please sign in to comment.