Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rollback breaking changes for common models #327

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/prevent_breaking-2024-1-28-17-11-9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

rollback some of the breaking changes for common model types method
5 changes: 2 additions & 3 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
UsageFlags,
getNamespaceFullName,
getProjectedName,
ignoreDiagnostics,
isService,
isTemplateDeclaration,
isTemplateDeclarationOrInstance,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -783,7 +782,7 @@ export function getAccess(
}

return entity.kind === "Model"
? ignoreDiagnostics(getSdkModel(context, entity)).access
? getSdkModel(context, entity).access
: getSdkEnum(context, entity).access;
}

Expand Down
42 changes: 36 additions & 6 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand All @@ -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
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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");
Expand Down
Loading