Skip to content

Commit

Permalink
Fix issues in converter tool (#5022)
Browse files Browse the repository at this point in the history
1. update ruleset in tspconfig.yaml
2. Some operations are not calculated as resource operations
3. Some duplicate operations in the same interface
4. interface Operations extends Azure.ResourceManager.Operations should
be generated according to swagger
5. Take variables into consideration when calculating parent.

---------

Co-authored-by: Pan Shao <[email protected]>
  • Loading branch information
pshao25 and Pan Shao authored Sep 25, 2024
1 parent ff93b8b commit cdf589f
Show file tree
Hide file tree
Showing 52 changed files with 921 additions and 519 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@autorest/openapi-to-typespec",
"comment": "Fix some issues in convertion tool",
"type": "patch"
}
],
"packageName": "@autorest/openapi-to-typespec"
}
41 changes: 34 additions & 7 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/extensions/openapi-to-typespec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"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",
"webpack-cli": "~5.1.4",
"webpack": "~5.89.0",
"@typescript-eslint/eslint-plugin": "^6.11.0",
Expand Down
17 changes: 15 additions & 2 deletions packages/extensions/openapi-to-typespec/src/emiters/emit-models.ts
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 { getNamespaceStatement } from "../utils/namespace";
import { getNamespace, getNamespaceStatement } from "../utils/namespace";

export async function emitModels(filePath: string, program: TypespecProgram): Promise<void> {
const content = generateModels(program);
Expand Down Expand Up @@ -33,7 +33,9 @@ function generateModels(program: TypespecProgram) {
namespaces,
"\n",
getNamespaceStatement(program),
isArm ? "\ninterface Operations extends Azure.ResourceManager.Operations {} \n" : "\n",
isArm && containsListOperation(program)
? "\ninterface Operations extends Azure.ResourceManager.Operations {} \n"
: "\n",
enums,
"\n",
objects,
Expand All @@ -45,3 +47,14 @@ function flattenEnums(enums: TypespecEnum[]) {
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 @@ -51,7 +51,7 @@ options:
}
linter:
extends:
- "@azure-tools/typespec-azure-resource-manager/all"
- "@azure-tools/typespec-azure-rulesets/resource-manager"
`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/extensions/openapi-to-typespec/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function processConverter(host: AutorestExtensionHost) {
const { isArm } = getOptions();
let metadata = undefined;
if (isArm) {
// await host.writeFile({ filename: "codeModel.yaml", content: serialize(codeModel, codeModelSchema)} );
// await host.writeFile({ filename: "codeModel.yaml", content: serialize(codeModel, codeModelSchema) });
metadata = parseMetadata(codeModel);
await host.writeFile({ filename: "resources.json", content: JSON.stringify(metadata, null, 2) });
pretransformArmResources(codeModel, metadata);
Expand Down
20 changes: 17 additions & 3 deletions packages/extensions/openapi-to-typespec/src/resource/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function isScopedPath(path: string): boolean {

export function isSingleton(set: OperationSet): boolean {
const lastSegment = getLastSegment(set.RequestPath);
if (lastSegment.match(/^\{\w+\}$/) === null) return true;
if (!isVariable(lastSegment)) return true;

const resourceKey = lastSegment.replace(/^\{(\w+)\}$/, "$1");
const resourceKeyParameter = set.Operations[0].parameters?.find(
Expand All @@ -79,8 +79,22 @@ export function isSingleton(set: OperationSet): boolean {
export function pathIncludes(path1: string, path2: string): boolean {
const lowerPath1 = path1.toLowerCase();
const lowerPath2 = path2.toLowerCase();
// TO-DO: escape the variable case
return lowerPath1.includes(lowerPath2);
const segments1 = lowerPath1.split("/");
const segments2 = lowerPath2.split("/");
if (segments2.length > segments1.length) return false;

// If the segment is a variable, then different variable names are still equivalent
for (let index = 0; index < segments2.length; ++index) {
if (isVariable(segments1[index])) {
if (!isVariable(segments2[index])) return false;
} else if (segments1[index] !== segments2[index]) return false;
}
return true;
}

// {variableName}
function isVariable(segment: string): boolean {
return segment.match(/^\{\w+\}$/) !== null;
}

function getLastSegment(path: string): string {
Expand Down
Loading

0 comments on commit cdf589f

Please sign in to comment.