diff --git a/.chronus/changes/prevent_breaking-2024-1-28-17-11-9.md b/.chronus/changes/prevent_breaking-2024-1-28-17-11-9.md new file mode 100644 index 0000000000..b7560c3c11 --- /dev/null +++ b/.chronus/changes/prevent_breaking-2024-1-28-17-11-9.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +rollback some of the breaking changes for common model types method \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/decorators.ts b/packages/typespec-client-generator-core/src/decorators.ts index aa038bf499..d5baeb834b 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -17,7 +17,6 @@ import { UsageFlags, getNamespaceFullName, getProjectedName, - ignoreDiagnostics, isService, isTemplateDeclaration, isTemplateDeclarationOrInstance, @@ -740,7 +739,7 @@ export function getUsage(context: TCGCContext, entity: Model | Enum): UsageFlags getAllModels(context); // this will populate modelsMap } return entity.kind === "Model" - ? ignoreDiagnostics(getSdkModel(context, entity)).usage + ? getSdkModel(context, entity).usage : getSdkEnum(context, entity).usage; } @@ -783,7 +782,7 @@ export function getAccess( } return entity.kind === "Model" - ? ignoreDiagnostics(getSdkModel(context, entity)).access + ? getSdkModel(context, entity).access : getSdkEnum(context, entity).access; } diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 2aa499c271..59d2e8c63d 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -271,6 +271,14 @@ export function getSdkArrayOrDict( context: TCGCContext, type: Model, operation?: Operation +): (SdkDictionaryType | SdkArrayType) | undefined { + return ignoreDiagnostics(getSdkArrayOrDictWithDiagnostics(context, type, operation)); +} + +export function getSdkArrayOrDictWithDiagnostics( + context: TCGCContext, + type: Model, + operation?: Operation ): [(SdkDictionaryType | SdkArrayType) | undefined, readonly Diagnostic[]] { const diagnostics = createDiagnosticCollector(); if (type.indexer !== undefined) { @@ -306,6 +314,14 @@ export function getSdkTuple( context: TCGCContext, type: Tuple, operation?: Operation +): SdkTupleType { + return ignoreDiagnostics(getSdkTupleWithDiagnostics(context, type, operation)); +} + +export function getSdkTupleWithDiagnostics( + context: TCGCContext, + type: Tuple, + operation?: Operation ): [SdkTupleType, readonly Diagnostic[]] { const diagnostics = createDiagnosticCollector(); return diagnostics.wrap({ @@ -320,7 +336,11 @@ function getNonNullOptions(context: TCGCContext, type: Union): Type[] { return [...type.variants.values()].map((x) => x.type).filter((t) => !isNullType(t)); } -export function getSdkUnion( +export function getSdkUnion(context: TCGCContext, type: Union, operation?: Operation): SdkType { + return ignoreDiagnostics(getSdkUnionWithDiagnostics(context, type, operation)); +} + +export function getSdkUnionWithDiagnostics( context: TCGCContext, type: Union, operation?: Operation @@ -379,7 +399,9 @@ function addDiscriminatorToModelType( if (discriminator) { let discriminatorProperty; for (const childModel of type.derivedModels) { - const childModelSdkType = diagnostics.pipe(getSdkModel(context, childModel, operation)); + const childModelSdkType = diagnostics.pipe( + getSdkModelWithDiagnostics(context, childModel, operation) + ); updateModelsMap(context, childModel, childModelSdkType, operation); for (const property of childModelSdkType.properties) { if (property.kind === "property") { @@ -460,6 +482,14 @@ export function getSdkModel( context: TCGCContext, type: Model, operation?: Operation +): SdkModelType { + return ignoreDiagnostics(getSdkModelWithDiagnostics(context, type, operation)); +} + +export function getSdkModelWithDiagnostics( + context: TCGCContext, + type: Model, + operation?: Operation ): [SdkModelType, readonly Diagnostic[]] { const diagnostics = createDiagnosticCollector(); type = getEffectivePayloadType(context, type); @@ -689,12 +719,12 @@ export function getClientTypeWithDiagnostics( retval = getSdkConstant(context, type); break; case "Tuple": - retval = diagnostics.pipe(getSdkTuple(context, type, operation)); + retval = diagnostics.pipe(getSdkTupleWithDiagnostics(context, type, operation)); break; case "Model": - retval = diagnostics.pipe(getSdkArrayOrDict(context, type, operation)); + retval = diagnostics.pipe(getSdkArrayOrDictWithDiagnostics(context, type, operation)); if (retval === undefined) { - retval = diagnostics.pipe(getSdkModel(context, type, operation)); + retval = diagnostics.pipe(getSdkModelWithDiagnostics(context, type, operation)); } break; case "Intrinsic": @@ -736,7 +766,7 @@ export function getClientTypeWithDiagnostics( if (unionAsEnum && type.name) { retval = getSdkUnionEnum(context, unionAsEnum, operation); } else { - retval = diagnostics.pipe(getSdkUnion(context, type, operation)); + retval = diagnostics.pipe(getSdkUnionWithDiagnostics(context, type, operation)); } break; case "ModelProperty": diff --git a/packages/typespec-client-generator-core/test/public-utils.test.ts b/packages/typespec-client-generator-core/test/public-utils.test.ts index bd08aace4c..2460a888d6 100644 --- a/packages/typespec-client-generator-core/test/public-utils.test.ts +++ b/packages/typespec-client-generator-core/test/public-utils.test.ts @@ -1325,9 +1325,7 @@ describe("typespec-client-generator-core: public-utils", () => { } `)) as { repeatabilityResult: ModelProperty }; - const union = ignoreDiagnostics( - getSdkUnion(runner.context, repeatabilityResult.type as Union) - ); + const union = getSdkUnion(runner.context, repeatabilityResult.type as Union); strictEqual( (union as SdkUnionType).generatedName, "ResponseWithAnonymousUnionRepeatabilityResult" @@ -1350,9 +1348,7 @@ describe("typespec-client-generator-core: public-utils", () => { } `)) as { repeatabilityResult: ModelProperty }; - const union = ignoreDiagnostics( - getSdkUnion(runner.context, repeatabilityResult.type as Union) - ); + const union = getSdkUnion(runner.context, repeatabilityResult.type as Union); strictEqual( (union as SdkUnionType).generatedName, "RequestParameterWithAnonymousUnionRepeatabilityResult" @@ -1375,9 +1371,7 @@ describe("typespec-client-generator-core: public-utils", () => { } `)) as { repeatabilityResult: ModelProperty }; - const stringType = ignoreDiagnostics( - getSdkUnion(runner.context, repeatabilityResult.type as Union) - )!; + const stringType = getSdkUnion(runner.context, repeatabilityResult.type as Union)!; strictEqual(stringType.kind, "union"); strictEqual(stringType.values.length, 3); strictEqual(stringType.values[0].kind, "constant");