Skip to content

Commit

Permalink
Fix: openapi description version validation (#5396)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome authored Sep 12, 2024
1 parent 420ac54 commit 03607d5
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 135 deletions.
4 changes: 4 additions & 0 deletions vscode/microsoft-kiota/src/generateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function generateClient(context: vscode.ExtensionContext,
operation: ConsumerOperation,
workingDirectory: string = getWorkspaceJsonDirectory()
): Promise<KiotaLogEntry[] | undefined> {
try {
return connectToKiota<KiotaLogEntry[]>(context, async (connection) => {
const request = new rpc.RequestType1<GenerationConfiguration, KiotaLogEntry[], void>(
"Generate"
Expand Down Expand Up @@ -51,4 +52,7 @@ export function generateClient(context: vscode.ExtensionContext,
} as GenerationConfiguration,
);
}, workingDirectory);
} catch (error) {
return Promise.resolve(undefined);
}
};
8 changes: 6 additions & 2 deletions vscode/microsoft-kiota/src/generatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { KiotaPluginType } from "./enums";
import { connectToKiota, ConsumerOperation, GenerationConfiguration, KiotaLogEntry } from "./kiotaInterop";
import { getWorkspaceJsonDirectory } from "./util";

export function generatePlugin(context: vscode.ExtensionContext,
export function generatePlugin(context: vscode.ExtensionContext,
descriptionPath: string,
output: string,
pluginTypes: KiotaPluginType[],
Expand All @@ -15,7 +15,8 @@ export function generatePlugin(context: vscode.ExtensionContext,
cleanOutput: boolean,
disableValidationRules: string[],
operation: ConsumerOperation,
workingDirectory: string = getWorkspaceJsonDirectory() ): Promise<KiotaLogEntry[] | undefined> {
workingDirectory: string = getWorkspaceJsonDirectory()): Promise<KiotaLogEntry[] | undefined> {
try {
return connectToKiota<KiotaLogEntry[]>(context, async (connection) => {
const request = new rpc.RequestType1<GenerationConfiguration, KiotaLogEntry[], void>(
"GeneratePlugin"
Expand All @@ -36,4 +37,7 @@ export function generatePlugin(context: vscode.ExtensionContext,
} as GenerationConfiguration,
);
}, workingDirectory);
} catch (error) {
return Promise.resolve(undefined);
}
};
4 changes: 4 additions & 0 deletions vscode/microsoft-kiota/src/getKiotaVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as rpc from "vscode-jsonrpc/node";
import * as vscode from "vscode";

export function getKiotaVersion(context: vscode.ExtensionContext, kiotaOutputChannel: vscode.LogOutputChannel): Promise<string | undefined> {
try {
return connectToKiota<string>(context, async (connection) => {
const request = new rpc.RequestType0<string, void>("GetVersion");
const result = await connection.sendRequest(request);
Expand All @@ -17,4 +18,7 @@ export function getKiotaVersion(context: vscode.ExtensionContext, kiotaOutputCha
kiotaOutputChannel.show();
return undefined;
});
} catch (error) {
return Promise.resolve(undefined);
}
};
49 changes: 28 additions & 21 deletions vscode/microsoft-kiota/src/getLanguageInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,44 @@ import * as vscode from "vscode";

let _languageInformation: LanguagesInformation | undefined; // doesn't change over the lifecycle of the extension
export async function getLanguageInformation(context: vscode.ExtensionContext): Promise<LanguagesInformation | undefined> {
if(_languageInformation)
{
return _languageInformation;
}
const result = await getLanguageInformationInternal(context);
if (result) {
_languageInformation = result;
}
return result;
if (_languageInformation) {
return _languageInformation;
}
const result = await getLanguageInformationInternal(context);
if (result) {
_languageInformation = result;
}
return result;
};

function getLanguageInformationInternal(context: vscode.ExtensionContext): Promise<LanguagesInformation | undefined> {
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType0<LanguagesInformation, void>(
try {
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType0<LanguagesInformation, void>(
"Info"
);
return await connection.sendRequest(
);
return await connection.sendRequest(
request,
);
});
);
});
} catch (error) {
return Promise.resolve(undefined);
}
};

export function getLanguageInformationForDescription(context: vscode.ExtensionContext, descriptionUrl: string, clearCache: boolean): Promise<LanguagesInformation | undefined> {
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, LanguagesInformation, void>(
try {
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, LanguagesInformation, void>(
"InfoForDescription"
);
return await connection.sendRequest(
);
return await connection.sendRequest(
request,
descriptionUrl,
clearCache
);
});
);
});
} catch (error) {
return Promise.resolve(undefined);
}
};
71 changes: 36 additions & 35 deletions vscode/microsoft-kiota/src/kiotaInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ import { KiotaGenerationLanguage, KiotaPluginType } from './enums';
import { ensureKiotaIsPresent, getKiotaPath } from './kiotaInstall';
import { getWorkspaceJsonDirectory } from "./util";

export async function connectToKiota<T>(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise<T | undefined>, workingDirectory:string = getWorkspaceJsonDirectory()): Promise<T | undefined> {
const kiotaPath = getKiotaPath(context);
await ensureKiotaIsPresent(context);
const childProcess = cp.spawn(kiotaPath, ["rpc"],{
cwd: workingDirectory,
env: {
...process.env,
// eslint-disable-next-line @typescript-eslint/naming-convention
KIOTA_CONFIG_PREVIEW: "true",
export async function connectToKiota<T>(context: vscode.ExtensionContext, callback: (connection: rpc.MessageConnection) => Promise<T | undefined>, workingDirectory: string = getWorkspaceJsonDirectory()): Promise<T | undefined> {
const kiotaPath = getKiotaPath(context);
await ensureKiotaIsPresent(context);
const childProcess = cp.spawn(kiotaPath, ["rpc"], {
cwd: workingDirectory,
env: {
...process.env,
// eslint-disable-next-line @typescript-eslint/naming-convention
KIOTA_CONFIG_PREVIEW: "true",
}
});
let connection = rpc.createMessageConnection(
new rpc.StreamMessageReader(childProcess.stdout),
new rpc.StreamMessageWriter(childProcess.stdin));
connection.listen();
try {
return await callback(connection);
} catch (error) {
const errorMessage = (error as { data?: { message: string } })?.data?.message
|| 'An unknown error occurred';
throw new Error(errorMessage);
} finally {
connection.dispose();
childProcess.kill();
}
});
let connection = rpc.createMessageConnection(
new rpc.StreamMessageReader(childProcess.stdout),
new rpc.StreamMessageWriter(childProcess.stdin));
connection.listen();
try {
return await callback(connection);
} catch (error) {
console.error(error);
return undefined;
} finally {
connection.dispose();
childProcess.kill();
}
}

export interface KiotaLogEntry {
level: LogLevel;
message: string;
level: LogLevel;
message: string;
}

export interface KiotaOpenApiNode {
Expand Down Expand Up @@ -76,16 +77,16 @@ export interface KiotaSearchResult extends KiotaLoggedResult {
results: Record<string, KiotaSearchResultItem>;
}
export interface KiotaSearchResultItem {
// eslint-disable-next-line @typescript-eslint/naming-convention
Title: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
Description: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
ServiceUrl?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
DescriptionUrl?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
VersionLabels?: string[];
// eslint-disable-next-line @typescript-eslint/naming-convention
Title: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
Description: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
ServiceUrl?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
DescriptionUrl?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
VersionLabels?: string[];
}

export enum ConsumerOperation {
Expand Down
54 changes: 29 additions & 25 deletions vscode/microsoft-kiota/src/migrateFromLockFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,46 @@ import { KIOTA_LOCK_FILE } from "./constants";
import { getWorkspaceJsonPath, handleMigration } from "./util";

export function migrateFromLockFile(context: vscode.ExtensionContext, lockFileDirectory: string): Promise<KiotaLogEntry[] | undefined> {
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType1<string, KiotaLogEntry[], void>(
"MigrateFromLockFile"
);
const result = await connection.sendRequest(
request,
lockFileDirectory
);
return result;
});
try {
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType1<string, KiotaLogEntry[], void>(
"MigrateFromLockFile"
);
const result = await connection.sendRequest(
request,
lockFileDirectory
);
return result;
});
} catch (error) {
return Promise.resolve(undefined);
}
};

export async function checkForLockFileAndPrompt(context: vscode.ExtensionContext) {
const workspaceFolders = vscode.workspace.workspaceFolders;

if(workspaceFolders) {
const lockFile = await vscode.workspace.findFiles(`{**/${KIOTA_LOCK_FILE},${KIOTA_LOCK_FILE}}`);
if (workspaceFolders) {
const lockFile = await vscode.workspace.findFiles(`{**/${KIOTA_LOCK_FILE},${KIOTA_LOCK_FILE}}`);

if (lockFile.length > 0) {
const result = await vscode.window.showInformationMessage(
vscode.l10n.t("Please migrate your API clients to Kiota workspace."),
vscode.l10n.t("OK"),
vscode.l10n.t("Remind me later")
);
if (lockFile.length > 0) {
const result = await vscode.window.showInformationMessage(
vscode.l10n.t("Please migrate your API clients to Kiota workspace."),
vscode.l10n.t("OK"),
vscode.l10n.t("Remind me later")
);

if (result === vscode.l10n.t("OK")) {
await handleMigration(context, workspaceFolders![0]);
await vscode.commands.executeCommand('kiota.workspace.refresh');
}
}
if (result === vscode.l10n.t("OK")) {
await handleMigration(context, workspaceFolders![0]);
await vscode.commands.executeCommand('kiota.workspace.refresh');
}
}
}
};
};

export function displayMigrationMessages(logEntries: KiotaLogEntry[]) {
const workspaceJsonUri = vscode.Uri.file(getWorkspaceJsonPath());
const successEntries = logEntries.filter(entry =>
const successEntries = logEntries.filter(entry =>
entry.level === LogLevel.information && entry.message.includes("migrated successfully")
);

Expand Down
Loading

0 comments on commit 03607d5

Please sign in to comment.