Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @armCommonDefinition decorator to be used on enums and unions #1116

Merged
merged 7 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Link CommonTypes enums and unions to the swagger common types
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@azure-tools/typespec-autorest"
---

Resolve Arm Common Definitions for enums and unions as well
8 changes: 7 additions & 1 deletion packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,13 @@ export async function getOpenAPIForService(
};
}

if (isArmCommonType(type) && (type.kind === "Model" || type.kind === "ModelProperty")) {
if (
isArmCommonType(type) &&
(type.kind === "Model" ||
type.kind === "ModelProperty" ||
type.kind === "Enum" ||
type.kind === "Union")
) {
const ref = getArmCommonTypeOpenAPIRef(program, type, {
version: context.version,
service: context.service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ alias ArmCommonTypeVersionSpec = {
* @param referenceFile Reference file
*/
extern dec armCommonDefinition(
target: Model,
target: Model | Enum | Union,
definitionName?: valueof string,
version?: valueof EnumMember | ArmCommonTypeVersionSpec | string,
referenceFile?: valueof string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,34 @@ namespace Azure.ResourceManager.CommonTypes;
Azure.ResourceManager.CommonTypes.Versions.v5,
"managedidentity.json"
);

@@armCommonDefinition(ManagedServiceIdentityType,
"ManagedServiceIdentityType",
Azure.ResourceManager.CommonTypes.Versions.v3,
"managedidentity.json"
);
@@armCommonDefinition(ManagedServiceIdentityType,
"ManagedServiceIdentityType",
Azure.ResourceManager.CommonTypes.Versions.v4,
"managedidentity.json"
);
@@armCommonDefinition(ManagedServiceIdentityType,
"ManagedServiceIdentityType",
Azure.ResourceManager.CommonTypes.Versions.v5,
"managedidentity.json"
);
@@armCommonDefinition(SystemAssignedServiceIdentityType,
"SystemAssignedServiceIdentityType",
Azure.ResourceManager.CommonTypes.Versions.v3,
"managedidentity.json"
);
@@armCommonDefinition(SystemAssignedServiceIdentityType,
"SystemAssignedServiceIdentityType",
Azure.ResourceManager.CommonTypes.Versions.v4,
"managedidentity.json"
);
@@armCommonDefinition(SystemAssignedServiceIdentityType,
"SystemAssignedServiceIdentityType",
Azure.ResourceManager.CommonTypes.Versions.v5,
"managedidentity.json"
);
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,36 @@ namespace Azure.ResourceManager.CommonTypes;
"privatelinks.json"
);

// PrivateEndpointServiceConnectionStatus
@@armCommonDefinition(PrivateEndpointServiceConnectionStatus,
"PrivateEndpointServiceConnectionStatus",
Azure.ResourceManager.CommonTypes.Versions.v3,
"privatelinks.json"
);
@@armCommonDefinition(PrivateEndpointServiceConnectionStatus,
"PrivateEndpointServiceConnectionStatus",
Azure.ResourceManager.CommonTypes.Versions.v4
);
@@armCommonDefinition(PrivateEndpointServiceConnectionStatus,
"PrivateEndpointServiceConnectionStatus",
Azure.ResourceManager.CommonTypes.Versions.v5
);

// PrivateEndpointServiceConnectionStatus
@@armCommonDefinition(PrivateEndpointConnectionProvisioningState,
"PrivateEndpointConnectionProvisioningState",
Azure.ResourceManager.CommonTypes.Versions.v3,
"privatelinks.json"
);
@@armCommonDefinition(PrivateEndpointConnectionProvisioningState,
"PrivateEndpointConnectionProvisioningState",
Azure.ResourceManager.CommonTypes.Versions.v4
);
@@armCommonDefinition(PrivateEndpointConnectionProvisioningState,
"PrivateEndpointConnectionProvisioningState",
Azure.ResourceManager.CommonTypes.Versions.v5
);

/** PrivateEndpointConnectionParameter */
@@CommonTypes.Private.armCommonParameter(PrivateEndpointConnectionParameter.name,
"PrivateEndpointConnectionName",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ namespace Azure.ResourceManager.CommonTypes;
Azure.ResourceManager.CommonTypes.Versions.v5
);

@@armCommonDefinition(SkuTier, "SkuTier", Azure.ResourceManager.CommonTypes.Versions.v3);
@@armCommonDefinition(SkuTier, "SkuTier", Azure.ResourceManager.CommonTypes.Versions.v4);
@@armCommonDefinition(SkuTier, "SkuTier", Azure.ResourceManager.CommonTypes.Versions.v5);

// -- Parameters
/** ApiVersionParameter */
@@armCommonParameter(ApiVersionParameter.apiVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Program,
Service,
Type,
Union,
isTypeSpecValueTypeOf,
} from "@typespec/compiler";
import { $useDependency, getVersion } from "@typespec/versioning";
Expand Down Expand Up @@ -69,7 +70,7 @@ export function getArmCommonTypesVersionFromString(
*/
export function isArmCommonType(entity: Type): boolean {
const commonDecorators = ["$armCommonDefinition", "$armCommonParameter"];
if (isTypeSpecValueTypeOf(entity, ["Model", "ModelProperty"])) {
if (isTypeSpecValueTypeOf(entity, ["Model", "ModelProperty", "Enum", "Union"])) {
return commonDecorators.some((commonDecorator) =>
entity.decorators.some((d) => d.decorator.name === commonDecorator)
);
Expand Down Expand Up @@ -134,7 +135,7 @@ export function getArmCommonTypesVersion(
*/
export function getArmCommonTypeOpenAPIRef(
program: Program,
entity: Model | ModelProperty,
entity: Model | ModelProperty | Enum | Union,
params: ArmCommonTypesResolutionOptions
): string | undefined {
const [record, diagnostics] = findArmCommonTypeRecord(program, entity, params);
Expand All @@ -161,7 +162,7 @@ export interface ArmCommonTypesResolutionOptions {

export function findArmCommonTypeRecord(
program: Program,
entity: Model | ModelProperty,
entity: Model | ModelProperty | Enum | Union,
params: ArmCommonTypesResolutionOptions
): [ArmCommonTypeRecord | undefined, readonly Diagnostic[]] {
const { records, defaultKey } = getCommonTypeRecords(program, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Model,
ModelProperty,
Program,
Union,
} from "@typespec/compiler";
import { ArmStateKeys } from "./state.js";

Expand All @@ -18,7 +19,7 @@ function getArmTypesPath(program: Program): string {

function storeCommonTypeRecord(
context: DecoratorContext,
entity: Model | ModelProperty,
entity: Model | ModelProperty | Enum | Union,
kind: "definitions" | "parameters",
name: string,
version?: string | EnumValue | ArmCommonTypeVersionSpec,
Expand Down Expand Up @@ -69,7 +70,7 @@ export interface ArmCommonTypeRecords {

export function getCommonTypeRecords(
program: Program,
entity: Model | ModelProperty
entity: Model | ModelProperty | Enum | Union
): ArmCommonTypeRecords {
return program.stateMap(ArmStateKeys.armCommonDefinitions).get(entity) ?? { records: {} };
}
Expand Down Expand Up @@ -114,14 +115,14 @@ export function $armCommonParameter(
*/
export function $armCommonDefinition(
context: DecoratorContext,
entity: Model,
entity: Model | Enum | Union,
definitionName?: string,
version?: string | EnumValue | ArmCommonTypeVersionSpec,
referenceFile?: string
): void {
// Use the name of the model type if not specified
if (!definitionName) {
definitionName = entity.name;
definitionName = entity.name!;
}

storeCommonTypeRecord(context, entity, "definitions", definitionName, version, referenceFile);
Expand Down
Loading