diff --git a/.chronus/changes/scalar_doc-2024-3-2-17-15-13.md b/.chronus/changes/scalar_doc-2024-3-2-17-15-13.md new file mode 100644 index 0000000000..f03d6ded4f --- /dev/null +++ b/.chronus/changes/scalar_doc-2024-3-2-17-15-13.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +support sclar doc \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/interfaces.ts b/packages/typespec-client-generator-core/src/interfaces.ts index 3610b91076..a8ac160dcb 100644 --- a/packages/typespec-client-generator-core/src/interfaces.ts +++ b/packages/typespec-client-generator-core/src/interfaces.ts @@ -85,6 +85,8 @@ interface SdkTypeBase { */ nullable: boolean; deprecation?: string; + description?: string; + details?: string; } export type SdkType = @@ -241,8 +243,6 @@ export interface SdkEnumType extends SdkTypeBase { valueType: SdkBuiltInType; values: SdkEnumValueType[]; isFixed: boolean; - description?: string; - details?: string; isFlags: boolean; usage: UsageFlags; access?: AccessFlags; @@ -257,8 +257,6 @@ export interface SdkEnumValueType extends SdkTypeBase { value: string | number; enumType: SdkEnumType; valueType: SdkBuiltInType; - description?: string; - details?: string; } export interface SdkConstantType extends SdkTypeBase { kind: "constant"; @@ -288,8 +286,6 @@ export interface SdkModelType extends SdkTypeBase { */ isError: boolean; isGeneratedName: boolean; - description?: string; - details?: string; access?: AccessFlags; usage: UsageFlags; additionalProperties?: SdkType; diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 1cb205d98f..85f27c8a5a 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -208,9 +208,12 @@ function getSdkBuiltInTypeWithDiagnostics( kind = getScalarKind(type); } } + const docWrapper = getDocHelper(context, type); return diagnostics.wrap({ ...getSdkTypeBaseHelper(context, type, kind), encode: getEncodeHelper(context, type, kind), + description: docWrapper.description, + details: docWrapper.details, }); } else if (type.kind === "String" || type.kind === "Boolean" || type.kind === "Number") { let kind: SdkBuiltInKinds; @@ -779,6 +782,9 @@ export function getClientTypeWithDiagnostics( retval = getKnownValuesEnum(context, type, operation) ?? baseType; const namespace = type.namespace ? getNamespaceFullName(type.namespace) : ""; retval.kind = context.knownScalars[`${namespace}.${type.name}`] ?? retval.kind; + const docWrapper = getDocHelper(context, type); + retval.description = docWrapper.description; + retval.details = docWrapper.details; break; } if (type.name === "utcDateTime" || type.name === "offsetDateTime") { diff --git a/packages/typespec-client-generator-core/test/types.test.ts b/packages/typespec-client-generator-core/test/types.test.ts index 3912dc0a32..2fb3e72019 100644 --- a/packages/typespec-client-generator-core/test/types.test.ts +++ b/packages/typespec-client-generator-core/test/types.test.ts @@ -263,6 +263,25 @@ describe("typespec-client-generator-core: types", () => { } } }); + it("with doc", async () => { + await runner.compileWithBuiltInService( + ` + @doc("doc") + @summary("title") + scalar TestScalar extends string; + + @usage(Usage.input | Usage.output) + @access(Access.public) + model Test { + prop: TestScalar; + } + ` + ); + const models = getAllModels(runner.context); + strictEqual(models[0].kind, "model"); + strictEqual(models[0].properties[0].type.description, "title"); + strictEqual(models[0].properties[0].type.details, "doc"); + }); }); describe("SdkDurationType", () => { it("default", async function () { @@ -333,9 +352,11 @@ describe("typespec-client-generator-core: types", () => { it("float seconds decorated scalar", async function () { await runner.compileWithBuiltInService( ` + @doc("doc") + @summary("title") @encode(DurationKnownEncoding.seconds, float32) scalar Float32Duration extends duration; - + @usage(Usage.input | Usage.output) @access(Access.public) model Test { @@ -348,6 +369,8 @@ describe("typespec-client-generator-core: types", () => { strictEqual(sdkType.valueType.kind, "duration"); strictEqual(sdkType.valueType.wireType.kind, "float32"); strictEqual(sdkType.valueType.encode, "seconds"); + strictEqual(sdkType.valueType.description, "title"); + strictEqual(sdkType.valueType.details, "doc"); }); }); @@ -420,6 +443,8 @@ describe("typespec-client-generator-core: types", () => { it("unixTimestamp array", async function () { await runner.compileWithBuiltInService( ` + @doc("doc") + @summary("title") @encode(DateTimeKnownEncoding.unixTimestamp, int64) scalar unixTimestampDatetime extends utcDateTime; @@ -435,6 +460,8 @@ describe("typespec-client-generator-core: types", () => { strictEqual(sdkType.valueType.kind, "utcDateTime"); strictEqual(sdkType.valueType.wireType.kind, "int64"); strictEqual(sdkType.valueType.encode, "unixTimestamp"); + strictEqual(sdkType.valueType.description, "title"); + strictEqual(sdkType.valueType.details, "doc"); }); }); describe("SdkUnionType", () => {