Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pshao25 committed Sep 6, 2024
1 parent e2e5553 commit 3325fb8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ export async function emitArmResources(program: TypespecProgram, metadata: Metad
}
}

const multiPathResources = Object.keys(metadata.Resources).filter(key => key.endsWith("FixMe"));
const multiPathResources = Object.keys(metadata.Resources).filter((key) => key.endsWith("FixMe"));
for (const resource of multiPathResources) {
const originalName = resource.replace("FixMe", "");
const filePath = join(basePath, `${resource}.tsp`);
session.writeFile({ filename: filePath, content: `// You defined multiple pathes under the model ${originalName}. We currently don't support it. Please fix it manually.` });
session.writeFile({
filename: filePath,
content: `// You defined multiple pathes under the model ${originalName}. We currently don't support it. Please fix it manually.`,
});
}
}

Expand Down
12 changes: 9 additions & 3 deletions packages/extensions/openapi-to-typespec/src/emiters/emit-main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Metadata } from "../utils/resource-discovery";
import { getSession } from "../autorest-session";
import { generateServiceInformation } from "../generate/generate-service-information";
import { TypespecProgram } from "../interfaces";
import { getOptions } from "../options";
import { formatTypespecFile } from "../utils/format";
import { Metadata } from "../utils/resource-discovery";
const packageInfo = require("../../package.json");

export async function emitMain(filePath: string, program: TypespecProgram, metadata: Metadata | undefined): Promise<void> {
export async function emitMain(
filePath: string,
program: TypespecProgram,
metadata: Metadata | undefined,
): Promise<void> {
const { isArm } = getOptions();
const content = `${getHeaders()}\n${isArm ? getArmServiceInformation(program, metadata!) : getServiceInformation(program)}`;
const content = `${getHeaders()}\n${
isArm ? getArmServiceInformation(program, metadata!) : getServiceInformation(program)
}`;
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 @@ -65,8 +65,11 @@ export function populateSingletonRequestPath(set: OperationSet): void {
updatedSegments.push(segment);
} else {
const keyName = segment.replace(/^\{(\w+)\}$/, "$1");
const resourceKeyParameter = set.Operations[0].parameters?.find((p) => p.language.default.name === keyName || p.language.default.serializedName === keyName);
if (resourceKeyParameter === undefined) throw `Cannot find parameter ${keyName} in operation ${set.Operations[0].operationId}`;
const resourceKeyParameter = set.Operations[0].parameters?.find(
(p) => p.language.default.name === keyName || p.language.default.serializedName === keyName,
);
if (resourceKeyParameter === undefined)
throw `Cannot find parameter ${keyName} in operation ${set.Operations[0].operationId}`;

if (!isConstantSchema(resourceKeyParameter.schema)) {
updatedSegments.push(segment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export function parseMetadata(codeModel: CodeModel): Metadata {
for (const resourceSchemaName in operationSetsByResourceDataSchemaName) {
const operationSets = operationSetsByResourceDataSchemaName[resourceSchemaName];
if (operationSets.length > 1) {
logger().info(`We cannot support multi path with same model. Some operations will be lost. \nResource schema name: ${resourceSchemaName}.\nPath:\n${operationSets
logger().info(
`We cannot support multi path with same model. Some operations will be lost. \nResource schema name: ${resourceSchemaName}.\nPath:\n${operationSets
.map((o) => o.RequestPath)
.join("\n")}`);
.join("\n")}`,
);
resources[resourceSchemaName + "FixMe"] = {
Name: resourceSchemaName + "FixMe",
GetOperations: [],
Expand All @@ -96,7 +98,7 @@ export function parseMetadata(codeModel: CodeModel): Metadata {
IsManagementGroupResource: false,
IsExtensionResource: false,
IsSingletonResource: false,
}
};
}
resources[resourceSchemaName] = buildResource(
resourceSchemaName,
Expand Down Expand Up @@ -202,7 +204,7 @@ function buildResourceOperationFromOperation(operation: Operation, operationName
pagingMetadata = {
Method: operation.language.default.name,
ItemName: itemName,
NextLinkName: nextLinkName
NextLinkName: nextLinkName,
};
}

Expand Down
7 changes: 5 additions & 2 deletions packages/extensions/openapi-to-typespec/src/resource/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ export function isSingleton(set: OperationSet): boolean {
if (lastSegment.match(/^\{\w+\}$/) === null) return true;

const resourceKey = lastSegment.replace(/^\{(\w+)\}$/, "$1");
const resourceKeyParameter = set.Operations[0].parameters?.find((p) => p.language.default.name === resourceKey|| p.language.default.serializedName === resourceKey);
if (resourceKeyParameter === undefined) throw `Cannot find parameter ${resourceKey} in operation ${set.Operations[0].operationId}`;
const resourceKeyParameter = set.Operations[0].parameters?.find(
(p) => p.language.default.name === resourceKey || p.language.default.serializedName === resourceKey,
);
if (resourceKeyParameter === undefined)
throw `Cannot find parameter ${resourceKey} in operation ${set.Operations[0].operationId}`;
return isConstantSchema(resourceKeyParameter?.schema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function generate(root: string, path: string, debug = false, isFullCompati
overrideGuess = fileContent.includes("guessResourceKey: false");
}

const files = await readdir(join(dirname(path), "tsp-output"), {recursive: true});
const files = await readdir(join(dirname(path), "tsp-output"), { recursive: true });
for (const file of files) {
const fullPath = join(dirname(path), "tsp-output", file);
if (lstatSync(fullPath).isDirectory()) continue;
Expand Down

0 comments on commit 3325fb8

Please sign in to comment.