Skip to content

Commit

Permalink
[tcgc] fix two issues found by go mpg (Azure#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored May 13, 2024
1 parent bb338af commit bb655ca
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/main-2024-4-11-15-5-45.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

change from using logical result to final result
7 changes: 7 additions & 0 deletions .chronus/changes/main-2024-4-11-15-7-31.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

add union support for templated model naming
5 changes: 4 additions & 1 deletion packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ export function getLibraryName(
type.templateMapper.args
.filter(
(arg): arg is Model | Enum =>
"kind" in arg && (arg.kind === "Model" || arg.kind === "Enum") && arg.name.length > 0
"kind" in arg &&
(arg.kind === "Model" || arg.kind === "Enum" || arg.kind === "Union") &&
arg.name !== undefined &&
arg.name.length > 0
)
.map((arg) => pascalCase(arg.name))
.join("")
Expand Down
30 changes: 16 additions & 14 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1302,22 +1302,24 @@ function updateTypesFromOperation(
}
const lroMetaData = getLroMetadata(program, operation);
if (lroMetaData && generateConvenient) {
const logicalResults = diagnostics.pipe(
checkAndGetClientType(context, lroMetaData.logicalResult, operation)
);
logicalResults.forEach((logicalResult) => {
updateUsageOfModel(context, UsageFlags.Output, logicalResult);
});

if (!context.arm) {
// TODO: currently skipping adding of envelopeResult due to arm error
// https://github.com/Azure/typespec-azure/issues/311
const envelopeResults = diagnostics.pipe(
checkAndGetClientType(context, lroMetaData.envelopeResult, operation)
if (lroMetaData.finalResult !== undefined && lroMetaData.finalResult !== "void") {
const finalResults = diagnostics.pipe(
checkAndGetClientType(context, lroMetaData.finalResult, operation)
);
envelopeResults.forEach((envelopeResult) => {
updateUsageOfModel(context, UsageFlags.Output, envelopeResult);
finalResults.forEach((finalResult) => {
updateUsageOfModel(context, UsageFlags.Output, finalResult);
});

if (!context.arm) {
// TODO: currently skipping adding of envelopeResult due to arm error
// https://github.com/Azure/typespec-azure/issues/311
const envelopeResults = diagnostics.pipe(
checkAndGetClientType(context, lroMetaData.envelopeResult, operation)
);
envelopeResults.forEach((envelopeResult) => {
updateUsageOfModel(context, UsageFlags.Output, envelopeResult);
});
}
}
}
return diagnostics.wrap(undefined);
Expand Down
53 changes: 53 additions & 0 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,8 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(method.name, "delete");
strictEqual(method.kind, "lro");
strictEqual(method.response.type, undefined);
strictEqual(runnerWithCore.context.experimental_sdkPackage.models.length, 0);
strictEqual(runnerWithCore.context.experimental_sdkPackage.enums.length, 1);
});
it("paging", async () => {
const runnerWithCore = await createSdkTestRunner({
Expand Down Expand Up @@ -3376,6 +3378,57 @@ describe("typespec-client-generator-core: package", () => {
deepStrictEqual(headerParam.apiVersions, ["v2"]);
});
});

describe("lro", () => {
it("customized lro delete", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compile(`
@versioned(MyVersions)
@server("http://localhost:3000", "endpoint")
@service({name: "Service"})
namespace My.Service;
enum MyVersions {
@useDependency(Versions.v1_0_Preview_2)
v1: "v1",
}
op delete(): {
...AcceptedResponse,
@header("Location")
@Azure.Core.pollingLocation(Azure.Core.StatusMonitorPollingOptions<ArmOperationStatus>)
@Azure.Core.finalLocation(void)
location?: string;
};
@Azure.Core.lroStatus
union ResourceProvisioningState {
Succeeded: "Succeeded",
Failed: "Failed",
Canceled: "Canceled",
string,
}
model ArmOperationStatus {
@Azure.Core.lroStatus
status: ResourceProvisioningState;
@key
@path
@segment("operationStatuses")
id: Azure.Core.uuid;
@visibility("read")
name?: string;
}
`);
const sdkPackage = runnerWithCore.context.experimental_sdkPackage;
strictEqual(sdkPackage.models.length, 0);
strictEqual(sdkPackage.enums.length, 1);
});
});
});

function getServiceMethodOfClient(
Expand Down
29 changes: 29 additions & 0 deletions packages/typespec-client-generator-core/test/public-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,35 @@ describe("typespec-client-generator-core: public-utils", () => {
const model = models.filter((x) => x.name === "ResourceOperationStatusUser")[0];
ok(model);
});

it("template without @friendlyName renaming for union as enum", async () => {
await runner.compileWithBuiltInService(`
union DependencyOfOrigins {
serviceExplicitlyCreated: "ServiceExplicitlyCreated",
userExplicitlyCreated: "UserExplicitlyCreated",
string,
}
model DependencyOfRelationshipProperties
is BaseRelationshipProperties<DependencyOfOrigins>;
model BaseRelationshipProperties<TOrigin> {
originInformation: RelationshipOriginInformation<TOrigin>;
}
model RelationshipOriginInformation<TOrigin = string> {
relationshipOriginType: TOrigin;
}
op test(): DependencyOfRelationshipProperties;
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 2);
const model = models.filter(
(x) => x.name === "RelationshipOriginInformationDependencyOfOrigins"
)[0];
ok(model);
});
});

describe("getGeneratedName", () => {
Expand Down

0 comments on commit bb655ca

Please sign in to comment.