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

Templated types should have unique crossLanguageDefinitionId #1353

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
28 changes: 27 additions & 1 deletion packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function getCrossLanguageDefinitionId(
operation?: Operation,
appendNamespace: boolean = true
): string {
let retval = type.name || "anonymous";
let [retval, args] = getTypeName(context, type, operation, appendNamespace);
const namespace = type.kind === "ModelProperty" ? type.model?.namespace : type.namespace;
switch (type.kind) {
case "Union":
Expand Down Expand Up @@ -252,9 +252,35 @@ export function getCrossLanguageDefinitionId(
if (appendNamespace && namespace && getNamespaceFullName(namespace)) {
retval = `${getNamespaceFullName(namespace)}.${retval}`;
}

if (args) {
retval = `${retval}<${args}>`;
}
return retval;
}

function getTypeName(
context: TCGCContext,
type: Union | Model | Enum | Scalar | ModelProperty | Operation | Namespace | Interface,
operation?: Operation,
appendNamespace: boolean = true
): [string, string | undefined] {
const typeName = type.name || "anonymous";

if (
type.kind === "Model" ||
type.kind === "Union" ||
type.kind === "Scalar" ||
type.kind === "Interface" ||
type.kind === "Operation"
) {
if (type.templateMapper?.args) {
}
}

return [typeName, undefined];
}

/**
* Helper function return the cross langauge package id for a package
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,38 @@ describe("typespec-client-generator-core: model types", () => {
strictEqual(discriminatorProperty.serializedName, "@data.kind");
});

it("crossLanguageDefinitionId should be unique for templated models", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compileWithBuiltInAzureCoreService(`
@access(Access.public)
@usage(Usage.input | Usage.output)
model User {};

@access(Access.public)
@usage(Usage.input | Usage.output)
model AnotherUser {};

@access(Access.public)
@usage(Usage.input | Usage.output)
model Test {
prop1: Page<User>;
prop2: Page<AnotherUser>;
}
`);

const models = runnerWithCore.context.sdkPackage.models;
const pagedUser = models.find((x) => x.name === "PagedUser");
const pagedAnotherUser = models.find((x) => x.name === "PagedAnotherUser");
ok(pagedUser);
ok(pagedAnotherUser);
strictEqual(pagedUser.crossLanguageDefinitionId, "My.Service.PagedUser");
strictEqual(pagedAnotherUser.crossLanguageDefinitionId, "My.Service.PagedAnotherUser");
});

it("filterOutCoreModels true", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
Expand Down
Loading