Skip to content

Commit

Permalink
tcgc, bug fix on encode on ModelProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Aug 12, 2024
1 parent 5b46eef commit c93f6fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/release-august-2024-2024-7-12-14-21-18.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Bug fix for encode as string on ModelProperty.
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
UsageFlags,
getKnownScalars,
isSdkBuiltInKind,
isSdkIntKind,
} from "./interfaces.js";
import {
createGeneratedName,
Expand Down Expand Up @@ -215,6 +216,11 @@ export function addEncodeInfo(
innerType.encode = "bytes";
}
}
if (isSdkIntKind(innerType.kind)) {
if (encodeData && "encode" in innerType) {
innerType.encode = getEncodeHelper(context, type, innerType.kind);
}
}
return diagnostics.wrap(undefined);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,21 @@ describe("typespec-client-generator-core: built-in types", () => {
strictEqual(type.details, "doc");
strictEqual(type.crossLanguageDefinitionId, "TestService.TestScalar");
});

it("integer scalar encoded as string", async function () {
await runner.compileWithBuiltInService(
`
@usage(Usage.input | Usage.output)
@access(Access.public)
model Test {
@encode(string)
value: safeint;
}
`
);
const sdkType = getSdkTypeHelper(runner);
strictEqual(sdkType.kind, "safeint");
strictEqual(sdkType.encode, "string");
strictEqual(sdkType.baseType, undefined);
});
});

0 comments on commit c93f6fa

Please sign in to comment.