2

I have a API endpoint method like this:

@ApiMethod(name = "test")
public TestModel test(@Named("testArray") ArrayList<String> myTest) {
for (String i:myTest){
     log.warning("i=="+i);
}

A request is made as such:

{
"testArray":[
    "ID1",
    "ID2"
]
}

However, when I check my log (and also the problems this caused in my app), I see

i=="ID1,ID2"

not how i expected to see:

 i=="ID1"
 i=="ID2"

as the output. It is putting both elements of the array into myTest(0). How do I populate the Array correctly?

Japes
  • 255
  • 2
  • 16

2 Answers2

2

This issue doesn't appear to occur on the latest SDK 1.9.22.

I'm wondering how you're creating the request, and if it's possible that you're just joining the strings with commas as a single element in the array that you send. While I know it sounds crazy to do this deliberately and I don't think you're doing that, it's possible that some layer of abstraction below your code does it?

Another possibility is that you're not using the generated client library for your Endpoints API (you really should, it does a lot of work that you'd end up replicating on your own), and you're forming raw JSON to send over? In that case, I can see how maybe the request that ultimately gets sent might be different from what you expect, collapsing all the values inside the array that "testArray" is a key to into a comma-separated list.

So, my best advice is to examine how you form the request, and if possible, get access to the raw request itself inside your endpoints method to see what was sent.

Community
  • 1
  • 1
Nick
  • 3,581
  • 1
  • 14
  • 36
  • Great answer, allow me to clarify a few points. 1. The request is exactly as typed above. I simply replaces a 9 digit user ID with ID1 ID2 for SO purposed. I'm only working on the backend, front end android is pretty much magic to me, so I cannot speak about any strangness that might be happening in the request. My Frontend engineer just sent me his request via slack for debug purposes. 2. As far as the client library goes, I believe they are using it. We have our projects merged in android studio. 3. We are using the latest SDK. Can you suggest another step for debug? – Japes Jun 24 '15 at 01:46
  • If you don't know how the system works, you should have the person who does open and manage stackoverflow threads about the issues related to those systems. The JSON object posted is more the idea behind what you want to send, rather than the actual payload which is sent, it seems. What specific code forms the request in the client? When formulating the request properly, I deployed an API with code identical to yours which worked as expected. In addition, have you managed to examine the raw HttpServletRequest object? – Nick Jun 24 '15 at 17:23
  • I would have loved for the front end dev to look into this, however since we have found a work around, they are too busy. (also, they only speak Japanese). I have not looked into the req yet as we are nearing release and busy with final testing. After release, we will look into this issue further and create a patch. At that time I will update this thread as I'm personally very curious about this. thanks for looking into it. – Japes Jun 25 '15 at 00:43
  • 1
    I'm glad to hear you've found a workaround for now. Please do update with any findings in future, as I (and many future users, no doubt) am (are) similarly curious! – Nick Jun 25 '15 at 17:07
  • For the record, the work around was to parse the comma separated list that was being passed like a string. Since the IDs were fixed length, it was very easy to do. A few extra CPU cycles though, which is why I want it fixed. Let you know next week how it goes! – Japes Jun 26 '15 at 01:14
1

The easiest way, since this seems like a bug, is to not use a named parameter. Create a new request class that has a list field and use that as an unnamed (aka resource) parameter, instead.

saiyr
  • 2,575
  • 1
  • 11
  • 13
  • seems like even more work. My current workaround was to change the type to a simple String. Since the request was populating the ArrayList (0) with a comma separated list, I just parsed the list into an array (I already had this function written) and continue on. I will test your method when time allows and select it as correct if it works out. – Japes Jun 23 '15 at 00:43
  • as well, I should add that the request is comming from an associated android app. Just in case that makes a difference. – Japes Jun 23 '15 at 00:45