Skip to content

Commit 65db443

Browse files
committed
Add test for optional body
1 parent ca2988a commit 65db443

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

packages/typespec-autorest/test/arm/resources.test.ts

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepEqual, ok, strictEqual } from "assert";
1+
import { deepEqual, deepStrictEqual, ok, strictEqual } from "assert";
22
import { it } from "vitest";
33
import { openApiFor } from "../test-host.js";
44

@@ -376,7 +376,7 @@ it("excludes properties marked @invisible from the resource payload", async () =
376376
});
377377
});
378378

379-
it("allows resources with multiple endpoint using LegacyOperations", async () => {
379+
it("allows resources with multiple endpoints using LegacyOperations", async () => {
380380
const openApi = await openApiFor(`
381381
@armProviderNamespace
382382
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
@@ -540,3 +540,113 @@ it("allows resources with multiple endpoint using LegacyOperations", async () =>
540540
ok(resourceGroupOperations.put);
541541
ok(resourceGroupOperations.head);
542542
});
543+
it("allows action requests with optional body parameters", async () => {
544+
const openApi = await openApiFor(`
545+
@armProviderNamespace
546+
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
547+
namespace Microsoft.ContosoProviderhub;
548+
549+
/** A ContosoProviderHub resource */
550+
model Employee is TrackedResource<EmployeeProperties> {
551+
...ResourceNameParameter<Employee>;
552+
}
553+
554+
/** Employee properties */
555+
model EmployeeProperties {
556+
/** Age of employee */
557+
age?: int32;
558+
559+
/** City of employee */
560+
city?: string;
561+
562+
/** Profile of employee */
563+
@encode("base64url")
564+
profile?: bytes;
565+
566+
/** The status of the last operation. */
567+
@visibility(Lifecycle.Read)
568+
provisioningState?: ProvisioningState;
569+
}
570+
571+
/** The provisioning state of a resource. */
572+
@lroStatus
573+
union ProvisioningState {
574+
string,
575+
576+
/** The resource create request has been accepted */
577+
Accepted: "Accepted",
578+
579+
/** The resource is being provisioned */
580+
Provisioning: "Provisioning",
581+
582+
/** The resource is updating */
583+
Updating: "Updating",
584+
585+
/** Resource has been created. */
586+
Succeeded: "Succeeded",
587+
588+
/** Resource creation failed. */
589+
Failed: "Failed",
590+
591+
/** Resource creation was canceled. */
592+
Canceled: "Canceled",
593+
594+
/** The resource is being deleted */
595+
Deleting: "Deleting",
596+
}
597+
598+
/** Employee move request */
599+
model MoveRequest {
600+
/** The moving from location */
601+
from: string;
602+
603+
/** The moving to location */
604+
to: string;
605+
}
606+
607+
/** Employee move response */
608+
model MoveResponse {
609+
/** The status of the move */
610+
movingStatus: string;
611+
}
612+
613+
/** Create an optional request body parameter */
614+
model OptionalBody<T extends {}> {
615+
/** The request body */
616+
@body body?: T;
617+
}
618+
619+
interface Operations extends Azure.ResourceManager.Operations {}
620+
621+
@armResourceOperations
622+
interface Employees {
623+
get is ArmResourceRead<Employee>;
624+
createOrUpdate is ArmResourceCreateOrReplaceAsync<Employee>;
625+
update is ArmResourcePatchAsync<Employee, Employee>;
626+
delete is ArmResourceDeleteWithoutOkAsync<Employee>;
627+
list is ArmResourceListByParent<Employee>;
628+
listBySubscription is ArmListBySubscription<Employee>;
629+
630+
/** A sample resource action that move employee to different location */
631+
move is ArmResourceActionAsync<Employee, MoveRequest, MoveResponse, BodyParameter = OptionalBody<MoveRequest>>;
632+
633+
}
634+
`);
635+
636+
const resourceGroupPath =
637+
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContosoProviderhub/employees/{employeeName}";
638+
639+
const movePath = `${resourceGroupPath}/move`;
640+
const moveOperation = openApi.paths[movePath].post;
641+
ok(moveOperation);
642+
ok(moveOperation.parameters[4]);
643+
deepStrictEqual(moveOperation.parameters[4], {
644+
name: "body",
645+
in: "body",
646+
description: "The request body",
647+
required: false,
648+
schema: {
649+
$ref: "#/definitions/MoveRequest",
650+
},
651+
});
652+
});

0 commit comments

Comments
 (0)