diff --git a/.chronus/changes/flatten-2024-1-20-11-30-53.md b/.chronus/changes/flatten-2024-1-20-11-30-53.md new file mode 100644 index 0000000000..01e62940e7 --- /dev/null +++ b/.chronus/changes/flatten-2024-1-20-11-30-53.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@azure-tools/typespec-autorest" +--- + +Support `@flattenProperty` decorator. diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 7fe136815c..8ca25357ce 100644 --- a/packages/typespec-autorest/src/openapi.ts +++ b/packages/typespec-autorest/src/openapi.ts @@ -12,6 +12,7 @@ import { SdkContext, createSdkContext, getClientNameOverride, + shouldFlattenProperty, } from "@azure-tools/typespec-client-generator-core"; import { ArrayModelType, @@ -1945,6 +1946,13 @@ function createOAPIEmitter( } } + if ( + typespecType.kind === "ModelProperty" && + shouldFlattenProperty(tcgcSdkContext, typespecType) + ) { + newTarget["x-ms-client-flatten"] = true; + } + attachExtensions(typespecType, newTarget); return typespecType.kind === "Scalar" || typespecType.kind === "ModelProperty" diff --git a/packages/typespec-autorest/test/flatten.test.ts b/packages/typespec-autorest/test/flatten.test.ts new file mode 100644 index 0000000000..e69b77b9bf --- /dev/null +++ b/packages/typespec-autorest/test/flatten.test.ts @@ -0,0 +1,28 @@ +import { deepStrictEqual } from "assert"; +import { it } from "vitest"; +import { openApiFor } from "./test-host.js"; + +it("applies x-ms-client-flatten for property marked with @flattenProperty", async () => { + const res = await openApiFor( + ` + model Widget { + #suppress "deprecated" "for test" + @flattenProperty + properties?: WidgetProperties; + } + + model WidgetProperties { + } + ` + ); + const model = res.definitions["Widget"]!; + deepStrictEqual(model, { + properties: { + properties: { + $ref: "#/definitions/WidgetProperties", + "x-ms-client-flatten": true, + }, + }, + type: "object", + }); +});