How to pass list of strings as parameter with Google Cloud Endpoints

1k views Asked by At

I am using Google Cloud Endpoints for a simple Android application. However, I am not able to pass a list of strings as parameter to an API method. If I use List as parameter type, build is fine, but strings in the list I pass are concatenated, separated by comma, and there is only one entry in the list the server receives, this concatenated string. For example if in the client I pass ["a", "b", "c"] the server receives ["a,b,c"]. Could you help me with a correct way to do it?

Edit1:

In Cloud Endpoint I have:

@ApiMethod(name = "addGroup")
public void addGroup(@Named("members") List<String> members,
                     @Named("session") String sessionString) throws ForbiddenException

In the Android client:

final List<String> selectedFriends = new ArrayList<>();
selectedFriends.add("A");
selectedFriends.add("B");
selectedFriends.add("C");         
ServerApi.getInstance().addGroup(selectedFriends,Session.JSONSession()).execute();

Edit2: The class generated automatically looks decompiled as:

public class AddGroup extends MyApiRequest<Void> {
    private static final String REST_PATH = "addGroup/{members}/{session}";
    @Key
    private List<String> members;
    @Key
    private String session;

    protected AddGroup(List<String> this$0, String members) {
        super(MyApi.this, "POST", "addGroup/{members}/{session}", (Object)null, Void.class);
        this.members = (List)Preconditions.checkNotNull(members, "Required parameter members must be specified.");
        this.session = (String)Preconditions.checkNotNull(session, "Required parameter session must be specified.");
    }
... everything as expected
}

What confuses me is the this$0 parameter and the second parameter called members (the session parameter looks to be skipped?).

Thank you!

3

There are 3 answers

0
Larry Schiefer On

I'm not sure why the annotation processor is somehow injecting this$0 as a path parameter. Does your API really need to have the members and session in part of the path? Or, can/should these both be query parameters instead (see the auto-generated code and how it has injected them there)? Try removing the @Named annotation for both of these, which should make them query parameters automatically.

0
saiyr On

Given that this method is doing modification, it should be a POST. I would recommend not using @Named here. I would create an AddGroupRequest class that contains both parameters. This way they are passed in as a JSON body rather than as URL parameters. As mentioned, this is a bug, and you can file an issue at the App Engine public tracker if you need a List to work in URL parameters. But I suggest going with my solution instead, unless you have good reason to use such a URL.

0
Bam On

I have also experienced this exact issue. Attempted different things to remedy the problem, but nothing worked.

Reported the issue to the Google AppEngine issue tracker: https://code.google.com/p/googleappengine/issues/detail?id=12105