From aeabbb36cbb95f41196b59ab5775434c35006b6e Mon Sep 17 00:00:00 2001 From: tadelesh Date: Tue, 20 Feb 2024 11:29:26 +0800 Subject: [PATCH 1/6] Support new added `@flattenProperty` decorator in autorest emitter --- packages/typespec-autorest/src/openapi.ts | 5 ++ .../typespec-autorest/test/flatten.test.ts | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 packages/typespec-autorest/test/flatten.test.ts diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 73539ef146..5cf0c86b6f 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,10 @@ 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..d3a698a679 --- /dev/null +++ b/packages/typespec-autorest/test/flatten.test.ts @@ -0,0 +1,66 @@ +import { deepStrictEqual } from "assert"; +import { describe, it } from "vitest"; +import { openApiFor } from "./test-host.js"; + +describe("typespec-autorest: flatten", () => { + it("tcgc flatten decorator", async () => { + const res = await openApiFor( + ` + @service + namespace Test; + + model Widget { + #suppress "deprecated" "@flattenProperty decorator is not recommended to use." + @flattenProperty + properties?: WidgetProperties; + } + + model WidgetProperties { + prop: string; + } + + op get(): void; + ` + ); + const model = res.definitions["Widget"]!; + deepStrictEqual(model, { + properties: { + properties: { + "$ref": "#/definitions/WidgetProperties", + "x-ms-client-flatten": true, + }, + }, + type: "object", + }); + }); + + it("openapi extension decorator", async () => { + const res = await openApiFor( + ` + @service + namespace Test; + + model Widget { + @extension("x-ms-client-flatten", true) + properties?: WidgetProperties; + } + + model WidgetProperties { + prop: string; + } + + op get(): void; + ` + ); + const model = res.definitions["Widget"]!; + deepStrictEqual(model, { + properties: { + properties: { + "$ref": "#/definitions/WidgetProperties", + "x-ms-client-flatten": true, + }, + }, + type: "object", + }); + }); +}); From c37aabc6e8eaadb4879d9f2186b4c6f0f2758f3d Mon Sep 17 00:00:00 2001 From: tadelesh Date: Tue, 20 Feb 2024 11:30:58 +0800 Subject: [PATCH 2/6] changelog --- .chronus/changes/flatten-2024-1-20-11-30-53.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/flatten-2024-1-20-11-30-53.md 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..9b1c865a25 --- /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 new added @flattenProperty decorator. \ No newline at end of file From 582b7bdcc60dcefeaec282a36727e6d6e64967b4 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Wed, 21 Feb 2024 16:13:29 +0800 Subject: [PATCH 3/6] format --- packages/typespec-autorest/src/openapi.ts | 5 ++++- packages/typespec-autorest/test/flatten.test.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 5cf0c86b6f..e2d10e4909 100644 --- a/packages/typespec-autorest/src/openapi.ts +++ b/packages/typespec-autorest/src/openapi.ts @@ -1946,7 +1946,10 @@ function createOAPIEmitter( } } - if (typespecType.kind === "ModelProperty" && shouldFlattenProperty(tcgcSdkContext, typespecType)) { + if ( + typespecType.kind === "ModelProperty" && + shouldFlattenProperty(tcgcSdkContext, typespecType) + ) { newTarget["x-ms-client-flatten"] = true; } diff --git a/packages/typespec-autorest/test/flatten.test.ts b/packages/typespec-autorest/test/flatten.test.ts index d3a698a679..6ef247d893 100644 --- a/packages/typespec-autorest/test/flatten.test.ts +++ b/packages/typespec-autorest/test/flatten.test.ts @@ -26,7 +26,7 @@ describe("typespec-autorest: flatten", () => { deepStrictEqual(model, { properties: { properties: { - "$ref": "#/definitions/WidgetProperties", + $ref: "#/definitions/WidgetProperties", "x-ms-client-flatten": true, }, }, @@ -56,7 +56,7 @@ describe("typespec-autorest: flatten", () => { deepStrictEqual(model, { properties: { properties: { - "$ref": "#/definitions/WidgetProperties", + $ref: "#/definitions/WidgetProperties", "x-ms-client-flatten": true, }, }, From fd90846c7a4df8fe5e4629939596bc0a5ecdfc4c Mon Sep 17 00:00:00 2001 From: tadelesh Date: Fri, 23 Feb 2024 17:16:33 +0800 Subject: [PATCH 4/6] address review comment --- .../changes/flatten-2024-1-20-11-30-53.md | 2 +- .../typespec-autorest/test/flatten.test.ts | 80 +++++-------------- 2 files changed, 22 insertions(+), 60 deletions(-) diff --git a/.chronus/changes/flatten-2024-1-20-11-30-53.md b/.chronus/changes/flatten-2024-1-20-11-30-53.md index 9b1c865a25..7ef33e1d45 100644 --- a/.chronus/changes/flatten-2024-1-20-11-30-53.md +++ b/.chronus/changes/flatten-2024-1-20-11-30-53.md @@ -4,4 +4,4 @@ packages: - "@azure-tools/typespec-autorest" --- -Support new added @flattenProperty decorator. \ No newline at end of file +Support new added @flattenProperty decorator. diff --git a/packages/typespec-autorest/test/flatten.test.ts b/packages/typespec-autorest/test/flatten.test.ts index 6ef247d893..93f77c2029 100644 --- a/packages/typespec-autorest/test/flatten.test.ts +++ b/packages/typespec-autorest/test/flatten.test.ts @@ -1,66 +1,28 @@ import { deepStrictEqual } from "assert"; -import { describe, it } from "vitest"; +import { it } from "vitest"; import { openApiFor } from "./test-host.js"; -describe("typespec-autorest: flatten", () => { - it("tcgc flatten decorator", async () => { - const res = await openApiFor( - ` - @service - namespace Test; - - model Widget { - #suppress "deprecated" "@flattenProperty decorator is not recommended to use." - @flattenProperty - properties?: WidgetProperties; - } - - model WidgetProperties { - prop: string; - } - - op get(): void; - ` - ); - const model = res.definitions["Widget"]!; - deepStrictEqual(model, { - properties: { - properties: { - $ref: "#/definitions/WidgetProperties", - "x-ms-client-flatten": true, - }, - }, - type: "object", - }); - }); - - it("openapi extension decorator", async () => { - const res = await openApiFor( - ` - @service - namespace Test; - - model Widget { - @extension("x-ms-client-flatten", true) - properties?: WidgetProperties; - } - - model WidgetProperties { - prop: string; - } - - op get(): void; - ` - ); - const model = res.definitions["Widget"]!; - deepStrictEqual(model, { +it("applies x-ms-client-flatten for property marked with @flattenProperty", async () => { + const res = await openApiFor( + ` + model Widget { + #suppress "deprecated" "@flattenProperty decorator is not recommended to use." + @flattenProperty + properties?: WidgetProperties; + } + + model WidgetProperties { + } + ` + ); + const model = res.definitions["Widget"]!; + deepStrictEqual(model, { + properties: { properties: { - properties: { - $ref: "#/definitions/WidgetProperties", - "x-ms-client-flatten": true, - }, + $ref: "#/definitions/WidgetProperties", + "x-ms-client-flatten": true, }, - type: "object", - }); + }, + type: "object", }); }); From d06eb61ea7e67424d7d609ea91df872874e24201 Mon Sep 17 00:00:00 2001 From: Chenjie Shi Date: Thu, 29 Feb 2024 10:43:23 +0800 Subject: [PATCH 5/6] Update .chronus/changes/flatten-2024-1-20-11-30-53.md Co-authored-by: Timothee Guerin --- .chronus/changes/flatten-2024-1-20-11-30-53.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/flatten-2024-1-20-11-30-53.md b/.chronus/changes/flatten-2024-1-20-11-30-53.md index 7ef33e1d45..01e62940e7 100644 --- a/.chronus/changes/flatten-2024-1-20-11-30-53.md +++ b/.chronus/changes/flatten-2024-1-20-11-30-53.md @@ -4,4 +4,4 @@ packages: - "@azure-tools/typespec-autorest" --- -Support new added @flattenProperty decorator. +Support `@flattenProperty` decorator. From 053d786c447812fb3a425e375e29a7a55579137d Mon Sep 17 00:00:00 2001 From: Chenjie Shi Date: Thu, 29 Feb 2024 10:43:30 +0800 Subject: [PATCH 6/6] Update packages/typespec-autorest/test/flatten.test.ts Co-authored-by: Timothee Guerin --- packages/typespec-autorest/test/flatten.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typespec-autorest/test/flatten.test.ts b/packages/typespec-autorest/test/flatten.test.ts index 93f77c2029..e69b77b9bf 100644 --- a/packages/typespec-autorest/test/flatten.test.ts +++ b/packages/typespec-autorest/test/flatten.test.ts @@ -6,7 +6,7 @@ it("applies x-ms-client-flatten for property marked with @flattenProperty", asyn const res = await openApiFor( ` model Widget { - #suppress "deprecated" "@flattenProperty decorator is not recommended to use." + #suppress "deprecated" "for test" @flattenProperty properties?: WidgetProperties; }