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

Fixes issue when parsing a picklist custom field that has a single va… #222

Merged
merged 2 commits into from
Nov 26, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cparra/apexdocs",
"version": "3.7.0",
"version": "3.7.1",
"description": "Library with CLI capabilities to generate documentation for Salesforce Apex classes.",
"keywords": [
"apex",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('when parsing custom field metadata', () => {
assertEither(result, (data) => expect(data[0].type.description).toBe('A Photo URL field'));
});

test('can parse picklist values', async () => {
test('can parse picklist values when there are multiple picklist values available', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
name: 'Status__c',
Expand Down Expand Up @@ -153,6 +153,73 @@ describe('when parsing custom field metadata', () => {
assertEither(result, (data) => expect(data[0].type.pickListValues).toEqual(['Staging', 'Active', 'Inactive']));
});

test('can parse picklist values when there is a single value available', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
name: 'Status__c',
parentName: 'MyFirstObject__c',
filePath: 'src/field/Status__c.field-meta.xml',
content: `
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Status__c</fullName>
<externalId>false</externalId>
<label>Status</label>
<required>true</required>
<trackFeedHistory>false</trackFeedHistory>
<description>Status</description>
<type>Picklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
<value>
<fullName>Staging</fullName>
<default>false</default>
<label>Staging</label>
</value>
</valueSetDefinition>
</valueSet>
</CustomField>`,
};

const result = await reflectCustomFieldSources([unparsed])();

assertEither(result, (data) => expect(data[0].type.description).toBe('Status'));
assertEither(result, (data) => expect(data[0].type.pickListValues).toEqual(['Staging']));
});

test('can parse picklist values when there are no values', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
name: 'Status__c',
parentName: 'MyFirstObject__c',
filePath: 'src/field/Status__c.field-meta.xml',
content: `
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Status__c</fullName>
<externalId>false</externalId>
<label>Status</label>
<required>true</required>
<trackFeedHistory>false</trackFeedHistory>
<description>Status</description>
<type>Picklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
</valueSetDefinition>
</valueSet>
</CustomField>`,
};

const result = await reflectCustomFieldSources([unparsed])();

assertEither(result, (data) => expect(data[0].type.description).toBe('Status'));
assertEither(result, (data) => expect(data[0].type.pickListValues).toEqual(undefined));
});

test('An error is returned when the XML is in an invalid format', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
Expand Down
14 changes: 12 additions & 2 deletions src/core/reflection/sobject/parse-picklist-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ function toPickListValues(customField: MaybeTyped): string[] | undefined {
if ('valueSetDefinition' in valueSet) {
const valueSetDefinition = valueSet.valueSetDefinition as object;
if ('value' in valueSetDefinition) {
const pickListValues = valueSetDefinition.value as Record<'fullName', string>[];
return pickListValues.filter((each) => 'fullName' in each).map((current) => current.fullName);
const pickListValues = valueSetDefinition.value as Record<'fullName', string>[] | Record<'fullName', string>;
if (isArrayOfValues(pickListValues)) {
return pickListValues.filter((each) => 'fullName' in each).map((current) => current.fullName);
} else {
return [pickListValues.fullName];
}
}
}
}

return undefined;
}

function isArrayOfValues(
value: Record<string, unknown> | Record<string, unknown>[],
): value is Record<string, unknown>[] {
return Array.isArray(value);
}