From 8244e3cf93b2e5d6f23f12bcfb829454f6a541eb Mon Sep 17 00:00:00 2001 From: mcgallan <1186003288@qq.com> Date: Fri, 11 Oct 2024 15:29:57 +0800 Subject: [PATCH] update --- .changeset/rich-comics-draw.md | 2 +- .../cadl-ranch-specs/cadl-ranch-summary.md | 64 ++++++++++--------- .../http/parameters/body-optionality/main.tsp | 38 +++++++++++ .../parameters/body-optionality/mockapi.ts | 18 ++++++ .../parameters/query-optionality/main.tsp | 59 ----------------- .../parameters/query-optionality/mockapi.ts | 19 ------ 6 files changed, 92 insertions(+), 108 deletions(-) delete mode 100644 packages/cadl-ranch-specs/http/parameters/query-optionality/main.tsp delete mode 100644 packages/cadl-ranch-specs/http/parameters/query-optionality/mockapi.ts diff --git a/.changeset/rich-comics-draw.md b/.changeset/rich-comics-draw.md index 4fbd96a75..3f1913b19 100644 --- a/.changeset/rich-comics-draw.md +++ b/.changeset/rich-comics-draw.md @@ -2,4 +2,4 @@ "@azure-tools/cadl-ranch-specs": minor --- -Add test in parameters/query-optional. +Add optional parameter ordering test in parameter\body-optionality. diff --git a/packages/cadl-ranch-specs/cadl-ranch-summary.md b/packages/cadl-ranch-specs/cadl-ranch-summary.md index cd532a470..e1f5529df 100644 --- a/packages/cadl-ranch-specs/cadl-ranch-summary.md +++ b/packages/cadl-ranch-specs/cadl-ranch-summary.md @@ -2489,6 +2489,41 @@ Expected request body for `set` Expected no request body for `omit` +### Parameters_BodyOptionality_OptionalityOrdering + +- Endpoints: + - `head /parameters/body-optionality/optional-ordering/startwithequired` + - `head /parameters/body-optionality/optional-ordering/startwithoptional` + +Test scenario for using a combination of required parameters first and optional parameters later. + +Should generate an operation like below: + +``` +orderingWithRequiredStart(start: string, end?: string) +``` + +Expected parameter: api-version=2022-12-01-preview +Expected request body: + +```json +{ "start": "required" } +``` + +Another scenario using a combination of optional parameters first and required parameters later + +Should generate an operation like below: + +``` +orderingWithOptionalStart(end: string, start?: string) +``` + +Expected request body: + +```json +{ "end": "required" } +``` + ### Parameters_BodyOptionality_requiredExplicit - Endpoint: `post /parameters/body-optionality/required-explicit` @@ -2549,35 +2584,6 @@ This test is testing sending a ssv collection format array query parameters This test is testing sending a tsv collection format array query parameters -### Parameters_QueryOptionality_OrderingWithOptionalStart - -- Endpoint: `head /parameters/query-optionality/startwithoptional` - -Test scenarios for using a combination of optional query parameters first and required query parameters later - -Should generate an operation like below: - -``` -orderingWithOptionalStart(end: string, start?: string) -``` - -Expected query parameter: end=required - -### Parameters_QueryOptionality_OrderingWithRequiredStart - -- Endpoint: `head /parameters/query-optionality/startwithequired` - -Test scenarios for using a combination of required query parameters first and optional query parameters later. - -Should generate an operation like below: - -``` -orderingWithRequiredStart(start: string, end?: string) -``` - -Expected query parameter: api-version=2022-12-01-preview -Expected query parameter: start=required - ### Parameters_Spread_Alias_spreadAsRequestBody - Endpoint: `put /parameters/spread/alias/request-body` diff --git a/packages/cadl-ranch-specs/http/parameters/body-optionality/main.tsp b/packages/cadl-ranch-specs/http/parameters/body-optionality/main.tsp index ff5914172..25d635e74 100644 --- a/packages/cadl-ranch-specs/http/parameters/body-optionality/main.tsp +++ b/packages/cadl-ranch-specs/http/parameters/body-optionality/main.tsp @@ -50,6 +50,44 @@ namespace OptionalExplicit { op omit(@body body?: BodyModel): NoContentResponse; } +@scenario +@scenarioDoc(""" + Test scenario for using a combination of required parameters first and optional parameters later. + + Should generate an operation like below: + ``` + orderingWithRequiredStart(start: string, end?: string) + ``` + + Expected parameter: api-version=2022-12-01-preview + Expected request body: + ```json + { "start": "required" } + ``` + + Another scenario using a combination of optional parameters first and required parameters later + + Should generate an operation like below: + ``` + orderingWithOptionalStart(end: string, start?: string) + ``` + Expected request body: + ```json + { "end": "required" } + ``` + """) +@operationGroup +@route("/optional-ordering") +namespace OptionalityOrdering { + @route("/startwithequired") + @head + op OrderingWithRequiredStart(start: string, end?: string): void; + + @route("/startwithoptional") + @head + op OrderingWithOptionalStart(start?: string, end: string): void; +} + @scenario @scenarioDoc(""" Scenario defining how an implicit required body parameter is specified. diff --git a/packages/cadl-ranch-specs/http/parameters/body-optionality/mockapi.ts b/packages/cadl-ranch-specs/http/parameters/body-optionality/mockapi.ts index 3918943cb..4cc75fe07 100644 --- a/packages/cadl-ranch-specs/http/parameters/body-optionality/mockapi.ts +++ b/packages/cadl-ranch-specs/http/parameters/body-optionality/mockapi.ts @@ -27,3 +27,21 @@ Scenarios.Parameters_BodyOptionality_requiredImplicit = passOnSuccess( return { status: 204 }; }), ); + +Scenarios.Parameters_BodyOptionality_OptionalityOrdering = passOnSuccess([ + mockapi.head("/parameters/body-optionality/optional-ordering/startwithequired", (req) => { + req.expect.bodyEquals({ start: "required" }); + return { status: 204 }; + }), + mockapi.head("/parameters/body-optionality/optional-ordering/startwithoptional", (req) => { + req.expect.bodyEquals({ end: "required" }); + return { status: 204 }; + }), +]); + +Scenarios.Parameters_BodyOptionality_OrderingWithOptionalStart = passOnSuccess( + mockapi.head("/parameters/query-optionality/startwithoptional", (req) => { + req.expect.bodyEquals({ end: "required" }); + return { status: 204 }; + }), +); diff --git a/packages/cadl-ranch-specs/http/parameters/query-optionality/main.tsp b/packages/cadl-ranch-specs/http/parameters/query-optionality/main.tsp deleted file mode 100644 index c080fb53f..000000000 --- a/packages/cadl-ranch-specs/http/parameters/query-optionality/main.tsp +++ /dev/null @@ -1,59 +0,0 @@ -import "@azure-tools/cadl-ranch-expect"; -import "@azure-tools/typespec-azure-core"; -import "@azure-tools/typespec-client-generator-core"; -import "@typespec/http"; -import "@typespec/rest"; -import "@typespec/versioning"; - -using TypeSpec.Http; -using Azure.Core; -using global.Azure.Core.Traits; -using Azure.ClientGenerator.Core; -using TypeSpec.Versioning; - -#suppress "@azure-tools/typespec-azure-core/casing-style" "For spec" -@doc("Test describing optionality of the query.") -@scenarioService( - "/parameters/query-optionality", - { - versioned: Versions, - } -) -namespace Parameters.QueryOptionality; - -@doc("The API version.") -enum Versions { - @doc("The 2022-12-01-preview version.") - @useDependency(global.Azure.Core.Versions.v1_0_Preview_2) - v2022_12_01_preview: "2022-12-01-preview", -} - -@scenario -@scenarioDoc(""" - Test scenarios for using a combination of required query parameters first and optional query parameters later. - - Should generate an operation like below: - ``` - orderingWithRequiredStart(start: string, end?: string) - ``` - - Expected query parameter: api-version=2022-12-01-preview - Expected query parameter: start=required - """) -@route("/startwithequired") -@head -op OrderingWithRequiredStart(@query start: string, @query end?: string, @query("api-version") apiVersion: string): void; - -@scenario -@scenarioDoc(""" - Test scenarios for using a combination of optional query parameters first and required query parameters later - - Should generate an operation like below: - ``` - orderingWithOptionalStart(end: string, start?: string) - ``` - Expected query parameter: end=required - """) -@route("/startwithoptional") -@head -op OrderingWithOptionalStart(@query start?: string, @query end: string): void; diff --git a/packages/cadl-ranch-specs/http/parameters/query-optionality/mockapi.ts b/packages/cadl-ranch-specs/http/parameters/query-optionality/mockapi.ts deleted file mode 100644 index a34a4bff8..000000000 --- a/packages/cadl-ranch-specs/http/parameters/query-optionality/mockapi.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { passOnSuccess, mockapi } from "@azure-tools/cadl-ranch-api"; -import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api"; - -export const Scenarios: Record = {}; - -Scenarios.Parameters_QueryOptionality_OrderingWithRequiredStart = passOnSuccess( - mockapi.head("/parameters/query-optionality/startwithrequired", (req) => { - req.expect.containsQueryParam("api-version", "2022-12-01-preview"); - req.expect.containsQueryParam("start", "required"); - return { status: 204 }; - }), -); - -Scenarios.Parameters_QueryOptionality_OrderingWithOptionalStart = passOnSuccess( - mockapi.head("/parameters/query-optionality/startwithoptional", (req) => { - req.expect.containsQueryParam("end", "required"); - return { status: 204 }; - }), -);