Skip to content

Commit

Permalink
Update and respond to review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
markcowl committed Jul 11, 2024
1 parent 04928d7 commit 8579746
Show file tree
Hide file tree
Showing 12 changed files with 11,517 additions and 15,472 deletions.
2 changes: 1 addition & 1 deletion .chronus/changes/fix-patch-2024-5-13-3-36-28.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ packages:
- "@azure-tools/typespec-autorest"
---

Fix #383: Generate patch models for common-types
Fix patch models for common-types
2 changes: 1 addition & 1 deletion .chronus/changes/resource-patch-2024-5-27-2-24-51.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Fix ArmResourcePatch templates and incorporate common-types updates Fix #859
Fix ArmResourcePatch templates and incorporate common-types updates
20 changes: 0 additions & 20 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,6 @@
"order": 2
}
},
{
"type": "node",
"request": "launch",
"name": "Compile AllProps",
"program": "${workspaceRoot}/core/packages/compiler/entrypoints/cli.js",
"args": ["compile", ".", "--emit=@azure-tools/typespec-autorest"],
"smartStep": true,
"sourceMaps": true,
"skipFiles": ["<node_internals>/**/*.js"],
"outFiles": [
"${workspaceFolder}/packages/*/dist/**/*.js",
"${workspaceFolder}/packages/*/dist-dev/**/*.js",
"${workspaceFolder}/core/packages/*/dist/**/*.js",
"${workspaceFolder}/core/packages/*/dist-dev/**/*.js"
],
"cwd": "${workspaceRoot}/packages/samples/specs/resource-manager/arm-library",
"presentation": {
"order": 2
}
},
{
"name": "Regenerate .tmlanguage",
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion core
Submodule core updated 836 files
8 changes: 7 additions & 1 deletion docs/emitters/typespec-autorest/reference/emitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ Create read-only property schema for lro status

Determine whether and how to emit x-ms-long-running-operation-options for lro resolution

### `arm-resource-flattening`

**Type:** `boolean`

Back-compat flag. If true, continue to emit `x-ms-client-flatten` in for some of the ARM resource properties.

### `emit-common-types-schema`

**Type:** `"reference-only" | "for-visibility-changes"`
**Type:** `"never" | "for-visibility-changes"`

Determine whether and how to emit schemas for common-types rather than referencing them
Original file line number Diff line number Diff line change
Expand Up @@ -1811,9 +1811,14 @@
"userAssignedIdentities": {
"type": "object",
"description": "The identities assigned to this resource by the user.",
"x-nullable": true,
"additionalProperties": {
"$ref": "../../../../../../../specs/resource-manager/common-types/v5/managedidentity.json#/definitions/UserAssignedIdentity"
"allOf": [
{
"$ref": "../../../../../../../specs/resource-manager/common-types/v5/managedidentity.json#/definitions/UserAssignedIdentity"
}
],
"type": "object",
"x-nullable": true
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/typespec-autorest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ Create read-only property schema for lro status

Determine whether and how to emit x-ms-long-running-operation-options for lro resolution

#### `arm-resource-flattening`

**Type:** `boolean`

Back-compat flag. If true, continue to emit `x-ms-client-flatten` in for some of the ARM resource properties.

#### `emit-common-types-schema`

**Type:** `"reference-only" | "for-visibility-changes"`
**Type:** `"never" | "for-visibility-changes"`

Determine whether and how to emit schemas for common-types rather than referencing them

Expand Down
4 changes: 2 additions & 2 deletions packages/typespec-autorest/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export interface AutorestEmitterOptions {
* Determines whether and how to emit schemas for common-types
* @default "for-visibility-changes"
*/
"emit-common-types-schema"?: "reference-only" | "for-visibility-changes";
"emit-common-types-schema"?: "never" | "for-visibility-changes";
}

const EmitterOptionsSchema: JSONSchemaType<AutorestEmitterOptions> = {
Expand Down Expand Up @@ -213,7 +213,7 @@ const EmitterOptionsSchema: JSONSchemaType<AutorestEmitterOptions> = {
},
"emit-common-types-schema": {
type: "string",
enum: ["reference-only", "for-visibility-changes"],
enum: ["never", "for-visibility-changes"],
nullable: true,
default: "for-visibility-changes",
description:
Expand Down
6 changes: 3 additions & 3 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ export interface AutorestDocumentEmitterOptions {
* readOnly property ARM resource flattening
*/
readonly armResourceFlattening?: boolean;

/**
* Determines whether and how to emit schema for arm common-types
* @default "for-visibility-only"
*/
readonly emitCommonTypesSchema?: "reference-only" | "for-visibility-changes";
readonly emitCommonTypesSchema?: "never" | "for-visibility-changes";
}

/**
Expand Down Expand Up @@ -898,7 +898,7 @@ export async function getOpenAPIForService(
const ref = resolveExternalRef(type);
if (ref) {
if (
options.emitCommonTypesSchema === "reference-only" ||
options.emitCommonTypesSchema === "never" ||
!metadataInfo.isTransformed(type, schemaContext.visibility)
) {
return ref;
Expand Down
177 changes: 177 additions & 0 deletions packages/typespec-autorest/test/arm/arm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,180 @@ it("no x-ms-client-flatten emitted with default configuration", async () => {
undefined
);
});
it("generates PATCH bodies for custom patch of common resource envelope mixins", async () => {
const openapi = await openApiFor(
`@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
@armProviderNamespace
@armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.v5)
namespace Microsoft.PatchTest;
interface Operations extends Azure.ResourceManager.Operations {}
/** The all properties resource */
model AllPropertiesResource is TrackedResource<AllPropertiesProperties> {
...ResourceNameParameter<AllPropertiesResource>;
...EncryptionProperty;
...EntityTagProperty;
...ExtendedLocationProperty;
...ManagedByProperty;
...ResourceKindProperty;
...ResourcePlanProperty;
...ResourceSkuProperty;
...ManagedServiceIdentityProperty;
}
/** rp-specific property bag */
model AllPropertiesProperties {
...DefaultProvisioningStateProperty;
/** An optional Property */
optProp?: string;
/** A required property */
reqProperty: string;
}
/** rp-specific property bag */
model SystemAssignedProperties {
...DefaultProvisioningStateProperty;
/** An optional Property */
optProp?: string;
/** A required property */
reqProperty: string;
}
/** The SystemAssignedResource */
model SystemAssignedResource is TrackedResource<SystemAssignedProperties> {
...ResourceNameParameter<SystemAssignedResource>;
...ManagedSystemAssignedIdentityProperty;
}
@armResourceOperations(AllPropertiesResource)
interface AllProperties {
get is ArmResourceRead<AllPropertiesResource>;
put is ArmResourceCreateOrReplaceAsync<AllPropertiesResource>;
update is ArmCustomPatchAsync<AllPropertiesResource, AllPropertiesResource>;
delete is ArmResourceDeleteWithoutOkAsync<AllPropertiesResource>;
}
@armResourceOperations(SystemAssignedResource)
interface AssignedOperations {
get is ArmResourceRead<SystemAssignedResource>;
put is ArmResourceCreateOrReplaceAsync<SystemAssignedResource>;
update is ArmCustomPatchAsync<SystemAssignedResource, SystemAssignedResource>;
delete is ArmResourceDeleteWithoutOkAsync<SystemAssignedResource>;
}
`
);

const all = openapi.definitions["AllPropertiesResourceUpdate"];
const system = openapi.definitions["SystemAssignedResourceUpdate"];
ok(all);
ok(system);
deepStrictEqual(
all["properties"]["plan"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.PlanUpdate"
);
deepStrictEqual(
all["properties"]["sku"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.SkuUpdate"
);
deepStrictEqual(
all["properties"]["identity"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.ManagedServiceIdentityUpdate"
);
deepStrictEqual(
system["properties"]["identity"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.SystemAssignedServiceIdentityUpdate"
);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.PlanUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.SkuUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.ManagedServiceIdentityUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.SystemAssignedServiceIdentityUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.TrackedResourceUpdate"]);
deepStrictEqual(
openapi.definitions["Azure.ResourceManager.CommonTypes.ResourceModelWithAllowedPropertySet"],
undefined
);
});
it("generates PATCH bodies for resource patch of common resource envelope mixins", async () => {
const openapi = await openApiFor(
`@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
@armProviderNamespace
@armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.v5)
namespace Microsoft.PatchTest;
interface Operations extends Azure.ResourceManager.Operations {}
/** The all properties resource */
model AllPropertiesResource is TrackedResource<AllPropertiesProperties> {
...ResourceNameParameter<AllPropertiesResource>;
...EncryptionProperty;
...EntityTagProperty;
...ExtendedLocationProperty;
...ManagedByProperty;
...ResourceKindProperty;
...ResourcePlanProperty;
...ResourceSkuProperty;
...ManagedServiceIdentityProperty;
}
/** rp-specific property bag */
model AllPropertiesProperties {
...DefaultProvisioningStateProperty;
/** An optional Property */
optProp?: string;
/** A required property */
reqProperty: string;
}
/** rp-specific property bag */
model SystemAssignedProperties {
...DefaultProvisioningStateProperty;
/** An optional Property */
optProp?: string;
/** A required property */
reqProperty: string;
}
/** The SystemAssignedResource */
model SystemAssignedResource is TrackedResource<SystemAssignedProperties> {
...ResourceNameParameter<SystemAssignedResource>;
...ManagedSystemAssignedIdentityProperty;
}
@armResourceOperations(AllPropertiesResource)
interface AllProperties {
get is ArmResourceRead<AllPropertiesResource>;
put is ArmResourceCreateOrReplaceAsync<AllPropertiesResource>;
update is ArmResourcePatchAsync<AllPropertiesResource, AllPropertiesProperties>;
delete is ArmResourceDeleteWithoutOkAsync<AllPropertiesResource>;
}
@armResourceOperations(SystemAssignedResource)
interface AssignedOperations {
get is ArmResourceRead<SystemAssignedResource>;
put is ArmResourceCreateOrReplaceAsync<SystemAssignedResource>;
update is ArmResourcePatchAsync<SystemAssignedResource, SystemAssignedProperties>;
delete is ArmResourceDeleteWithoutOkAsync<SystemAssignedResource>;
}
`
);

const all = openapi.definitions["AllPropertiesResourceUpdate"];
const system = openapi.definitions["SystemAssignedResourceUpdate"];
ok(all);
ok(system);
deepStrictEqual(
all["properties"]["plan"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.PlanUpdate"
);
deepStrictEqual(
all["properties"]["sku"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.SkuUpdate"
);
deepStrictEqual(
all["properties"]["identity"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.ManagedServiceIdentityUpdate"
);
deepStrictEqual(
system["properties"]["identity"]["$ref"],
"#/definitions/Azure.ResourceManager.CommonTypes.SystemAssignedServiceIdentityUpdate"
);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.PlanUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.SkuUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.ManagedServiceIdentityUpdate"]);
ok(openapi.definitions["Azure.ResourceManager.CommonTypes.SystemAssignedServiceIdentityUpdate"]);
});
4 changes: 2 additions & 2 deletions packages/typespec-autorest/test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ op test(): void;
}
`;

it("emits only schema references with 'reference-only' setting", async () => {
it("emits only schema references with 'never' setting", async () => {
const output = await openapiWithOptions(commonCode, {
"emit-common-types-schema": "reference-only",
"emit-common-types-schema": "never",
});
ok(output.definitions);
ok(output.definitions["WidgetUpdate"]);
Expand Down
Loading

0 comments on commit 8579746

Please sign in to comment.