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

Add name and tspNamespace to SdkArrayType #1000

Merged
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
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: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Add `name` and `tspNamespace` to `SdkArrayType`
2 changes: 2 additions & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export interface SdkDurationType extends SdkTypeBase {

export interface SdkArrayType extends SdkTypeBase {
kind: "array";
name: string;
tspNamespace?: string;
valueType: SdkType;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,15 @@ export function getSdkArrayOrDictWithDiagnostics(
keyType: diagnostics.pipe(
getClientTypeWithDiagnostics(context, type.indexer.key, operation)
),
valueType,
valueType: valueType,
});
} else if (name === "integer") {
// only array's index key name is integer
return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, "array"),
valueType,
name: getLibraryName(context, type),
tspNamespace: getNamespaceHelper(type.namespace),
valueType: valueType,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { SdkTestRunner, createSdkTestRunner } from "../test-host.js";
Expand All @@ -11,16 +12,16 @@ describe("typespec-client-generator-core: array types", () => {

it("use model is to represent array", async () => {
await runner.compile(`
@service({})
namespace TestClient {
model TestModel {
prop: string;
}
model TestArray is TestModel[];

op get(): TestArray;
@service({})
namespace TestClient {
model TestModel {
prop: string;
}
`);
model TestArray is TestModel[];

op get(): TestArray;
}
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
Expand All @@ -32,7 +33,65 @@ describe("typespec-client-generator-core: array types", () => {
ok(method);
strictEqual(method.response.kind, "method");
strictEqual(method.response.type?.kind, "array");
strictEqual(method.response.type?.name, "TestArray");
strictEqual(method.response.type?.tspNamespace, "TestClient");
strictEqual(method.response.type?.valueType.kind, "model");
strictEqual(method.response.type?.valueType.name, "TestModel");
});

it("EmbeddingVector from azure-core", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
"filter-out-core-models": false,
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compileWithBuiltInAzureCoreService(`
@service({})
namespace TestClient {
model ModelWithEmbeddingVector {
prop: EmbeddingVector<int32>;
}

op get(): ModelWithEmbeddingVector;
}
`);
const models = runnerWithCore.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
const property = model.properties[0];
strictEqual(property.type.kind, "array");
strictEqual(property.type.name, "EmbeddingVector");
strictEqual(property.type.tspNamespace, "Azure.Core");
strictEqual(property.type.valueType.kind, "int32");
});

it("alias of EmbeddingVector", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
"filter-out-core-models": false,
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compileWithBuiltInAzureCoreService(`
@service({})
namespace TestClient {
alias MyEmbeddingVector = EmbeddingVector<int32>;

model ModelWithEmbeddingVector {
prop: MyEmbeddingVector;
}

op get(): ModelWithEmbeddingVector;
}
`);
const models = runnerWithCore.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
const property = model.properties[0];
strictEqual(property.type.kind, "array");
strictEqual(property.type.name, "EmbeddingVector");
strictEqual(property.type.tspNamespace, "Azure.Core");
strictEqual(property.type.valueType.kind, "int32");
});
});
Loading