When writing a controller for a Lightning Component, it is convenient to add the data transfer object classes as inner classes of the controller. This works fine for sending data to the client but I get a gack that includes a java.lang.UnsupportedOperationException when sending from client to server:
An internal server error has occurred\nError ID: 2066801247-4851
(-1600101374)\n\norg.auraframework.throwable.AuraExecutionException:
apex://LifePaymentsController:
java.lang.UnsupportedOperationException\n\tat
.(apex://LifePaymentsController)\n\tat
ui.services.facades.CoreLightningComponentFacadeImpl.runApexAction(CoreLightningComponentFacadeImpl.java:233) …Here is what the server-side looks like (the save1 method fails):
public class SaveRequest { @AuraEnabled public Payee[] newPayees; @AuraEnabled public Payment[] payments; } ... // Fails @AuraEnabled public static void save1(SaveRequest saveRequest) { System.debug(JSON.serializePretty(saveRequest)); } // Works @AuraEnabled public static void save2(String jsonString) { SaveRequest r = (SaveRequest) JSON.deserializeStrict(jsonString, SaveRequest.class); System.debug(JSON.serializePretty(r)); }
and the client-side:
// Fails var action = servicesComponent.get("c.save1") action.setParams({ "saveRequest": saveRequest }); // Works var action = servicesComponent.get("c.save2"); action.setParams({ "jsonString": JSON.stringify(saveRequest) });
The save2 alternative method (where deserialization is explicitly coded) works.
I interpret these results to mean that inner classes are still (April 2017) not fully supported in @AuraEnabled methods. Is that the case?
Answer
Unfortunately I wasn’t able to make it work using the class directly, either with an inner class or even a top level class. Using the second solution with a String attribute in your AuraEnabled method and then deserialize it is the best solution I’ve found. It works for both inner class and top level class.
I’ve just published a blog post to list all the issues I’ve found with AuraEnabled types (unfortunately this one isn’t the only one) and the workaround I used. You can find it here:
https://blog.texei.com/lightning-components-auraenabled-method-parameters-whats-working-and-what-s-not-83c351356104
Attribution
Source : Link , Question Author : Keith C , Answer Author : Fabien Taillon