From 5a6d92928625b46d7a81a9e42f9ade25c7708232 Mon Sep 17 00:00:00 2001 From: Mary Gao Date: Tue, 18 Apr 2023 09:59:12 +0800 Subject: [PATCH] Add spread test cases (#247) * Update test cases for spread * Update the spread test cases * Add changelog * Add test case * Update summery * Update summary * Remove the test case for quantum model considerint we are still under discussion over them * Update the comments * Update the comments * Update the docs * Remove this case considering we have no clear conscencus on what it should do * Update the summery doc * Update the summery doc * Update the sumary * Reorgnized to sub-namespaces * Update the summary * Update the summary * Reorganize the test cases' positions --- .changeset/four-geckos-act.md | 5 + .../cadl-ranch-specs/cadl-ranch-summary.md | 93 ++++++++++++ .../http/parameters/spread/main.tsp | 141 ++++++++++++++++++ .../http/parameters/spread/mockapi.ts | 41 +++++ 4 files changed, 280 insertions(+) create mode 100644 .changeset/four-geckos-act.md create mode 100644 packages/cadl-ranch-specs/http/parameters/spread/main.tsp create mode 100644 packages/cadl-ranch-specs/http/parameters/spread/mockapi.ts diff --git a/.changeset/four-geckos-act.md b/.changeset/four-geckos-act.md new file mode 100644 index 000000000..541c0903d --- /dev/null +++ b/.changeset/four-geckos-act.md @@ -0,0 +1,5 @@ +--- +"@azure-tools/cadl-ranch-specs": patch +--- + +Add test case for spread operator diff --git a/packages/cadl-ranch-specs/cadl-ranch-summary.md b/packages/cadl-ranch-specs/cadl-ranch-summary.md index 46753e188..aaca65846 100644 --- a/packages/cadl-ranch-specs/cadl-ranch-summary.md +++ b/packages/cadl-ranch-specs/cadl-ranch-summary.md @@ -2228,6 +2228,99 @@ This test is testing sending a csv collection format array query parameters This test is testing sending a csv collection format array header parameters +### Parameters_Spread_Model_spreadAsRequestBody + +- Endpoint: `put /parameters/spread/model/request-body` + +Test case for spread named model. + +Should generate request body model named `BodyParameter`. +Should generate an operation like below: + +``` +spreadAsRequestBody(bodyParameter: BodyParameter) +``` + +Note the parameter name is guessed from the model name and it may vary by language. + +Expected request body: + +```json +{ "name": "foo" } +``` + +### Parameters_Spread_Alias_spreadAsRequestBody + +- Endpoint: `put /parameters/spread/alias/request-body` + +Test case for spread alias. + +Should not generate any model named `BodyParameter`. +Should generate an operation like: + +``` +spreadAsRequestBody(name: string) +``` + +Expected request body: + +```json +{ "name": "foo" } +``` + +### Parameters_Spread_Alias_spreadAsRequestParameter + +- Endpoint: `put /parameters/spread/alias/request-parameter/{id}` + +Test case for spread alias with path and header parameter. + +Should not generate any model named `RequestParameter`. +Should generate an operation like below: + +``` +spreadAsRequestParameter(id: string, x_ms_test_header: string, name: string) +``` + +Note the parameter name may be normalized and vary by language. + +Expected path parameter: id="1" +Expected header parameter: x-ms-test-header="bar" +Expected request body: + +```json +{ "name": "foo" } +``` + +### Parameters_Spread_Alias_spreadWithMultipleParameters + +- Endpoint: `put /parameters/spread/alias/multiple-parameters/{id}` + +Test case for spread alias including 6 parameters. May handle as property bag for these parameters. + +Should not generate any model named `AliasMultipleRequestParameters`. +Should generate an operation like below: + +``` +spreadWithMultipleParameters(id: string, x_ms_test_header: string, prop1: string, prop2: string, prop3: string, prop4: string, prop5: string, prop6: string) +``` + +Note it's also acceptable if some languages handle it as property bag. + +Expected path parameter: id="1" +Expected header parameter: x-ms-test-header="bar" +Expected request body: + +```json +{ + "prop1": "foo1", + "prop2": "foo2", + "prop3": "foo3", + "prop4": "foo4", + "prop5": "foo5", + "prop6": "foo6" +} +``` + ### ProjectedName_jsonProjection - Endpoint: `post /projection/json` diff --git a/packages/cadl-ranch-specs/http/parameters/spread/main.tsp b/packages/cadl-ranch-specs/http/parameters/spread/main.tsp new file mode 100644 index 000000000..71e8208c4 --- /dev/null +++ b/packages/cadl-ranch-specs/http/parameters/spread/main.tsp @@ -0,0 +1,141 @@ +import "@typespec/http"; +import "@azure-tools/cadl-ranch-expect"; +import "@azure-tools/typespec-client-generator-core"; + +using TypeSpec.Http; +using Azure.ClientGenerator.Core; + +@doc("Test for the spread operator.") +@supportedBy("dpg") +@scenarioService("/parameters/spread") +namespace Parameters.Spread; + +@route("/model") +@operationGroup +namespace Model { + @doc("This is a simple model.") + model BodyParameter { + name: string; + } + + @scenario + @scenarioDoc(""" + Test case for spread named model. + + Should generate request body model named `BodyParameter`. + Should generate an operation like below: + ``` + spreadAsRequestBody(bodyParameter: BodyParameter) + ``` + Note the parameter name is guessed from the model name and it may vary by language. + + Expected request body: + ```json + { "name": "foo" } + ``` + """) + @route("/request-body") + @put + op spreadAsRequestBody(...BodyParameter): NoContentResponse; +} + +@route("/alias") +@operationGroup +namespace Alias { + alias BodyParameter = { + name: string; + }; + + @scenario + @scenarioDoc(""" + Test case for spread alias. + + Should not generate any model named `BodyParameter`. + Should generate an operation like: + ``` + spreadAsRequestBody(name: string) + ``` + + Expected request body: + ```json + { "name": "foo" } + ``` + """) + @route("/request-body") + @put + op spreadAsRequestBody(...BodyParameter): NoContentResponse; + + alias RequestParameter = { + @path + id: string; + + @header + `x-ms-test-header`: string; + name: string; + }; + + @scenario + @scenarioDoc(""" + Test case for spread alias with path and header parameter. + + Should not generate any model named `RequestParameter`. + Should generate an operation like below: + ``` + spreadAsRequestParameter(id: string, x_ms_test_header: string, name: string) + ``` + Note the parameter name may be normalized and vary by language. + + Expected path parameter: id="1" + Expected header parameter: x-ms-test-header="bar" + Expected request body: + ```json + { "name": "foo" } + ``` + """) + @route("/request-parameter/{id}") + @put + op spreadAsRequestParameter(...RequestParameter): NoContentResponse; + + alias MultipleRequestParameters = { + @path + id: string; + + @header + `x-ms-test-header`: string; + prop1: string; + prop2: string; + prop3: string; + prop4: string; + prop5: string; + prop6: string; + }; + + @scenario + @scenarioDoc(""" + Test case for spread alias including 6 parameters. May handle as property bag for these parameters. + + Should not generate any model named `AliasMultipleRequestParameters`. + Should generate an operation like below: + ``` + spreadWithMultipleParameters(id: string, x_ms_test_header: string, prop1: string, prop2: string, prop3: string, prop4: string, prop5: string, prop6: string) + ``` + Note it's also acceptable if some languages handle it as property bag. + + Expected path parameter: id="1" + Expected header parameter: x-ms-test-header="bar" + Expected request body: + ```json + { + "prop1":"foo1", + "prop2":"foo2", + "prop3":"foo3", + "prop4":"foo4", + "prop5":"foo5", + "prop6":"foo6" + } + ``` + """) + @route("/multiple-parameters/{id}") + @put + op spreadWithMultipleParameters(...MultipleRequestParameters): NoContentResponse; +} diff --git a/packages/cadl-ranch-specs/http/parameters/spread/mockapi.ts b/packages/cadl-ranch-specs/http/parameters/spread/mockapi.ts new file mode 100644 index 000000000..ac5f417f0 --- /dev/null +++ b/packages/cadl-ranch-specs/http/parameters/spread/mockapi.ts @@ -0,0 +1,41 @@ +import { passOnSuccess, mockapi } from "@azure-tools/cadl-ranch-api"; +import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; + +export const Scenarios: Record = {}; + +Scenarios.Parameters_Spread_Model_spreadAsRequestBody = passOnSuccess( + mockapi.put("/parameters/spread/model/request-body", (req) => { + req.expect.bodyEquals({ name: "foo" }); + return { status: 204 }; + }), +); + +Scenarios.Parameters_Spread_Alias_spreadAsRequestBody = passOnSuccess( + mockapi.put("/parameters/spread/alias/request-body", (req) => { + req.expect.bodyEquals({ name: "foo" }); + return { status: 204 }; + }), +); + +Scenarios.Parameters_Spread_Alias_spreadAsRequestParameter = passOnSuccess( + mockapi.put("/parameters/spread/alias/request-parameter/1", (req) => { + req.expect.containsHeader("x-ms-test-header", "bar"); + req.expect.bodyEquals({ name: "foo" }); + return { status: 204 }; + }), +); + +Scenarios.Parameters_Spread_Alias_spreadWithMultipleParameters = passOnSuccess( + mockapi.put("/parameters/spread/alias/multiple-parameters/1", (req) => { + req.expect.containsHeader("x-ms-test-header", "bar"); + req.expect.bodyEquals({ + prop1: "foo1", + prop2: "foo2", + prop3: "foo3", + prop4: "foo4", + prop5: "foo5", + prop6: "foo6", + }); + return { status: 204 }; + }), +);