azyoot
azyoot

Reputation: 1172

ApiTransformer for parametrized, unavailable type

I'm using Objectify and wish to have its Key<> type passed around in my API. I've created an ApiTransformer, but my questions is where to declare it, since the serialized Key<> class is not available, hence I cannot declare its transformer as a class annotation. I tried declaring it in the @Api annotation, but it doesn't work, I still get the error:

There was a problem generating the API metadata for your Cloud Endpoints classes: java.lang.IllegalArgumentException: Parameterized type com.googlecode.objectify.Key<[my package].User> not supported. 

The ApiTransformer looks like:

public class KeyTransformer implements Transformer<Key<?>, String> {

  public String transformTo(Key<?> in) {
    return in.getString();
  }

  public Key<?> transformFrom(String in) {
    return Key.valueOf(in);
  }

}

And in my @Api I have:

@Api(name = "users", version = "v1",transformers = {KeyTransformer.class})

Upvotes: 4

Views: 546

Answers (2)

johngray1965
johngray1965

Reputation: 652

You can add transforms to 3rd party classes by listing the transform in @Api annotation. I'm not dead sure it'll work parameterized class, but I don't see why not.

https://cloud.google.com/appengine/docs/java/endpoints/javadoc/com/google/api/server/spi/config/Api#transformers()

Upvotes: 0

jirungaray
jirungaray

Reputation: 1674

Unfortunately you can't. As you said you need to declare it on the Key class, your only chances to make this work are either.

1) Recompile the Key class for objectify with the @transformer annotation.

2) Extend the Key class with your own implementation and define the transformer there.

I don't really like any of those options so the way i usually resolve this is to hide the key object getter (by using @ApiResourceProperty(ignored=AnnotationBoolean.TRUE)) and only expose the id from that key.

That way you get a Endpoints frendly object, the only downside is you'll have to reconstitute the key using Key.create(YourClass.class, longId) manually whenever you need it.

Upvotes: 2

Related Questions