[Java] Templates to support google-api-client library#6838
[Java] Templates to support google-api-client library#6838wing328 merged 14 commits intoswagger-api:masterfrom
Conversation
…ing API instances
JFCote
left a comment
There was a problem hiding this comment.
I did a quick check. Look goods to me. I just have some points that needs clarification. Thanks for the PR.
| supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java")); | ||
| supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java")); | ||
| // google-api-client doesn't use the Swagger auth, because it uses Google Credential directly (HttpRequestInitializer) | ||
| if (!"google-api-client".equals(getLibrary())) { |
There was a problem hiding this comment.
I think this should go in the GoogleApiClientCodegen.java since it's only useful for your generator.
There was a problem hiding this comment.
Okay, I'll do that right now, sounds like a plan.
There was a problem hiding this comment.
FYI this is following the existing pattern. There actually aren't any subclasses of JavaClientCodegen; it uses if-statements on the library throughout to determine the configuration of each library. I'd have to figure out how to support a new subclass, it looks like it uses reflection to instantiate the JavaClientCodegen with the classes listed in modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig
| } | ||
|
|
||
| if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary())) { | ||
| if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary() || "google-api-client".equals(getLibrary()))) { |
There was a problem hiding this comment.
oh I see other library has done it too... @wing328, is this normal? Since we are using objects, we should'nt have any "if" in the JavaClientCodegen... It should all be in specific file of each generator...
Or there is a good reason for this ??
There was a problem hiding this comment.
Yeah, it appears there are no subclasses of JavaClientCodegen, see above comment, @wing328 probably has a more complete explanation.
There was a problem hiding this comment.
Ok. It's weird because for example, for the server generator "java play framework" which I maintain, there is:
JavaPlayFrameworkCodegen -> AbstractJavaCodegen -> DefaultCodegen.
This prevent having "if" everywhere.
Same thing for Typescript jQuery which I maintain too:
TypeScriptJqueryClientCodegen -> AbstractTypeScriptClientCodegen -> DefaultCodegen
I wonder why the JavaClientCodegen are all mixed in one file.
For the moment, don't change anything. We will see with @wing328 answers :)
There was a problem hiding this comment.
That makes sense. Yeah, a refactor into subclasses probably belongs in a separate pull request, but I'm game to do whatever you suggest.
There was a problem hiding this comment.
This is ok for now and belong in another pull request like you said. :)
There was a problem hiding this comment.
When we added support for other HTTP libraries, we didn't foresee we can support so many libraries (9 HTTP libraries support so far thanks to the awesome community). I agree a separate PR to refactor into subclasses would be ideal.
There was a problem hiding this comment.
That makes sense. Is this waiting on any additional reviews? Let me know if any more changes are needed here. Thanks! This will really help out our company in our migration to use Swagger clients for all of our service-to-service communication. (And possibly we could use this for our Android app, too).
There was a problem hiding this comment.
@charlescapps I'll take another look later today and merge accordingly.
| public List<CodegenParameter> queryParams = new ArrayList<CodegenParameter>(); | ||
| public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>(); | ||
| public List<CodegenParameter> formParams = new ArrayList<CodegenParameter>(); | ||
| public List<CodegenParameter> requiredParams = new ArrayList<>(); |
There was a problem hiding this comment.
I think you need to put CodegenParameter in the ArrayList to be able to compile swagger-codegen in Java < 1.7
There was a problem hiding this comment.
Will do right now! Yes, makes sense to support 1.6.
| public class CodegenOperation { | ||
| public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>(); | ||
| public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, | ||
| public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams, |
There was a problem hiding this comment.
I'm surprised this is needed. Most generator use the params array and loop with "isRequired" to build almost everything. Is there any specific reason to add this to the "generic code"?
There was a problem hiding this comment.
Yeah, you can check {{isRequired}} but that's not sufficient, because we use {{hasNext}} to see if there's a subsequent element, and that isn't aware of isRequired, so it was generating code with 2 commas in a row.
|
@charlescapps thanks for the PR, which looks good to me and my test results are good. cc @lwander |
|
UPDATE: Added the new Java API client to CircleCI via cf5eba8 |
|
Thanks for being so responsive. I'm excited to use this new client, now we won't need to build from a fork. |
|
@charlescapps FYI. I've filed #6885 to skip the check for body parameter, which can be optional Ref: #6878 |
|
@charlescapps looks like #6885 introduces an issue. In the latest master, I ran May I know if you've time to take a look? |
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.shand./bin/security/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\.3.0.0branch for changes related to OpenAPI spec 3.0. Default:master.@bbdouglas (2017/07) @JFCote (2017/08) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09)
Description of the PR
See issue #6825
This PR adds support for generating client code for the Google APIs Client for Java. This HTTP client is not just for Google's APIs, as you can read in the description of their page:
Motivation
The
google-api-clientis really standard in GCP for making HTTP requests. It works great with authentication in GCP (or outside GCP), by automatically handling refresh tokens with the googleCredentialclass. Furthermore, this provides a clear upgrade path for anyone on Google Cloud Endpoints who wants to migrate to a better REST framework, since Cloud Endpoints uses thegoogle-api-client.Changes in this PR
google-api-clientmodules/swagger-codegen/src/main/resources/Java/libraries/google-api-clientDefaultCodegento have a list of required/body params, so the client can generate overloaded methods that use aMap<String, Object>for query params.google-api-client, and verified they compile, etc.Features of the generated Google ApiClient
apiClient.myFooApi().doSomeRequest(params...)Map<String, Object>, and methods ending inforHttpResponsethat return the rawHttpResponserather than deserializing as a POJO.basePath,HttpTransport,HttpRequestInitializer(used for auth), andObjectMapper.ObjectMapperInstead of using the problematic Google-custom-Jackson (e.g.com.google.api.client.json.jackson2.JacksonFactory)authpackage, because I'm not sure what that would gain us; it's really straightforward to use anHttpRequestInitializerto provide and access token, e.g. you can usecom.google.api.client.auth.oauth2.Credentialand it can also do token refreshes automatically (or just use an access token if that's all you have).cc: @drd