Skip to content

Commit

Permalink
feat: add operations tag to esr xml and operationId to yaml (#6013)
Browse files Browse the repository at this point in the history
* feat: add operations tag to esr xml and operationId to yaml

* chore: lint

* chore: update operations on overwrite

* chore: ?
  • Loading branch information
CristiCanizales authored Jan 18, 2025
1 parent a41b501 commit e0d4141
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { nls } from '../messages';
import {
ApexClassOASEligibleResponse,
ApexClassOASGatherContextResponse,
ApexOASInfo
ApexOASInfo,
ExternalServiceOperation
} from '../openApiUtilities/schemas';
import { getTelemetryService } from '../telemetry/telemetry';
import { MetadataOrchestrator } from './metadataOrchestrator';
Expand Down Expand Up @@ -244,6 +245,7 @@ export class ApexActionController {
const baseName = path.basename(fullPath).split('.')[0];
const safeOasSpec = oasSpec.replaceAll('"', ''').replaceAll('type: Id', 'type: string');
const { description, version } = this.extractInfoProperties(safeOasSpec);
const operations = this.getOperationsFromYaml(safeOasSpec);
const orgVersion = await (await WorkspaceContextUtil.getInstance().getConnection()).retrieveMaxApiVersion();
if (!orgVersion) {
throw new Error(nls.localize('error_retrieving_org_version'));
Expand All @@ -258,7 +260,12 @@ export class ApexActionController {
if (jsonObj.ExternalServiceRegistration?.schema) {
jsonObj.ExternalServiceRegistration.schema = safeOasSpec;
} else {
throw new Error('schema_element_not_found');
throw new Error(nls.localize('schema_element_not_found'));
}
if (jsonObj.ExternalServiceRegistration?.operations?.ExternalServiceOperation) {
jsonObj.ExternalServiceRegistration.operations.ExternalServiceOperation = operations;
} else {
throw new Error(nls.localize('operations_element_not_found'));
}
} else {
// Create a new XML structure
Expand All @@ -274,6 +281,9 @@ export class ApexActionController {
schemaUploadFileName: `${baseName.toLowerCase()}_openapi`,
status: 'Complete',
systemVersion: '3',
operations: {
ExternalServiceOperation: operations
},
registrationProvider: baseName,
...(this.isVersionGte(orgVersion, '63.0') // Guarded inclusion for API version 254 and above (instance api version 63.0 and above)
? {
Expand Down Expand Up @@ -309,9 +319,26 @@ export class ApexActionController {
if (!parsed?.info?.description || !parsed?.info?.version) {
throw new Error(nls.localize('error_parsing_yaml'));
}

return {
description: parsed?.info?.description,
version: parsed?.info?.version
};
};
private getOperationsFromYaml = (oasSpec: string): ExternalServiceOperation[] => {
const parsed = parse(oasSpec);
if (!parsed?.paths) {
throw new Error(nls.localize('error_parsing_yaml'));
}
const operations = parsed.paths
? Object.keys(parsed.paths).flatMap(p =>
Object.keys(parsed.paths[p]).map(operation => ({
name: parsed.paths[p][operation].operationId,
active: true
}))
)
: [];

return operations;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class MetadataOrchestrator {
Ensure no sensitive details are included. Decline requests unrelated to OpenAPI v3 specs or asking for sensitive information.`;

const userPrompt =
'Generate an OpenAPI v3 specification for the following Apex class. The OpenAPI v3 specification should be a YAML file. The paths should be in the format of /{ClassName}/{MethodName} for all the @AuraEnabled methods specified. For every `type: object`, generate a `#/components/schemas` entry for that object. The method should have a $ref entry pointing to the generated `#/components/schemas` entry. Only include methods that have the @AuraEnabled annotation in the paths of the OpenAPI v3 specification. I do not want AUTHOR_PLACEHOLDER in the result. Do not forget info.description property';
'Generate an OpenAPI v3 specification for the following Apex class. The OpenAPI v3 specification should be a YAML file. The paths should be in the format of /{ClassName}/{MethodName} for all the @AuraEnabled methods specified. For every `type: object`, generate a `#/components/schemas` entry for that object. The method should have a $ref entry pointing to the generated `#/components/schemas` entry. Only include methods that have the @AuraEnabled annotation in the paths of the OpenAPI v3 specification. I do not want AUTHOR_PLACEHOLDER in the result. Do not forget info.description property. For each path, you define operations (HTTP methods) that can be used to access that path. Make sure that each operation includes a mandatory operationId property, which should be a unique string matching the operations name.';

const systemTag = '<|system|>';
const endOfPromptTag = '<|endofprompt|>';
Expand Down
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-apex/src/messages/i18n.ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const messages = {
registry_access_failed: 'Failed to retrieve ESR directory name from the registry.',
full_path_failed: 'Failed to determine the full path for the OpenAPI document.',
schema_element_not_found: 'The <schema> element was not found in the provided XML.',
operations_element_not_found: 'The <operations> element was not found in the provided XML.',
error_retrieving_org_version: 'Failed to retrieve org version',
error_parsing_yaml: 'Error parsing YAML'
};
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-apex/src/messages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const messages = {
registry_access_failed: 'Failed to retrieve ESR directory name from the registry.',
full_path_failed: 'Failed to determine the full path for the OpenAPI document.',
schema_element_not_found: 'The <schema> element was not found in the provided XML.',
operations_element_not_found: 'The <operations> element was not found in the provided XML.',
error_retrieving_org_version: 'Failed to retrieve org version',
error_parsing_yaml: 'Error parsing YAML'
};
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export type ApexOASInfo = {
description: string;
version: string;
};

export type ExternalServiceOperation = {
name: string;
active: boolean;
};
// export interface ApexClassOASFilter {
// modifiers: string[] | null;
// }
Expand Down

0 comments on commit e0d4141

Please sign in to comment.