Skip to content

Commit

Permalink
remove useless enum, refine example name, refine check name availabil…
Browse files Browse the repository at this point in the history
…ity operation
  • Loading branch information
tadelesh committed Jan 12, 2024
1 parent a4ebf16 commit 70f743e
Show file tree
Hide file tree
Showing 673 changed files with 64 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ function generateExamples(
) {
const count = _.keys(examples).length;
for (const [title, example] of _.entries(examples)) {
if (!example.operationId) {
example.operationId = operationId;
example.title = title;
}
example.operationId = operationId;
example.title = title;
let filename = operationId;
if (count > 1) {
filename = `${filename}_${Case.pascal(title)}`;
if (title.startsWith(filename)) {
filename = title;
} else {
const suffix = Case.train(title).replaceAll("-", "_");
filename = `${filename}_${suffix}`;
}
}
generatedExamples[filename] = JSON.stringify(example, null, 2);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/extensions/openapi-to-typespec/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { transformEnum } from "./transforms/transform-choices";
import { getTypespecType, transformObject } from "./transforms/transform-object";
import { transformOperationGroup } from "./transforms/transform-operations";
import { transformServiceInformation } from "./transforms/transform-service-information";
import { ArmResourceSchema, filterArmModels, isResourceSchema } from "./utils/resource-discovery";
import { ArmResourceSchema, filterArmEnums, filterArmModels, isResourceSchema } from "./utils/resource-discovery";
import { isChoiceSchema } from "./utils/schemas";

const models: Map<CodeModel, TypespecProgram> = new Map();
Expand Down Expand Up @@ -41,7 +41,7 @@ export function transformDataType(schema: Schema, codeModel: CodeModel): Typespe
}

function transformModel(codeModel: CodeModel): TypespecProgram {
const caldEnums = [...(codeModel.schemas.choices ?? []), ...(codeModel.schemas.sealedChoices ?? [])]
const typespecEnums = [...(codeModel.schemas.choices ?? []), ...(codeModel.schemas.sealedChoices ?? [])]
.filter((c) => c.language.default.name !== "Versions")
.map((c) => transformEnum(c, codeModel));

Expand Down Expand Up @@ -70,7 +70,7 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
return {
serviceInformation,
models: {
enums: caldEnums,
enums: isArm ? filterArmEnums(typespecEnums) : typespecEnums,
objects: isArm ? filterArmModels(codeModel, typespecObjects) : typespecObjects,
armResources,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ function convertResourceActionOperations(
}
}

const request = buildOperationBodyRequest(swaggerOperation, resourceMetadata) ?? "void";
const request = buildOperationBodyRequest(swaggerOperation) ?? "void";
const baseParameters = buildOperationBaseParameters(swaggerOperation, resourceMetadata);
let kind;
if (!okResponse) {
Expand Down Expand Up @@ -537,14 +537,29 @@ function convertCheckNameAvailabilityOperations(
for (const operation of resourceMetadata.OperationsFromSubscriptionExtension) {
if (operation.Path.includes("/checkNameAvailability")) {
const swaggerOperation = operations[operation.OperationID];

converted.push({
doc: operation.Description,
kind: "checkGlobalNameAvailability",
name: getOperationName(operation.OperationID),
operationId: operation.OperationID,
examples: swaggerOperation.extensions?.["x-ms-examples"],
});
const response = (swaggerOperation?.responses?.filter(
(o) => o.protocol.http?.statusCodes.includes("200"),
)?.[0] as SchemaResponse).schema?.language.default.name ?? "CheckNameAvailabilityResponse";
const request = buildOperationBodyRequest(swaggerOperation) ?? "CheckNameAvailabilityRequest";
if (operation.Path.includes("/locations/")) {
converted.push({
doc: operation.Description,
kind: "checkLocalNameAvailability",
name: getOperationName(operation.OperationID),
operationId: operation.OperationID,
examples: swaggerOperation.extensions?.["x-ms-examples"],
templateParameters: [request, response],
});
} else {
converted.push({
doc: operation.Description,
kind: "checkGlobalNameAvailability",
name: getOperationName(operation.OperationID),
operationId: operation.OperationID,
examples: swaggerOperation.extensions?.["x-ms-examples"],
templateParameters: [request, response],
});
}
}
}
}
Expand Down Expand Up @@ -630,7 +645,7 @@ function getOperationGroupName(name: string | undefined): string {
}
}

function buildOperationBodyRequest(operation: Operation, resource: ArmResource): string | undefined {
function buildOperationBodyRequest(operation: Operation): string | undefined {
const codeModel = getSession().model;
const bodyParam: Parameter | undefined = operation.requests?.[0].parameters?.find(
(p) => p.protocol.http?.in === "body",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from "fs";
import { join } from "path";
import { CodeModel, ObjectSchema, Operation, SchemaResponse } from "@autorest/codemodel";
import { getSession } from "../autorest-session";
import { TypespecObject, TspArmResource } from "../interfaces";
import { TypespecObject, TspArmResource, TypespecEnum } from "../interfaces";
import { isGeneratedResourceObject } from "../transforms/transform-arm-resources";
export interface _ArmResourceOperation {
Name: string;
Expand Down Expand Up @@ -197,6 +197,21 @@ export function filterArmModels(codeModel: CodeModel, objects: TypespecObject[])
return objects.filter((o) => !filtered.includes(o.name) && !isGeneratedResourceObject(o.name));
}

const _ArmCoreEnums = [
"CreatedByType",
"Origin",
"ActionType",
"ManagedIdentityType",
"ManagedSystemIdentityType",
"SkuTier",
"CheckNameAvailabilityRequest",
"CheckNameAvailabilityReason",
];

export function filterArmEnums(enums: TypespecEnum[]): TypespecEnum[] {
return enums.filter((e) => !_ArmCoreEnums.includes(e.name));
}

export function isTspArmResource(schema: TypespecObject): schema is TspArmResource {
return Boolean((schema as TspArmResource).resourceKind);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,12 @@ namespace Azure.ResourceManager.AgFoodPlatform;

interface Operations extends Azure.ResourceManager.Operations {}

@doc("The reason why the given name is not available.")
enum CheckNameAvailabilityReason {
Invalid,
AlreadyExists,
}

@doc("Enum for different types of AuthCredentials supported.")
enum AuthCredentialsKind {
OAuthClientCredentials,
ApiKeyAuthCredentials,
}

@doc("The type of identity that created the resource.")
enum CreatedByType {
User,
Application,
ManagedIdentity,
Key,
}

@doc("Data Manager For Agriculture instance provisioning state.")
enum ProvisioningState {
Creating,
Expand Down Expand Up @@ -62,18 +48,6 @@ enum PrivateEndpointConnectionProvisioningState {
Failed,
}

@doc("The intended executor of the operation; as in Resource Based Access Control (RBAC) and audit logs UX. Default value is \"user,system\"")
enum Origin {
user,
system,
`user,system`,
}

@doc("Enum. Indicates the action type. \"Internal\" refers to actions that are for internal only APIs.")
enum ActionType {
Internal,
}

@doc("The check availability request body.")
model CheckNameAvailabilityRequest {
@doc("The name of the resource for which availability needs to be checked.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ enum RecurrenceType {
Monthly,
}

@doc("Action that should be applied.")
enum ActionType {
AddActionGroups,
RemoveAllActionGroups,
}

@doc("The type of identity that created the resource.")
enum CreatedByType {
User,
Application,
ManagedIdentity,
Key,
}

enum Identifier {
MonitorServiceList,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ interface AnalysisServicesServers {
#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"
@doc("Check the name availability in the target location.")
@operationId("Servers_CheckNameAvailability")
checkNameAvailability is checkGlobalNameAvailability;
checkNameAvailability is checkLocalNameAvailability<
CheckServerNameAvailabilityParameters,
CheckServerNameAvailabilityResult
>;

#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"
@operationId("Servers_ListSkusForExisting")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ enum ProvisioningState {
Scaling,
}

@doc("The name of the Azure pricing tier to which the SKU applies.")
enum SkuTier {
Development,
Basic,
Standard,
}

@doc("The managed mode of the server (0 = not managed, 1 = managed).")
enum ManagedMode {
`0`: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ interface ApiManagementServiceResources {
#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"
@doc("Checks availability and correctness of a name for an API Management service.")
@operationId("ApiManagementService_CheckNameAvailability")
checkNameAvailability is checkGlobalNameAvailability;
checkNameAvailability is checkGlobalNameAvailability<
ApiManagementServiceCheckNameAvailabilityParameters,
ApiManagementServiceNameAvailabilityResult
>;

#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"
@operationId("Api_ListByTags")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,6 @@ enum Method {
POST,
}

@doc("The origin of the issue.")
enum Origin {
Local,
Inbound,
Outbound,
}

@doc("The severity of the issue.")
enum Severity {
Error,
Expand Down Expand Up @@ -348,14 +341,6 @@ enum ApimIdentityType {
None,
}

@doc("The type of identity that created the resource.")
enum CreatedByType {
User,
Application,
ManagedIdentity,
Key,
}

enum TemplateName {
applicationApprovedNotificationMessage,
accountClosedDeveloper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ namespace Microsoft.IntegrationSpaces;

interface Operations extends Azure.ResourceManager.Operations {}

@doc("The intended executor of the operation; as in Resource Based Access Control (RBAC) and audit logs UX. Default value is \"user,system\"")
enum Origin {
user,
system,
`user,system`,
}

@doc("Enum. Indicates the action type. \"Internal\" refers to actions that are for internal only APIs.")
enum ActionType {
Internal,
}

@doc("The status of the current operation.")
enum ProvisioningState {
@doc("Resource has been created.") Succeeded,
Expand All @@ -35,14 +23,6 @@ enum ProvisioningState {
Accepted,
}

@doc("The type of identity that created the resource.")
enum CreatedByType {
User,
Application,
ManagedIdentity,
Key,
}

@doc("The properties of space.")
model SpaceResourceProperties {
@doc("The status of the last operation.")
Expand Down
Loading

0 comments on commit 70f743e

Please sign in to comment.