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

upgrade compiler version to 0.60 #5025

Merged
merged 7 commits into from
Oct 8, 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,10 @@
{
"changes": [
{
"packageName": "@autorest/openapi-to-typespec",
"comment": "Upgrade compiler to 0.60.0",
"type": "patch"
}
],
"packageName": "@autorest/openapi-to-typespec"
}
316 changes: 152 additions & 164 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions packages/extensions/openapi-to-typespec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
"@azure-tools/codegen": "~2.10.0",
"@autorest/extension-base": "~3.6.0",
"@autorest/codemodel": "~4.20.0",
"@typespec/compiler": "^0.59.0",
"@typespec/rest": "^0.59.0",
"@typespec/http": "^0.59.0",
"@typespec/versioning": "^0.59.0",
"@typespec/prettier-plugin-typespec": "^0.59.0",
"@azure-tools/typespec-azure-core": "^0.45.0",
"@azure-tools/typespec-autorest": "^0.45.0",
"@azure-tools/typespec-azure-resource-manager": "^0.45.0",
"@typespec/openapi": "^0.59.0",
"@typespec/openapi3": "^0.59.0",
"@typespec/compiler": "^0.60.0",
"@typespec/rest": "^0.60.0",
"@typespec/http": "^0.60.0",
"@typespec/versioning": "^0.60.1",
"@typespec/prettier-plugin-typespec": "^0.60.0",
"@azure-tools/typespec-azure-core": "^0.46.0",
"@azure-tools/typespec-autorest": "^0.46.0",
"@azure-tools/typespec-azure-resource-manager": "^0.46.0",
"@typespec/openapi": "^0.60.0",
"@typespec/openapi3": "^0.60.0",
"prettier": "~3.1.0",
"lodash": "~4.17.20",
"pluralize": "^8.0.0",
Expand All @@ -68,9 +68,9 @@
"fs-extra": "^10.1.0",
"@types/fs-extra": "^9.0.13",
"chalk": "^4.1.0",
"@azure-tools/typespec-autorest": "^0.45.0",
"@azure-tools/typespec-client-generator-core": "^0.45.0",
"@azure-tools/typespec-azure-rulesets": "^0.45.0",
"@azure-tools/typespec-autorest": "^0.46.0",
"@azure-tools/typespec-client-generator-core": "^0.46.0",
"@azure-tools/typespec-azure-rulesets": "^0.46.0",
"webpack-cli": "~5.1.4",
"webpack": "~5.89.0",
"@typescript-eslint/eslint-plugin": "^6.11.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export async function emitMain(
const { isArm } = getOptions();
const content = `${getHeaders()}\n${
isArm ? getArmServiceInformation(program, metadata!) : getServiceInformation(program)
}${
isArm && program.containsListOperation ? "\n\ninterface Operations extends Azure.ResourceManager.Operations {}" : ""
}`;
const session = getSession();
session.writeFile({ filename: filePath, content: await formatTypespecFile(content, filePath) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TypespecEnum, TypespecProgram } from "../interfaces";
import { getOptions } from "../options";
import { formatTypespecFile } from "../utils/format";
import { getModelsImports } from "../utils/imports";
import { getNamespace, getNamespaceStatement } from "../utils/namespace";
import { getNamespaceStatement } from "../utils/namespace";

export async function emitModels(filePath: string, program: TypespecProgram): Promise<void> {
const content = generateModels(program);
Expand All @@ -23,38 +23,13 @@ function generateModels(program: TypespecProgram) {
"\n",
);

const isArm = getOptions().isArm;

const enums = flattenEnums(models.enums).join("");
const objects = models.objects.map(generateObject).join("\n\n");
return [
imports,
"\n",
namespaces,
"\n",
getNamespaceStatement(program),
isArm && containsListOperation(program)
? "\ninterface Operations extends Azure.ResourceManager.Operations {} \n"
: "\n",
enums,
"\n",
objects,
].join("\n");
return [imports, "\n", namespaces, "\n", getNamespaceStatement(program), "\n", enums, "\n", objects].join("\n");
}

function flattenEnums(enums: TypespecEnum[]) {
return enums.reduce<string[]>((a, c) => {
return [...a, ...generateEnums(c)];
}, []);
}

function containsListOperation(program: TypespecProgram): boolean {
const providerNamespace = getNamespace(program);
const listOperationRoute = `/providers/${providerNamespace}/operations`;
return (
program.operationGroups
.flatMap((g) => g.operations)
.map((o) => o.route)
.find((r) => r === listOperationRoute) !== undefined
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ function generateArmResourceOperation(resource: TspArmResource): string {
if (
isFullCompatible &&
operation.operationId &&
(operation.operationId !== getGeneratedOperationId(formalOperationGroupName, operation.name) ||
operation.kind === "ArmResourceListByParent")
operation.operationId !== getGeneratedOperationId(formalOperationGroupName, operation.name)
) {
definitions.push(`@operationId("${operation.operationId}")`);
definitions.push(`#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"`);
definitions.push(`#suppress "@azure-tools/typespec-azure-core/no-openapi" "non-standard operations"`);
}
if (isFullCompatible && operation.suppressions) {
definitions.push(...generateSuppressions(operation.suppressions));
Expand All @@ -127,7 +126,7 @@ function generateArmResourceOperation(resource: TspArmResource): string {
operation.operationId !== getGeneratedOperationId(formalOperationGroupName, operation.name)
) {
definitions.push(`@operationId("${operation.operationId}")`);
definitions.push(`#suppress "@azure-tools/typespec-azure-core/no-operation-id" "For backward compatibility"`);
definitions.push(`#suppress "@azure-tools/typespec-azure-core/no-openapi" "non-standard operations"`);
}
definitions.push(generateOperation(operation as TypespecOperation));
definitions.push("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { TypespecEnum } from "../interfaces";
import { getOptions } from "../options";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { generateSuppressions } from "../utils/suppressions";
import {
generateSuppressionForDocumentRequired,
generateSuppressionForNoEnum,
generateSuppressions,
} from "../utils/suppressions";

export function generateEnums(typespecEnum: TypespecEnum) {
const { isFullCompatible } = getOptions();
const definitions: string[] = [];
const doc = generateDocs(typespecEnum);
definitions.push(doc);
definitions.push(doc.length > 0 || !isFullCompatible ? doc : `${generateSuppressionForDocumentRequired()}\n`);

const isExtensible = typespecEnum.isExtensible && !["ApiVersion"].includes(typespecEnum.name);
if (!isExtensible && isFullCompatible) definitions.push(`${generateSuppressionForNoEnum()}\n`);

for (const fixme of typespecEnum.fixMe ?? []) {
definitions.push(`\n${fixme}`);
Expand All @@ -27,8 +34,10 @@ export function generateEnums(typespecEnum: TypespecEnum) {
${typespecEnum.choiceType},\n
${typespecEnum.members
.map((m) => {
const doc = generateDocs(m);
const kv = `"${m.name}": ${m.value}`;
return `${generateDocs(m)}${kv}`;
if (doc.length > 0 || !isFullCompatible) return `${doc}${kv}`;
else return `${generateSuppressionForDocumentRequired()}\n${kv}`;
})
.join(", ")}
}\n\n`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TypespecOperation, TypespecOperationGroup, TypespecParameter } from "../interfaces";
import { getOptions } from "../options";
import { replaceGeneratedResourceObject } from "../transforms/transform-arm-resources";
import { generateDecorators } from "../utils/decorators";
import { generateDocs, generateSummary } from "../utils/docs";
import { generateParameter } from "./generate-parameter";

Expand All @@ -20,6 +21,9 @@ export function generateOperation(operation: TypespecOperation, operationGroup?:
statements.push(fixme);
}

if (operation.decorators) {
statements.push(generateDecorators(operation.decorators));
}
if (isArm) {
statements.push(`@route("${route}")`);
statements.push(
Expand Down
2 changes: 2 additions & 0 deletions packages/extensions/openapi-to-typespec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface TypespecProgram {
models: Models;
operationGroups: TypespecOperationGroup[];
serviceInformation: ServiceInformation;
containsListOperation: boolean;
}

export interface TypespecOptions {
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface TypespecOperation extends WithDoc, WithSummary, WithFixMe {
operationId?: string;
examples?: Record<string, Record<string, unknown>>;
clientDecorators?: TypespecDecorator[];
decorators?: TypespecDecorator[];
}

export type ResourceKind =
Expand Down
8 changes: 7 additions & 1 deletion packages/extensions/openapi-to-typespec/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
.filter((c) => c.language.default.name !== "Versions")
.map((c) => transformEnum(c, codeModel));

const { isArm } = getOptions();
const { isArm, namespace } = getOptions();

// objects need to be converted first because they are used in operation convertion
const typespecObjects = (codeModel.schemas.objects ?? []).map((o) => transformObject(o, codeModel));
Expand All @@ -67,6 +67,7 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
}
}

const listOperationRoute = `/providers/${namespace}/operations`;
return {
serviceInformation,
models: {
Expand All @@ -75,5 +76,10 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
armResources,
},
operationGroups: typespecOperationGroups,
containsListOperation:
codeModel.operationGroups
.flatMap((g) => g.operations)
.map((o) => o.requests?.[0].protocol.http?.path)
.find((r) => r === listOperationRoute) !== undefined,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export function createCSharpNameDecorator(schema: RenamableSchema): TypespecDeco
arguments: [schema.language.csharp!.name, "csharp"],
};
}
export function createClientNameDecorator(target: string, value: string): TypespecDecorator {
return {
name: "clientName",
module: "@azure-tools/typespec-client-generator-core",
namespace: "Azure.ClientGenerator.Core",
arguments: [target, value],
};
}

function parseNewCSharpNameAndSetToSchema(schema: RenamableSchema, renameValue: string) {
const newName = parseNewName(renameValue);
Expand Down
Loading
Loading