diff --git a/.changeset/rich-comics-draw.md b/.changeset/rich-comics-draw.md new file mode 100644 index 000000000..3f1913b19 --- /dev/null +++ b/.changeset/rich-comics-draw.md @@ -0,0 +1,5 @@ +--- +"@azure-tools/cadl-ranch-specs": minor +--- + +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 fbaf634ef..795350c99 100644 --- a/packages/cadl-ranch-specs/cadl-ranch-summary.md +++ b/packages/cadl-ranch-specs/cadl-ranch-summary.md @@ -2488,6 +2488,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` 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 6709a0799..93517f349 100644 --- a/packages/cadl-ranch-specs/http/parameters/body-optionality/mockapi.ts +++ b/packages/cadl-ranch-specs/http/parameters/body-optionality/mockapi.ts @@ -58,3 +58,32 @@ Scenarios.Parameters_BodyOptionality_requiredImplicit = createServerTests( name: "foo", }, ); + +Scenarios.Parameters_BodyOptionality_OptionalityOrdering = passOnSuccess([ + { + uri: "/parameters/body-optionality/optional-ordering/startwithequired", + method: "head", + request: { + body: { + start: "required", + }, + }, + response: { + status: 204, + }, + kind: "MockApiDefinition", + }, + { + uri: "/parameters/body-optionality/optional-ordering/startwithoptional", + method: "head", + request: { + body: { + end: "required", + }, + }, + response: { + status: 204, + }, + kind: "MockApiDefinition", + }, +]);