Skip to content

Commit

Permalink
fix wrong judgement for array type (#692)
Browse files Browse the repository at this point in the history
@iscai-msft please help to review and release, thanks.

fix: #690

cc: @haolingdong-msft
  • Loading branch information
tadelesh authored Apr 19, 2024
1 parent d61015f commit 62a9231
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_array-2024-3-18-17-41-54.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

fix wrong judgement for array type
4 changes: 3 additions & 1 deletion packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export function getSdkArrayOrDictWithDiagnostics(
if (type.sourceModel?.kind === "Model" && type.sourceModel?.name === "Record") {
return diagnostics.wrap(undefined);
}
// other cases are dict
return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, "dict"),
keyType: diagnostics.pipe(
Expand All @@ -296,7 +297,8 @@ export function getSdkArrayOrDictWithDiagnostics(
valueType,
nullableValues: isNullable(type.indexer.value!),
});
} else if (name === "integer" && type.name === "Array") {
} else if (name === "integer") {
// only array's index key name is integer
return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, "array"),
valueType,
Expand Down
30 changes: 30 additions & 0 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3067,6 +3067,36 @@ describe("typespec-client-generator-core: types", () => {
);
});
});

describe("SdkArrayType", () => {
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;
}
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
strictEqual(model.kind, "model");
strictEqual(model.name, "TestModel");
const client = runner.context.experimental_sdkPackage.clients[0];
ok(client);
const method = client.methods[0];
ok(method);
strictEqual(method.response.kind, "method");
strictEqual(method.response.type?.kind, "array");
strictEqual(method.response.type?.valueType.kind, "model");
strictEqual(method.response.type?.valueType.name, "TestModel");
});
});

describe("SdkMultipartFormType", () => {
it("multipart form basic", async function () {
await runner.compileWithBuiltInService(`
Expand Down

0 comments on commit 62a9231

Please sign in to comment.