Skip to content

Commit

Permalink
fix error and warning
Browse files Browse the repository at this point in the history
  • Loading branch information
pshao25 committed May 17, 2024
1 parent 573d221 commit 0483fd2
Show file tree
Hide file tree
Showing 207 changed files with 2,779 additions and 2,192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import pluralize from "pluralize";
import { getArmCommonTypeVersion } from "../autorest-session";
import { getOptions } from "../options";
import { replaceGeneratedResourceObject } from "../transforms/transform-arm-resources";
import { generateDecorators } from "../utils/decorators";
import { generateAugmentedDecorators, generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { getModelPropertiesDeclarations } from "../utils/model-generation";
import { generateOperation } from "./generate-operations";
import { generateSuppressions } from "../utils/suppressions";

export function generateArmResource(resource: TspArmResource): string {
const definitions: string[] = [];
Expand All @@ -21,6 +22,10 @@ export function generateArmResource(resource: TspArmResource): string {

definitions.push("\n");

for (const a of resource.augmentDecorators ?? []) {
definitions.push(generateAugmentedDecorators(a.target!, [a]));
}

for (const o of resource.resourceOperations) {
for (const d of o.customizations ?? []) {
definitions.push(`${d}`);
Expand Down Expand Up @@ -61,8 +66,13 @@ function generateArmResourceModel(resource: TspArmResource): string {
resource.propertiesPropertyVisibility.includes("read") &&
resource.propertiesPropertyVisibility.includes("create"))
) {
definitions.push(`model ${resource.name} is ${resource.resourceKind}<${resource.propertiesModelName}> {`);
definitions.push(
`model ${resource.name} is Azure.ResourceManager.${resource.resourceKind}<${resource.propertiesModelName}> {`,
);

if (resource.keyExpression) {
definitions.push(`${resource.keyExpression}`);
}
definitions = [...definitions, ...getModelPropertiesDeclarations(resource.properties)];
} else {
definitions.push(
Expand All @@ -81,9 +91,12 @@ function generateArmResourceModel(resource: TspArmResource): string {
}
} else {
definitions.push(`@Azure.ResourceManager.Private.armResourceInternal(${resource.propertiesModelName})`);
definitions.push(`model ${resource.name} extends ${resource.resourceKind}Base {`);
definitions.push(`model ${resource.name} extends Foundations.${resource.resourceKind} {`);
}

if (resource.keyExpression) {
definitions.push(`${resource.keyExpression}`);
}
definitions = [...definitions, ...getModelPropertiesDeclarations(resource.properties)];

const propertyDoc = generateDocs({ doc: resource.propertiesPropertyDescription });
Expand Down Expand Up @@ -134,6 +147,9 @@ function generateArmResourceOperation(resource: TspArmResource): string {
definitions.push(`@operationId("${operation.operationId}")`);
definitions.push(`#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"`);
}
if (isFullCompatible && operation.suppressions) {
definitions.push(...generateSuppressions(operation.suppressions));
}
if (operation.kind === "ArmResourceExists") {
definitions.push(`op ${operation.name}(${operation.parameters.join(",")}): ${operation.responses.join("|")}`);
} else if (operation.templateParameters?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export function generateObject(typespecObject: TypespecObject) {
if (typespecObject.extendedParents?.length) {
const firstParent = typespecObject.extendedParents[0];
definitions.push(`model ${typespecObject.name} extends ${firstParent} {`);
} else if (typespecObject.spreadParents && typespecObject.spreadParents.length === 1) {
definitions.push(`model ${typespecObject.name} extends ${typespecObject.spreadParents[0]} {`);
} else if (typespecObject.alias) {
const { alias, params } = typespecObject.alias;

Expand All @@ -29,10 +27,8 @@ export function generateObject(typespecObject: TypespecObject) {
}

if (typespecObject.spreadParents?.length) {
if (typespecObject.extendedParents?.length || typespecObject.spreadParents.length > 1) {
for (const parent of typespecObject.spreadParents) {
definitions.push(`...${parent};`);
}
for (const parent of typespecObject.spreadParents) {
definitions.push(`...${parent};`);
}
}

Expand Down
11 changes: 9 additions & 2 deletions packages/extensions/openapi-to-typespec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export interface WithFixMe {
fixMe?: string[];
}

export interface WithSuppressDirectives {
suppressions?: WithSuppressDirective[];
}

export interface WithSuppressDirective {
suppressionCode?: string;
suppressionMessage?: string;
Expand All @@ -146,7 +150,7 @@ export interface TypespecParameter extends TypespecDataType {
defaultValue?: any;
}

export interface TypespecObjectProperty extends TypespecDataType {
export interface TypespecObjectProperty extends TypespecDataType, WithSuppressDirectives {
kind: "property";
isOptional: boolean;
type: string;
Expand All @@ -160,6 +164,7 @@ export interface TypespecDecorator extends WithFixMe, WithSuppressDirective {
arguments?: (string | number)[] | DecoratorArgument[];
module?: string;
namespace?: string;
target?: string;
}

export interface TypespecAlias {
Expand All @@ -175,6 +180,7 @@ export interface TypespecObject extends TypespecDataType {
extendedParents?: string[];
spreadParents?: string[];
decorators?: TypespecDecorator[];
augmentDecorators?: TypespecDecorator[];
clientDecorators?: TypespecDecorator[];
alias?: TypespecAlias;
}
Expand All @@ -201,7 +207,7 @@ export function isFirstLevelResource(value: string): value is FirstLevelResource
return FIRST_LEVEL_RESOURCE.includes(value as FirstLevelResource);
}

export interface TspArmResourceOperationBase extends WithDoc, WithFixMe {
export interface TspArmResourceOperationBase extends WithDoc, WithFixMe, WithSuppressDirectives {
kind: string;
name: string;
templateParameters?: string[];
Expand Down Expand Up @@ -252,6 +258,7 @@ export interface TspArmResourceExistsOperation extends TspArmResourceOperationBa

export interface TspArmResource extends TypespecObject {
resourceKind: ArmResourceKind;
keyExpression: string | undefined;
propertiesModelName: string;
propertiesPropertyRequired: boolean;
propertiesPropertyVisibility: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import {
import { isResponseSchema } from "../utils/schemas";
import { getTypespecType, transformObjectProperty } from "./transform-object";
import { transformParameter, transformRequest } from "./transform-operations";
import {
getSuppressionsForArmResourceDeleteAsync,
getSuppressionsForArmResourceDeleteSync,
} from "../utils/suppressions";
import { generateDocs, generateDocsContent } from "../utils/docs";

const generatedResourceObjects: Map<string, string> = new Map<string, string>();

Expand Down Expand Up @@ -111,12 +116,22 @@ export function transformTspArmResource(schema: ArmResourceSchema): TspArmResour
const otherOperations = operations[1];

const clientDecorators = buildResourceClientDecorators(schema, armResourceOperations, otherOperations);
const keyProperty = buildKeyProperty(schema);
const properties = [...getOtherProperties(schema, !getArmCommonTypeVersion())];
let keyExpression, augmentDecorators;
if (keyProperty.name === "name" && keyProperty.type === "string") {
keyExpression = buildKeyExpression(schema, keyProperty);
augmentDecorators = buildKeyAugmentDecorators(schema, keyProperty);
} else {
properties.unshift(keyProperty);
}

return {
fixMe,
resourceKind: getResourceKind(schema),
kind: "object",
properties: [buildKeyProperty(schema), ...getOtherProperties(schema, !getArmCommonTypeVersion())],
properties,
keyExpression,
name: schema.resourceMetadata.SwaggerModelName,
parents: [],
resourceParent: getParentResource(schema),
Expand All @@ -127,6 +142,7 @@ export function transformTspArmResource(schema: ArmResourceSchema): TspArmResour
doc: schema.language.default.description,
decorators,
clientDecorators,
augmentDecorators,
resourceOperations: armResourceOperations,
normalOperations: otherOperations,
optionalStandardProperties: getArmCommonTypeVersion() ? getResourceOptionalStandardProperties(schema) : [],
Expand Down Expand Up @@ -156,29 +172,29 @@ function getResourceOptionalStandardProperties(schema: ArmResourceSchema): strin
if (msi) {
let msiType;
if (msi.schema.language.default.name === "ManagedServiceIdentity") {
msiType = "Azure.ResourceManager.ManagedServiceIdentity";
msiType = "Azure.ResourceManager.ManagedServiceIdentityProperty";
} else if (msi.schema.language.default.name === "SystemAssignedServiceIdentity") {
msiType = "Azure.ResourceManager.ManagedSystemAssignedIdentity";
msiType = "Azure.ResourceManager.ManagedSystemAssignedIdentityProperty";
} else {
// TODO: handle non-standard property
msiType = "Azure.ResourceManager.ManagedServiceIdentity";
msiType = "Azure.ResourceManager.ManagedServiceIdentityProperty";
}
optionalStandardProperties.push(msiType);
}

if (schema.properties?.find((p) => p.serializedName === "sku")) {
// TODO: handle non-standard property
optionalStandardProperties.push("Azure.ResourceManager.ResourceSku");
optionalStandardProperties.push("Azure.ResourceManager.ResourceSkuProperty");
}

if (schema.properties?.find((p) => p.serializedName === "eTag")) {
// TODO: handle non-standard property
optionalStandardProperties.push("Azure.ResourceManager.EntityTag");
optionalStandardProperties.push("Azure.ResourceManager.EntityTagProperty");
}

if (schema.properties?.find((p) => p.serializedName === "plan")) {
// TODO: handle non-standard property
optionalStandardProperties.push("Azure.ResourceManager.ResourcePlan");
optionalStandardProperties.push("Azure.ResourceManager.ResourcePlanProperty");
}

return optionalStandardProperties;
Expand Down Expand Up @@ -360,7 +376,7 @@ function convertResourceUpdateOperation(
examples: swaggerOperation.extensions?.["x-ms-examples"],
customizations,
// To resolve auto-generate update model with proper visibility
decorators: [{ name: "parameterVisibility", arguments: ["read"] }],
decorators: [{ name: "parameterVisibility", arguments: [] }],
},
];
}
Expand All @@ -379,22 +395,30 @@ function convertResourceDeleteOperation(
const okResponse = swaggerOperation?.responses?.filter((o) => o.protocol.http?.statusCodes.includes("200"))?.[0];
const baseParameters = buildOperationBaseParameters(swaggerOperation, resourceMetadata);
const templateParameters = [resourceMetadata.SwaggerModelName];
const kind = isLongRunning
? okResponse
? "ArmResourceDeleteAsync"
: "ArmResourceDeleteWithoutOkAsync"
: "ArmResourceDeleteSync";
const suppressions =
kind === "ArmResourceDeleteAsync"
? getSuppressionsForArmResourceDeleteAsync()
: kind === "ArmResourceDeleteSync"
? getSuppressionsForArmResourceDeleteSync()
: undefined;
if (baseParameters) {
templateParameters.push(baseParameters);
}
return [
{
doc: operation.Description,
kind: isLongRunning
? okResponse
? "ArmResourceDeleteAsync"
: "ArmResourceDeleteWithoutOkAsync"
: "ArmResourceDeleteSync",
kind: kind,
name: getOperationName(operation.OperationID),
clientDecorators: getOperationClientDecorators(swaggerOperation),
operationId: operation.OperationID,
templateParameters,
examples: swaggerOperation.extensions?.["x-ms-examples"],
suppressions: suppressions,
},
];
}
Expand Down Expand Up @@ -810,6 +834,36 @@ function getSchemaResponseSchemaName(response: Response | undefined): string | u
return (response as SchemaResponse).schema.language.default.name;
}

function buildKeyExpression(schema: ArmResourceSchema, keyProperty: TypespecObjectProperty): string {
const namePattern = keyProperty.decorators?.find((d) => d.name === "pattern")?.arguments?.[0];
const keyName = keyProperty.decorators?.find((d) => d.name === "key")?.arguments?.[0];
const segmentName = keyProperty.decorators?.find((d) => d.name === "segment")?.arguments?.[0];
return `...ResourceNameParameter<
Resource = ${schema.resourceMetadata.SwaggerModelName}
${keyName ? `, KeyName = "${keyName}"` : ""}
${segmentName ? `, SegmentName = "${segmentName}"` : ""},
NamePattern = ${namePattern ? `"${namePattern}"` : `""`}
>`;
}

function buildKeyAugmentDecorators(
schema: ArmResourceSchema,
keyProperty: TypespecObjectProperty,
): TypespecDecorator[] | undefined {
return keyProperty.decorators
?.filter((d) => !["pattern", "key", "segment"].includes(d.name))
.filter((d) => !(d.name === "visibility" && d.arguments?.[0] === "read"))
.map((d) => {
d.target = `${schema.resourceMetadata.SwaggerModelName}.name`;
return d;
})
.concat({
name: "doc",
target: `${schema.resourceMetadata.SwaggerModelName}.name`,
arguments: [generateDocsContent(keyProperty)],
});
}

function buildKeyProperty(schema: ArmResourceSchema): TypespecObjectProperty {
let parameter;
if (!schema.resourceMetadata.IsSingletonResource) {
Expand Down
Loading

0 comments on commit 0483fd2

Please sign in to comment.