Skip to content

Commit

Permalink
tcgc, bug fix on encode string (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Aug 13, 2024
1 parent 5b46eef commit 84d89d8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 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.
25 changes: 16 additions & 9 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 @@ -137,15 +138,7 @@ function getAnyType(context: TCGCContext, type: Type): [SdkBuiltInType, readonly

function getEncodeHelper(context: TCGCContext, type: Type, kind: string): string {
if (type.kind === "ModelProperty" || type.kind === "Scalar") {
const encode = getEncode(context.program, type);
if (encode?.encoding) {
return encode.encoding;
}
if (encode?.type) {
// if we specify the encoding type in the decorator, we set the `.encode` string
// to the kind of the encoding type
return getSdkBuiltInType(context, encode.type).kind;
}
return getEncode(context.program, type)?.encoding || kind;
}
return kind;
}
Expand Down Expand Up @@ -215,6 +208,20 @@ export function addEncodeInfo(
innerType.encode = "bytes";
}
}
if (isSdkIntKind(innerType.kind)) {
// only integer type is allowed to be encoded as string
if (encodeData && "encode" in innerType) {
const encode = getEncode(context.program, type);
if (encode?.encoding) {
innerType.encode = encode.encoding;
}
if (encode?.type) {
// if we specify the encoding type in the decorator, we set the `.encode` string
// to the kind of the encoding type
innerType.encode = getSdkBuiltInType(context, encode.type).kind;
}
}
}
return diagnostics.wrap(undefined);
}

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

it("integer model property 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);
});

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

0 comments on commit 84d89d8

Please sign in to comment.