forcedotcom / sfdx-scanner

MIT License
217 stars 50 forks source link

[BUG] NoSuchElementException: No value present error when passing describe to method #1247

Open simon-nc-squared opened 1 year ago

simon-nc-squared commented 1 year ago

Description: When running the DFA scanner against our codebase we are getting a NoSuchElementException: No value present exception. I've narrowed it down to when we pass a describe to a method. This is similar to https://github.com/forcedotcom/sfdx-scanner/issues/1209 so could be a duplicate.

Documentation:

java.util.NoSuchElementException: No value present
    at java.util.Optional.get(Optional.java:143) ~[?:?]
    at com.salesforce.rules.fls.apex.operations.ObjectBasedCheckUtil.isCrudCheckExpected(ObjectBasedCheckUtil.java:67) ~[sfge-1.0.1-pilot.jar:?]
    at com.salesforce.rules.fls.apex.operations.SchemaBasedValidationAnalyzer.isCheckLevelValid(SchemaBasedValidationAnalyzer.java:199) ~[sfge-1.0.1-pilot.jar:?]
    at com.salesforce.rules.fls.apex.operations.SchemaBasedValidationAnalyzer.convert(SchemaBasedValidationAnalyzer.java:174) ~[sfge-1.0.1-pilot.jar:?]
    at com.salesforce.rules.fls.apex.operations.SchemaBasedValidationAnalyzer.checkForValidation(SchemaBasedValidationAnalyzer.java:77) ~[sfge-1.0.1-pilot.jar:?]
    at com.salesforce.rules.fls.apex.operations.FlsValidationCentral.checkSchemaBasedFlsValidation(FlsValidationCentral.java:71)

sfge.log

Steps To Reproduce: Run the DFA scanner against the following code. testFails will fail with the NoSuchElementException error and testWorks will correctly report a FLS violation.

public with sharing class OptionalIssue {

    @RemoteAction @ReadOnly
    public static void testFails(Account acct) {
        DescribeSObjectResult describe = acct.getSObjectType().getDescribe();
        if (canInsertDescribe(describe)) {
            doInsert(acct);
        }
    }

    @RemoteAction @ReadOnly
    public static void testWorks(Account acct) {
        if (canInsertObject(acct)) {
            doInsert(acct);
        }
    }

    public static Boolean canInsertDescribe(DescribeSObjectResult describe) {
        if (describe.isCreateable()) {
            return true;
        }
        return false;
    }

    public static Boolean canInsertObject(Account acct) {
        if (acct.getSObjectType().getDescribe().isCreateable()) {
            return true;
        }
        return false;
    }

    public static void doInsert(Account acct) {
        Database.insert(acct);
    }
}

Expected Behaviour: For the scan to correctly report the FLS error for both methods.

Desktop:

git2gus[bot] commented 9 months ago

This issue has been linked to a new work item: W-15080502

rmohan20 commented 8 months ago

Duplicate of #1209