Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Endpoints: Arrays or collections of entity types are not allowed

Why is there this limitation in Google Cloud Endpoints:

Arrays or collections of entity types are not allowed.

For an API with method:

@ApiMethod(name = "getCollection", path = "getCollection",  httpMethod = HttpMethod.POST)
public ArrayList<MyObject> getCollection(List<MyObject> pMyObjects) {

And what's the best way to get around this? Thanks!

like image 882
Shaun Avatar asked Oct 30 '13 23:10

Shaun


1 Answers

I think the reason it's not supported is because the named parameters in the method signature end up being URL query parameters, and they don't want to pollute that with long lists of items. Furthermore, they only support a single object of an Entity type in the signature, because this automatically becomes the "request body". You can read about it here in the docs.

As for working around it, you create a container entity object for the "request body". The nice side effect of this is that the APIs Explorer will expand the pieces of your entity object out in the GUI and help you do the JSON correctly.

Here's an example that adds a Map named "patchFieldOps" for implementing partial update. You can put as many fields into your Entity object as you like. I think if you embed more user-defined types they will also need to have the @Entity annotation.

@Entity
public class EndpointUpdateRequestBody {
    // Since Google Cloud Endpoints doesn't support HTTP PATCH, we are overloading
    // HTTP PUT to do something similar.
    private Map<String, String> patchFieldsOps;

    public EndpointUpdateRequestBody() {
        patchFieldsOps = new HashMap<String, String>();
    }

    public EndpointUpdateRequestBody(Map<String, String> patchFieldsOps) {
        this.patchFieldsOps = patchFieldsOps;
    }

    public Map<String, String> getPatchFieldsOps() {
        return patchFieldsOps;
    }

    public void setPatchFieldsOps(Map<String, String> patchFieldsOps) {
        this.patchFieldsOps = patchFieldsOps;
    }
}

...

@ApiMethod(
        name = "stuff.update",
        path = "stuff/{id}",
        httpMethod = ApiMethod.HttpMethod.PUT
)
public Group update(
        User apiUser,
        @Named("id") String id,
        @Nullable @Named("name") String name,
        @Nullable @Named("description") String description,
        EndpointUpdateRequestBody requestBody)
        throws OAuthRequestException, InternalServerErrorException, NotFoundException,
        BadRequestException, UnauthorizedException, ConflictException {
like image 191
John Michelau Avatar answered Nov 25 '22 14:11

John Michelau