Skip to content

Commit

Permalink
Replace direct OpenAPI extension reference to add `x-ms-azure-resourc…
Browse files Browse the repository at this point in the history
…e` with internal decorator (#1123)

Removed direct reference to OpenAPI extension `x-ms-azure-resource` in
ARM library and replaced with
`@Azure.ResourceManager.Private.azureResourceBase` decorator. It is only
used internally on base resource types. `autorest` emitter has been
updated to check the decorator and still emit `x-ms-azure-resource`
extension in swagger.

---------

Co-authored-by: Timothee Guerin <[email protected]>
  • Loading branch information
allenjzhang and timotheeguerin authored Jul 10, 2024
1 parent b7fef1d commit 8a10bb1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-autorest"
- "@azure-tools/typespec-azure-resource-manager"
---

Removed direct reference to OpenAPI extension `x-ms-azure-resource` in ARM library and replaced with `@Azure.ResourceManager.Private.azureResourceBase` decorator. It is only used internally on base resource types. `autorest` emitter has been updated to check the decorator and still emit `x-ms-azure-resource` extension in swagger.
3 changes: 3 additions & 0 deletions packages/samples/common-types/src/types.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ op registerParams(
): void;

@@extension(ErrorDetail.details, "x-ms-identifiers", ["message", "target"]);

@@extension(Resource, "x-ms-azure-resource", true);
@@extension(ResourceModelWithAllowedPropertySet, "x-ms-azure-resource", true);
4 changes: 4 additions & 0 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
getArmCommonTypeOpenAPIRef,
isArmCommonType,
isAzureResource,
} from "@azure-tools/typespec-azure-resource-manager";
import { shouldFlattenProperty } from "@azure-tools/typespec-client-generator-core";
import {
Expand Down Expand Up @@ -1854,6 +1855,9 @@ export async function getOpenAPIForService(
function attachExtensions(type: Type, emitObject: any) {
// Attach any OpenAPI extensions
const extensions = getExtensions(program, type);
if (isAzureResource(program, type as Model)) {
emitObject["x-ms-azure-resource"] = true;
}
if (getAsEmbeddingVector(program, type as Model) !== undefined) {
emitObject["x-ms-embedding-vector"] = true;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/typespec-autorest/test/arm/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,18 @@ it("emits a scalar string with decorator parameter for resource", async () => {
type: "string",
});
});

it("emits x-ms-azure-resource for resource with @azureResourceBase", async () => {
const openApi = await openApiFor(`
@armProviderNamespace
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
namespace Microsoft.Contoso;
@doc("Widget resource")
@Azure.ResourceManager.Private.azureResourceBase
model Widget {
name: string;
}
`);
ok(openApi.definitions.Widget["x-ms-azure-resource"]);
});
15 changes: 8 additions & 7 deletions packages/typespec-azure-resource-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
- [#811](https://github.com/Azure/typespec-azure/pull/811) Remove dependency on `typespec-autorest` emitter
- [#432](https://github.com/Azure/typespec-azure/pull/432) Add support for values


## 0.42.1

### Bug Fixes

- [#868](https://github.com/Azure/typespec-azure/pull/868) Changing back `ManagedServiceIdentity.userAssignedIdentities` back to `Record<UserAssignedIdentity`. Adding ARM common-type references for on all Managed Identity models.


## 0.42.0

### Bug Fixes
Expand All @@ -50,7 +48,7 @@
### Deprecations

- [#762](https://github.com/Azure/typespec-azure/pull/762) Renaming internal TypeSpec ARM foundation model names to be consistent with ARM common-type definitions.
However, these are `Azure.Resource.Manager.Foundations` models that would not normally be used directly in service specs.
However, these are `Azure.Resource.Manager.Foundations` models that would not normally be used directly in service specs.

- Deprecate `Foundations.ArmResource`. `Foundations.Resource` should be used instead.

Expand All @@ -68,6 +66,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `ManagedServiceIdentity`. `ManagedServiceIdentityProperty` should be used instead.

Example:

```diff
-...ManagedServiceIdentity;
+...ManagedServiceIdentityProperty;
Expand All @@ -76,6 +75,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `ManagedSystemAssignedIdentity`. `ManagedSystemAssignedIdentityProperty` should be used instead.

Example:

```diff
-...ManagedSystemAssignedIdentity;
+...ManagedSystemAssignedIdentityProperty;
Expand All @@ -84,6 +84,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `EntityTag`. `EntityTagProperty` should be used instead.

Example:

```diff
-...EntityTag;
+...EntityTagProperty;
Expand All @@ -92,6 +93,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `ResourceKind`. `ResourceKindProperty` should be used instead.

Example:

```diff
-...ResourceKind;
+...ResourceKindProperty;
Expand All @@ -100,6 +102,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `ResourcePlan`. `ResourcePlanProperty` should be used instead.

Example:

```diff
-...ResourcePlan;
+...ResourcePlanProperty;
Expand All @@ -108,6 +111,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `ResourceSku`. `ResourceSkuProperty` should be used instead.

Example:

```diff
-...ResourceSku;
+...ResourceSkuProperty;
Expand All @@ -116,6 +120,7 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- Deprecate `ManagedBy`. `ManagedByProperty` should be used instead.

Example:

```diff
-...ManagedBy;
+...ManagedByProperty;
Expand All @@ -125,7 +130,6 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no

- [#521](https://github.com/Azure/typespec-azure/pull/521) Switching ProvisioningState from enum to Open union


## 0.41.0

### Bug Fixes
Expand All @@ -148,7 +152,6 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no

- [#407](https://github.com/Azure/typespec-azure/pull/407) Deprecate `ResourceIdentifier` in favor of new `Azure.Core.armResourceIdentifier`


## 0.40.0

### Bug Fixes
Expand All @@ -167,7 +170,6 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- [#298](https://github.com/Azure/typespec-azure/pull/298) ARM: add `arm-delete-response-codes` rule.
- [#317](https://github.com/Azure/typespec-azure/pull/317) Add ruleset `canonical-versioning` to Azure Resource Manager library.


## 0.39.1

### Patch Changes
Expand All @@ -181,7 +183,6 @@ However, these are `Azure.Resource.Manager.Foundations` models that would not no
- ebfe639: Fix Location resource issue, add mechanism for additional path segments
- 148eee4: Update references to ARM, Add template customization parameters, add migration docs


## 0.38.0

Wed, 24 Jan 2024 05:47:19 GMT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ using Azure.ResourceManager.CommonTypes.Private;

namespace Azure.ResourceManager.CommonTypes;

@@azureResourceBase(Resource);
@@azureResourceBase(ResourceModelWithAllowedPropertySet);

/** Common resource fields that are returned in the response for all Azure Resource Manager resources. */
@@armCommonDefinition(Resource, "Resource", Azure.ResourceManager.CommonTypes.Versions.v3);
@@armCommonDefinition(Resource, "Resource", Azure.ResourceManager.CommonTypes.Versions.v4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ using Azure.Core;
namespace Azure.ResourceManager.CommonTypes;

/** Common fields that are returned in the response for all Azure Resource Manager resources */
@extension("x-ms-azure-resource", true)
model Resource {
/** Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName} */
@typeChangedFrom(Versions.v4, string)
Expand Down Expand Up @@ -60,7 +59,6 @@ model ExtensionResource extends Resource {}
/**
* The resource model definition containing the full set of allowed properties for a resource. Except properties bag, there cannot be a top level property outside of this set.
*/
@extension("x-ms-azure-resource", true)
model ResourceModelWithAllowedPropertySet extends TrackedResource {
/** The fully qualified resource ID of the resource that manages this resource. Indicates if this resource is managed by another Azure resource.
* If this is present, complete mode deployment will not delete the resource if it is removed from the template since it is managed by another resource. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ extern dec defaultResourceKeySegmentName(
* Note, this is intended for internal use only for now.
*/
extern dec enforceConstraint(target: Operation | Model, sourceType: Model, constraintType: Model);

/**
* This decorator is used to identify Azure Resource Manager resource. In generated
* swagger definition, it will be marked with `x-ms-azure-resource`.
*
* It is *not* meant to be used directly by a spec author,
*/
extern dec azureResourceBase(target: Model);
2 changes: 2 additions & 0 deletions packages/typespec-azure-resource-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export {
type ArmCommonTypeVersions,
} from "./common-types.js";

export { isAzureResource } from "./private.decorators.js";

export * from "./namespace.js";
export * from "./operations.js";
export * from "./resource.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,12 @@ function hasProperty(program: Program, model: Model): boolean {
if (model.baseModel) return hasProperty(program, model.baseModel);
return false;
}

export function $azureResourceBase(context: DecoratorContext, resourceType: Model) {
context.program.stateMap(ArmStateKeys.azureResourceBase).set(resourceType, true);
}

export function isAzureResource(program: Program, resourceType: Model): boolean {
const isResourceBase = program.stateMap(ArmStateKeys.azureResourceBase).get(resourceType);
return isResourceBase ?? false;
}
1 change: 1 addition & 0 deletions packages/typespec-azure-resource-manager/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const ArmStateKeys = {
armBuiltInResource: azureResourceManagerCreateStateSymbol("armExternalResource"),

// private.decorator.ts
azureResourceBase: azureResourceManagerCreateStateSymbol("azureResourceBase"),
armCommonDefinitions: azureResourceManagerCreateStateSymbol("armCommonDefinitions"),
armCommonParameters: azureResourceManagerCreateStateSymbol("armCommonParameters"),
armCommonTypesVersions: azureResourceManagerCreateStateSymbol("armCommonTypesVersions"),
Expand Down

0 comments on commit 8a10bb1

Please sign in to comment.