skallin / google-plugin-for-eclipse

Automatically exported from code.google.com/p/google-plugin-for-eclipse
Eclipse Public License 1.0
0 stars 0 forks source link

The classes generated by Google Cloud Endpoints should not be final and private #216

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm discussing this issue in the context of the code generated for 
Java/Android, but I think this would apply to varying degrees to other target 
platforms as well.

The method of generating client classes used by Google Cloud Endpoints has many 
advantages, but it is also very restrictive in some ways.  As an example, I 
posted this:
http://stackoverflow.com/questions/17180690/json-deserializing-issues-when-using
-both-gae-endpoints-and-gcm-on-android

I think it would help if we were able to inherit from the generated classes.  
This is currently not possible because the generated code is final.

What steps will reproduce the problem?
1. Create an endpoint that includes a class, e.g.
 @ApiMethod
 public void sendData( DataParcel data) { ... }

2. Create the 'DataParcel' class:
 public class DataParcel {
   public int dataType = -1;
   public String data;
   ...
}

3. Generate Cloud Endpoint Client Library (for Android in this case)

What is the expected output? What do you see instead?

The generated class looks like this:
@SuppressWarnings("javadoc")
public final class DataParcel extends com.google.api.client.json.GenericJson {
  @com.google.api.client.util.Key
  private java.lang.String debugStr;
  ...
}

I would prefer to see this:
public class DataParcel extends com.google.api.client.json.GenericJson {
  @com.google.api.client.util.Key
  protected java.lang.String debugStr;
  ...
}

Removing the 'final' and changing 'private' to 'protected' allows me to 
subclass the generated class.

What version of the product are you using? On what operating system?

I"m using GAE/J SDK 1.8.1.1 with Eclipse 4.2.2 (and Android SDK 4.2.2, it so 
happens).  GPE for Eclipse 4.2 is 3.2.4.v201306061638-rel-r42.  I'm on Windows 
7 32bit.

Original issue reported on code.google.com by t...@malcolmson.ca on 19 Jun 2013 at 5:17

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
The code generation service is independent of GPE, but it looks like you're 
trying to recreate the functionality of the endpoints framework on your own.  
As long as you're within the GCM data limit (4k?), you should be able to reuse 
the class (DataParcel) on the AppEngine side and Android side.  If you're 
asking what serialization method you should use, that is up to you.  You DONT 
have to use any of the http response stuff.  Just regular JSON serialization 
and de-serialization should work.

Original comment by a...@google.com on 19 Jun 2013 at 6:45

GoogleCodeExporter commented 9 years ago
"The code generation service is independent of GPE..."

Yes, but I couldn't find a better place to add this issue.

The AppEngine side is not a problem - I can add any code I want to the server 
version of DataParcel.

On Android, the generated version is annotated for Google Client Library JSON.  
At first glance I was not able to find a way to use that libraries' JSON 
functionality independent of HTTP request response, but perhaps I missed 
something.

In any event, I have written regular JSON code for my DataParcel class, as you 
suggest.  That code would be much more elegant if I could inherit from 
DataParcel, and the issue will become more important when I have 10 or 100 such 
classes that need JSON and other code (my validity test and debug print 
functions for starters).

So, if there is a reason that the generated code must be final and private then 
that is fine, but if there isn't, then I think it would be helpful if that 
could be changed.

Original comment by t...@malcolmson.ca on 19 Jun 2013 at 7:02

GoogleCodeExporter commented 9 years ago
Okay, so I was playing around with this a little and maybe I don't completely 
understand your issue... but I think I may have a solution for what I think it 
is.

To use the Google GsonFactory (toString an fromString) functionality you need 
to annotate your class (just the fields) on the App Engine Side with the same 
annotations you see on the generated client libraries side.  Those annotations 
actually decide what fields are going to be serialized and how, if they are 
missing the field is ignored by the GsonFactory functions. (I've only tried 
this in a simple example, I'm not sure if/how it would it affect your 
persistence)

I'm not sure how much code you added to do the JSON stuff, but with GsonFactory 
the extra code is very small.

Original comment by a...@google.com on 19 Jun 2013 at 8:20

GoogleCodeExporter commented 9 years ago
I appreciate your continued effort to help me out, but I'm not sure I follow.  
The Java classes generated by endpoints have annotations like this:

  @com.google.api.client.util.Key
  private java.lang.String typeId;

I can copy those annotations to my server side code, but I think those 
annotations will only work with the Google Client Library.  (I think GSON uses 
@Expose?)

Actually, adding JSON serializing on the server side wasn't a problem - I have 
full control of my class on the server.  

Where I had to get hacky was on the client side - due to the lack of 
flexibility and control that the developer has over the generated code.  I 
think I've seen other devs comment on that too - that they hoped for more 
@ApiMethod type annotations to allow them great control over the generated 
client side code.

Original comment by t...@malcolmson.ca on 19 Jun 2013 at 8:38

GoogleCodeExporter commented 9 years ago
Yeah I guess I should have mentioned you need some of the client library jars 
added to your App Engine project. So you could do that if you like, but I see 
what you're saying.

The code gen guys said you should file your bug against "final" in this list : 
https://code.google.com/p/google-api-java-client/issues/list

Original comment by a...@google.com on 19 Jun 2013 at 9:17

GoogleCodeExporter commented 9 years ago
Okay, I added it as:
https://code.google.com/p/google-api-java-client/issues/detail?id=802

Original comment by t...@malcolmson.ca on 19 Jun 2013 at 9:46

GoogleCodeExporter commented 9 years ago
I have tried the GsonFactory solution, but I have the feeling that the 
endpoints are not using the GsonFactory at all. I had to add some maven 
dependencies to get the GsonFactory into my project. 
So, is it not possible to use the same json classes that the endpoints are 
using? Because that would mean I have exactly the same as the endpoint and 
don't need extra annotations. 

Original comment by Peter.Fortuin on 28 Apr 2014 at 9:03