Lightning component calling wrong overloaded apex class method

I think in general it's asking for trouble to overload a method and make both versions @AuraEnabled. You've seen that it will allow you to fire the method with missing parameters, so I suggest you just keep only the second method and check for a null:

@AuraEnabled
public static List<KnowledgeArticleVersion> returnArticlesWithTwoTopicIds(String mainTopicId, String secondaryTopicId, Integer limitAmount, String status) {
    status = (status == null) ? 'Online' : status;
    // build query string
    List<KnowledgeArticleVersion> articles = Database.query(query);
    return articles;
}

And if you have a scenario where overloads are useful in your Apex code but only one version ever needs to be called from Lightning, then only make that one overload @AuraEnabled.


I would suspect that it uses the most complete signature like Java does.

(i.e. calling myFunction() will execute myFunction(a,b){})

You could get around this by changing the signatures as follows:

public with sharing class KnowledgeArticleVersionCtrl {
    @AuraEnabled
    public static List<KnowledgeArticleVersion> returnArticlesWithTwoTopicIds(String mainTopicId, String secondaryTopicId, Integer limitAmount) {
        return returnArticlesWithTwoTopicIds(mainTopicId, secondaryTopicId, 'Online', limitAmount );
    }

    @AuraEnabled
    public static List<KnowledgeArticleVersion> returnArticlesWithTwoTopicIds(String mainTopicId, String secondaryTopicId, String status, Integer limitAmount ) {
        // build query string
        List<KnowledgeArticleVersion> articles = Database.query(query);
        return articles;
    }
}

Doing this, it should execute the method you expect vs the last one.