2

Any simple @RestResource may return results in the form of an instance of an inner class. I expect any private attribute of the inner class to stay exposed only to the inner class, thus not be streamed out into the @RestResource results. However, private fields are also returned.

@RestResource(urlMapping('/mywebservice/v1/*)
global without sharing class MyWebservice_V1 {

    global class Result {
        String data = 'abc';
        private Boolean isClient = FALSE;
    }

    @HttpGet
    global static Result doGet() {
        return new Result();
    }
}

// Returns the following :
// {
//     "data" : "abc",
//     "isClient" : true
// }

// Expected result :
// {
//     "data" : "abc",
// }

How can I remove private fields from the streamed @RestResource returned result ?

2
  • 1
    Does this answer your question? Automated testing for transient keyword – identigral Feb 18 at 18:37
  • 1
    I think the link provided above answers this question. But the question itself is different than the one asked here. I wouldn't want to close this question as a Dupe, perhaps we can copy the relevant parts here so there is a proper answer? – Sebastian Kessel Feb 18 at 20:03
4

The docs do (kind of) call this out: (my emphasis)

You can use user-defined types for parameters in your Apex REST methods. Apex REST deserializes request data into public, private, or global class member variables of the user-defined type, unless the variable is declared as static or transient.

Their example is related to deserialization, but the inverse is also true for serialization. This is implied in the docs with the line:

Apex REST uses similar serialization logic for the response. An Apex method with a non-void return type will have the return value serialized into RestResponse.responseBody.

You will need to make the private members transient if you don't want them exposed through the API.

1
  • Ah, "transient" : good idea, and think that is the answer. I tried "private" before posting this, but it was not sufficient. – altius_rup Feb 25 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.